Ruby on Linux PTY离开了EOF,增加Errno::EIO。

时间:2020-11-29 15:58:58

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.

我正在编写一些代码,它接受一个文件,将该文件传递给多个二进制文件中的一个进行处理,并监视错误的转换过程。我已经在OSX上编写并测试了下面的例程,但是由于我不清楚的原因,linux失败了。

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

运行的命令变化很大,##标记的代码已经被简化为本地的echo,试图调试这个问题。命令执行,脚本在终端输出预期的输出,然后抛出异常。

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

它在Debian系统上产生的错误是:Errno::EIO(输入/输出错误—/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:

我能想到的所有命令字符串都会产生这个错误,当我运行代码时,没有本地的echo block,它运行良好:

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.

在这两种情况下,命令本身都执行得很好,但是看起来debian linux并没有发送eof的pty。PTY的doc页面,以及ruby-doc上的IO都没有提供任何帮助。

Any suggestions? Thanks.

有什么建议吗?谢谢。

-vox-

vox -

2 个解决方案

#1


15  

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.

因此,我必须尽可能地阅读PTY库的C源代码,才能对这里发生的事情感到满意。

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

Ruby PTY doc并没有真正说明源代码中注释的内容。

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:

它基本上和ptya一样使用。

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.

我很沮丧地发现,这是一个有效的回应,因为它完全没有在ruby-doc上登记。

#2


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.

它似乎对Errno::EIO在这里被提出(它只是意味着子进程已经完成并关闭了流),所以您应该期待并捕获它。

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/

例如,您可以在Ruby和http://www.shanison.com/2010/09/11/ptychildexit-exception -exit-status/中从外部进程的STDOUT中连续读取选中的答案。

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.

顺便说一下,我做了一些测试。在Ubuntu 10.04上的Ruby 1.8.7上,我没有出错。使用Ruby 1.9.3,我做到了。在Ubuntu的1.8和1.9模式中使用JRuby 1.6.4,我不会出错。在OS X上,有1.8.7、1.9.2和1.9.3,我不会出错。这种行为显然依赖于您的Ruby版本和平台。

#1


15  

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.

因此,我必须尽可能地阅读PTY库的C源代码,才能对这里发生的事情感到满意。

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

Ruby PTY doc并没有真正说明源代码中注释的内容。

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:

它基本上和ptya一样使用。

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.

我很沮丧地发现,这是一个有效的回应,因为它完全没有在ruby-doc上登记。

#2


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.

它似乎对Errno::EIO在这里被提出(它只是意味着子进程已经完成并关闭了流),所以您应该期待并捕获它。

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/

例如,您可以在Ruby和http://www.shanison.com/2010/09/11/ptychildexit-exception -exit-status/中从外部进程的STDOUT中连续读取选中的答案。

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.

顺便说一下,我做了一些测试。在Ubuntu 10.04上的Ruby 1.8.7上,我没有出错。使用Ruby 1.9.3,我做到了。在Ubuntu的1.8和1.9模式中使用JRuby 1.6.4,我不会出错。在OS X上,有1.8.7、1.9.2和1.9.3,我不会出错。这种行为显然依赖于您的Ruby版本和平台。