14

I'm writing some code which takes a file, passes that file to one of several binaries for processing, and monitors the conversion process for errors. I've written and tested the following routine on OSX but linux fails for reasons about which I'm not clear.

#run the command, capture the output so it doesn't display
PTY.spawn(command) {|r,w,pid|
    until r.eof? do
      ##mark
      puts r.readline
    end
}

The command that runs varies quite a lot and the code at the ##mark has been simplified into a local echo in an attempt to debug the problem. The command executes and the script prints the expected output in the terminal and then throws an exception.

The error it produces on Debian systems is: Errno::EIO (Input/output error - /dev/pts/0):

All of the command strings I can come up with produce that error, and when I run the code without the local echo block it runs just fine:

PTY.spawn(command) {|r,w,pid|}

In either case the command itself executes fine, but it seems like debian linux isn't sending eof up the pty. The doc pages for PTY, and IO on ruby-doc don't seem to lend any aid here.

Any suggestions? Thanks.

-vox-

voxobscuro
  • 1,948
  • 1
  • 20
  • 38
  • This is just a guess, but is readlines compiled in correctly to the Debian ruby version? If that is the issue and you use rvm, their notes on the issue may be of help:[http://beginrescueend.com/packages/readline/](http://beginrescueend.com/packages/readline/) – forforf Apr 20 '12 at 23:32
  • Thats a good thought. I'm not using rvm on those servers, but I did compile ruby1.9.3 from source (and on another server 1.9.2). I hadn't considered that changing the readline lib might sort it. Thanks for the suggestion. – voxobscuro Apr 21 '12 at 00:10
  • libreadline is related to command-line editing, history, etc. (e.g. in irb). It does not affect the IO#readline method. You can compile Ruby without libreadline support and IO will work as expected (but irb will be unpleasant to use). – Rich Drummond Apr 24 '12 at 21:47

3 Answers3

19

So I had to go as far as reading the C source for the PTY library to get really satisfied with what is going on here.

The Ruby PTY doc doesn't really say what the comments in the source code say.

My solution was to put together a wrapper method and to call that from my script where needed. I've also boxed into the method waiting on the process to for sure exit and the accessing of the exit status from $?:

# file: lib/safe_pty.rb

require 'pty'
module SafePty
  def self.spawn command, &block

    PTY.spawn(command) do |r,w,p|
      begin
        yield r,w,p
      rescue Errno::EIO
      ensure
        Process.wait p
      end
    end

    $?.exitstatus
  end
end

This is used basically the same as PTY.spawn:

require 'safe_pty'
exit_status = SafePty.spawn(command) do |r,w,pid|
  until r.eof? do
    logger.debug r.readline
  end
end

#test exit_status for zeroness

I was more than a little frustrated to find out that this is a valid response, as it was completely undocumented on ruby-doc.

voxobscuro
  • 1,948
  • 1
  • 20
  • 38
  • Interesting. Browsing the src, I came across this in the comments for pty_open: "The result of read operation when pty slave is closed is platform dependent". That's actually in the rdoc too, but I missed it. – Rich Drummond Apr 24 '12 at 23:08
  • @RichDrummond Yea, thats the comment I was talking about. I also missed it in the rdoc, but thats because I was looking at the notes for .spawn and not really anything else. :-/ – voxobscuro Apr 26 '12 at 19:29
5

It seems valid for Errno::EIO to be raised here (it simply means the child process has finished and closed the stream), so you should expect that and catch it.

For example, see the selected answer in Continuously read from STDOUT of external process in Ruby and http://www.shanison.com/2010/09/11/ptychildexited-exception-and-ptys-exit-status/

BTW, I did some testing. On Ruby 1.8.7 on Ubuntu 10.04, I don't get a error. With Ruby 1.9.3, I do. With JRuby 1.6.4 on Ubuntu in both 1.8 and 1.9 modes, I don't get an error. On OS X, with 1.8.7, 1.9.2 and 1.9.3, I don't get an error. The behavior is obviously dependent on your Ruby version and platform.

Community
  • 1
  • 1
Rich Drummond
  • 3,199
  • 13
  • 15
  • Bounty awarded for being the only person that had any idea what I was talking about! Had I not discovered the solution in the C source about 15 minutes before you posted this I'm pretty sure it would have led me to that anyway. Cheers! – voxobscuro Apr 27 '12 at 19:52
0

ruby-doc.org says this since ruby 1.9:

# The result of read operation when pty slave is closed is platform
# dependent.
ret = begin
        m.gets          # FreeBSD returns nil.
      rescue Errno::EIO # GNU/Linux raises EIO.
        nil
      end

Ok, so now I get this behavior is "normal" on Linux, but that means it's a little tricky to get the output of a PTY. If you do m.read it reads everything and then throws it away and raises Errno::EIO. You really need to read the content chunk by chunk with m.readline. And even then you risk losing the last line if it doesn't end with "\n" for whatever reason. To be extra safe you need to read the content byte by byte with m.read(1)

Additional note about the effect of tty and pty on buffering: it's not the same as STDOUT.sync = true (unbuffered output) in the child process, but rather it triggers line buffering, where output is flushed on "\n"

Daniel
  • 2,598
  • 4
  • 24
  • 24