Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Linux PTY goes away without EOF, raises Errno::EIO

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-

like image 374
voxobscuro Avatar asked Apr 19 '12 23:04

voxobscuro


3 Answers

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.

like image 61
voxobscuro Avatar answered Oct 31 '22 19:10

voxobscuro


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.

like image 24
Rich Drummond Avatar answered Oct 31 '22 18:10

Rich Drummond


As answered here and here, EIO can be avoided by keeping a file descriptor to the pty slave device open in the parent process.

Since PTY.spawn closes the slave file descriptor passed to the child process, a simple workaround is to open a new one. For example:

PTY.spawn("ls") do |r, w, pid|
  r2 = File.open(r.path)

  while IO.select([r], [], [], 1)
    puts r.gets
  end

  r2.close
end
like image 1
tangle Avatar answered Oct 31 '22 18:10

tangle