rubydebianruby-1.9.3pty

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-


Solution

  • 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.