[
https://issues.apache.org/jira/browse/EXEC-33?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12663915#action_12663915
]
Benjamin Bentmann commented on EXEC-33:
---------------------------------------
The problematic code can be reduced to:
{code:java}
InputStream is = System.in;
OutputStream os = System.out;
final byte[] buf = new byte[4096];
int length;
while ((length = is.read(buf)) > 0) {
os.write(buf, 0, length);
}
System.out.println("DONE");
{code}
i.e. this hangs, too. It appears that's a basic Java issue, cf. Sun bugs
[#4514257|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4514257],
[#4385444|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4385444] and
related. Basically {{System.in}} is an open but empty stream, so the call
{{InputStream.read()}} will block infinitely. As the mentioned Sun issues point
out, {{Thread.interrupt()}} doesn't help either once the blocking call to
{{read()}} has been made.
{{InputStream.available()}} seems the only non-blocking way to detect the empty
state of the stream but unfortunately it doesn't detect whether the stream is
closed.
Maybe one needs a special {{StreamPumper}} (e.g. subclass) for the
{{inputThread}} that pumps the streams via polling, i.e. {{available()}} +
{{read()}} + {{Thread.sleep()}}, and gets terminated when interrupted from
outside, i.e. by {{PumpStreamHandler.stop()}}, instead of stream closing. When
the subprocess has exited, there is no need to pump further input into it, so
terminating the {{inputThread}} in {{PumpStreamHandler.stop()}} without
consuming all of the input shouldn't be a problem. Surely, beauty is something
else.
> PumpStreamHandler hangs if System.in is redirect to process input stream
> ------------------------------------------------------------------------
>
> Key: EXEC-33
> URL: https://issues.apache.org/jira/browse/EXEC-33
> Project: Commons Exec
> Issue Type: Bug
> Affects Versions: 1.0
> Environment: Windows Vista
> Reporter: Marco Ferrante
> Assignee: Siegfried Goeschl
> Priority: Minor
>
> When process input is redirected using a PumpStreamHandler, e.g.
> PumpStreamHandler streamHanlder = new PumpStreamHandler(out, err,
> System.in);
> exec.setStreamHandler(streamHanlder);
> MockExecuteResultHandler handler = new MockExecuteResultHandler();
> exec.execute(cl, handler);
> the process hangs and never exit.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.