Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JRUBY-6165: Wait for child popen process to finish when opened for write
Also clean up unused outputPumper and extraneous setting of exit
status (already done in RubyIO#close2)
  • Loading branch information
nicksieger committed Oct 26, 2011
1 parent 080c52b commit 7443186
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
5 changes: 1 addition & 4 deletions src/org/jruby/RubyIO.java
Expand Up @@ -3535,19 +3535,16 @@ public static IRubyObject popen(ThreadContext context, IRubyObject recv, IRubyOb
if (io.openFile.isOpen()) {
io.close();
}
context.setLastExitStatus(RubyProcess.RubyStatus.newProcessStatus(runtime, process.waitFor(), ShellLauncher.getPidFromProcess(process)));
}
}
return io;
} catch (InvalidValueException ex) {
throw runtime.newErrnoEINVALError();
} catch (IOException e) {
throw runtime.newIOErrorFromException(e);
} catch (InterruptedException e) {
throw runtime.newThreadError("unexpected interrupt");
}
}

private static class Ruby19POpen {
public final RubyString cmd;
public final IRubyObject[] cmdPlusArgs;
Expand Down
30 changes: 13 additions & 17 deletions src/org/jruby/util/ShellLauncher.java
Expand Up @@ -764,6 +764,7 @@ public static InputStream unwrapBufferedStream(InputStream filteredStream) {

public static class POpenProcess extends Process {
private final Process child;
private final boolean waitForChild;

// real stream references, to keep them from being GCed prematurely
private InputStream realInput;
Expand All @@ -778,14 +779,15 @@ public static class POpenProcess extends Process {
private FileChannel inerrChannel;
private Pumper inputPumper;
private Pumper inerrPumper;
private Pumper outputPumper;

public POpenProcess(Process child, Ruby runtime, ModeFlags modes) {
this.child = child;

if (modes.isWritable()) {
this.waitForChild = true;
prepareOutput(child);
} else {
this.waitForChild = false;
// close process output
// See JRUBY-3405; hooking up to parent process stdin caused
// problems for IRB etc using stdin.
Expand All @@ -803,6 +805,7 @@ public POpenProcess(Process child, Ruby runtime, ModeFlags modes) {

public POpenProcess(Process child) {
this.child = child;
this.waitForChild = false;

prepareOutput(child);
prepareInput(child);
Expand Down Expand Up @@ -846,19 +849,7 @@ public Process getChild() {

@Override
public int waitFor() throws InterruptedException {
if (outputPumper == null) {
try {
if (output != null) output.close();
} catch (IOException ioe) {
// ignore, we're on the way out
}
} else {
outputPumper.quit();
}

int result = child.waitFor();

return result;
return child.waitFor();
}

@Override
Expand All @@ -879,13 +870,19 @@ public void destroy() {
// processes seem to have some peculiar locking sequences, so we
// need to ensure nobody is trying to close/destroy while we are
synchronized (this) {
RubyIO.obliterateProcess(child);
if (inputPumper != null) synchronized(inputPumper) {inputPumper.quit();}
if (inerrPumper != null) synchronized(inerrPumper) {inerrPumper.quit();}
if (outputPumper != null) synchronized(outputPumper) {outputPumper.quit();}
if (waitForChild) {
waitFor();
} else {
RubyIO.obliterateProcess(child);
}
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
}

Expand Down Expand Up @@ -922,7 +919,6 @@ private void prepareOutput(Process child) {
} else {
outputChannel = null;
}
outputPumper = null;
}

private void pumpInput(Process child, Ruby runtime) {
Expand Down

0 comments on commit 7443186

Please sign in to comment.