我正在尝试将错误日志记录添加到 JRuby 进程中,该进程从一个子进程的标准输出读取数据,并将这些数据写入另一个子进程的标准输入,同时收集一些关于数据的统计信息。子进程使用IO.popen4
.
要读取错误流,我不能使用阻塞读取,因为在正常情况下,这些流上没有输出。但是,当我read_nonblock
在这些流上使用时,我仍然会遇到 JRuby 中的阻塞读取。
为什么read_nonblock
调用会阻塞,我该如何重写这段代码,使其永远不会阻塞并始终显示任一子进程输出的任何标准错误?
下面是我正在使用的重现问题的代码的简化版本。它在 jruby 上阻塞,并且不会在ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
.
if RUBY_PLATFORM != "java" && RUBY_VERSION =~ /^1\.9/
class IO
def self.popen4(*args, &block)
require "open4"
Open4::popen4(*args, &block)
end
end
end
IO.popen4('echo', 'hi') do |_, _, stdout1, stderr1|
IO.popen4('sh', '-c', 'cat 1>&2') do |_, stdin2, _, stderr2|
stdout1.each_line do |line|
stdin2 << line
(IO.select([stderr1, stderr2], [], [], 0.1) or [[]]).first.each do |stream|
begin
# in jruby 1.6.8 (ruby-1.9.2-p312) (2012-09-18 1772b40) (Java HotSpot(TM) Client VM 1.6.0_37) [darwin-i386-java], read_nonblock blocks
# idem in jruby 1.7.2 (1.9.3p327) 2013-01-04 302c706 on Java HotSpot(TM) Client VM 1.6.0_37-b06-434-11M3909 [darwin-i386]
puts stream.read_nonblock(1000)
rescue Exception => e
puts e.message
end
end
end
end
end
我知道它在 read_nonblock 中被阻止,因为按下 ctrl+\ 时的堆栈跟踪:
"main" prio=5 tid=0000000003110800 nid=0xb0201000 runnable [00000000b01ff000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.FileDispatcher.read0(Native Method)
at sun.nio.ch.FileDispatcher.read(FileDispatcher.java:26)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:198)
at sun.nio.ch.IOUtil.read(IOUtil.java:171)
at sun.nio.ch.FileChannelImpl.read(FileChannelImpl.java:144)
- locked <0000000006158308> (a java.lang.Object)
at org.jruby.util.io.ChannelStream.refillBuffer(ChannelStream.java:196)
at org.jruby.util.io.ChannelStream.bufferedRead(ChannelStream.java:926)
at org.jruby.util.io.ChannelStream.bufferedRead(ChannelStream.java:888)
at org.jruby.util.io.ChannelStream.fread(ChannelStream.java:1288)
- locked <000000000615a8f8> (a org.jruby.util.io.ChannelStream)
at org.jruby.util.io.ChannelStream.readnonblock(ChannelStream.java:1314)
- locked <000000000615a8f8> (a org.jruby.util.io.ChannelStream)
at org.jruby.RubyIO.getPartial(RubyIO.java:2762)
at org.jruby.RubyIO.read_nonblock(RubyIO.java:2697)
at org.jruby.RubyIO$INVOKER$i$0$1$read_nonblock.call(RubyIO$INVOKER$i$0$1$read_nonblock.gen)
at org.jruby.internal.runtime.methods.JavaMethod$JavaMethodN.call(JavaMethod.java:646)
at org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:204)
at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:168)
at read_nonblock_test.chained_2_rescue_1$RUBY$SYNTHETIC__file__(read_nonblock_test.rb:19)
at read_nonblock_test.block_3$RUBY$__file__(read_nonblock_test.rb:16)
at read_nonblock_test$block_3$RUBY$__file__.call(read_nonblock_test$block_3$RUBY$__file__)
at org.jruby.runtime.CompiledBlock19.yield(CompiledBlock19.java:139)
at org.jruby.runtime.Block.yield(Block.java:130)
...