jarib/childprocess — Issue #30: Inconsistent IO on JRuby
Author: jkutner
Created: 2012-02-04 02:08:16+00:00
State: open

While trying to fix a JRuby specific bug in Vagrant , which uses ChildProcess to execute some commands, I found that output cannot always be retrieved from a subprocess.

I've been able to reproduce the bug in a single file here:
https://gist.github.com/1647275

It is creating two ChildProcesses (via the Subprocess class), and executing
them in order.

Vagrant::Util::Subprocess.new("VBoxManage", "--version").execute
Vagrant::Util::Subprocess.new("VBoxManage", "--version").execute

In the execute method, a ChildProcess is created:

process = ChildProcess.build(*@command)
stdout, stdout_writer = IO.pipe
stderr, stderr_writer = IO.pipe
process.io.stdout = stdout_writer
process.io.stderr = stderr_writer
process.duplex = true

Then it tries to read the output from the stdout pipe:

data << io.read_nonblock(READ_CHUNK_SIZE)

The first time, the ChildProcess works correctly, but the second time it
yields no output.

INFO subprocess: Starting process: ["VBoxManage", "--version"]
DEBUG subprocess: Selecting on IO
DEBUG subprocess: stdout: 4.1.8r75467
DEBUG subprocess: Waiting for process to exit. Remaining to timeout: 32000
DEBUG subprocess: Exit status: 0
INFO subprocess: Starting process: ["VBoxManage", "--version"]
DEBUG subprocess: Selecting on IO
DEBUG subprocess: Waiting for process to exit. Remaining to timeout: 31999
DEBUG subprocess: Exit status: 0

I've created a workaround in Vagrant for this issue by using IO.popen4 like this:

IO.popen4( cmd ) do |pid, stdin, stdout, stderr|
Thread.new(stdout) {|stdout_io|
stdout_io.each_line do |data|
@logger.debug("stdout: #{data}")
end
}.join
end

https://github.com/mitchellh/vagrant/pull/709

But it seems that this should be pushed down into Childprocess. I'm already looking into how this could be done, but would love some advice. Thanks.

--------------

Reproduce with this script:
https://gist.github.com/1647275

Background:
http://markmail.org/thread/e7rmwy4jp53e3wjj
https://github.com/mitchellh/vagrant/issues/658
http://jira.codehaus.org/browse/JRUBY-6362