Name: nt126004 Date: 04/17/2003
FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)
and
java version "1.4.2-rc"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-rc-b20)
Java HotSpot(TM) Client VM (build 1.4.2-rc-b20, mixed mode)
FULL OS VERSION :
Linux 7ctadev01 2.4.7-10 #1 Thu Sep 6 17:27:27 EDT 2001 i686 unknown
A DESCRIPTION OF THE PROBLEM :
When using Runtime.exec() to run a shell script that spawns background processes, reads against the InputStream that is returned by Process.getInputStream() will block until the spawned process dies, even if that process is launched in the background and it's output is redirected.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
[root@7ctadev01 tmp]# cat test.sh
#!/bin/sh
echo "Launching sleep script now"
sleep 10 1>/dev/null 2>&1 &
echo "Done launching sleep script now"
[root@7ctadev01 tmp]# /usr/java/jdk1.3.1_02/bin/java Exec ./test.sh
stdout : Launching sleep script now
stdout : Done launching sleep script now
Done reading standard output
It took 35 milliseconds to read output
[root@7ctadev01 tmp]# /usr/java/j2sdk1.4.1_01/bin/java Exec ./test.sh
stdout : Launching sleep script now
stdout : Done launching sleep script now
Done reading standard output
It took 10043 milliseconds to read output
EXPECTED VERSUS ACTUAL BEHAVIOR :
The run with jdk 1.4.1 should have completed before the process spawned by the test.sh script ended, as the run with jdk 1.3.1 did. The timing was added to the code in an effort to prove that the Exec program was trying to read the output from the spawned process.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.io.*;
public class Exec {
public static void main(String[] args) {
String command = "";
for (int i = 0; i < args.length; i++) {
command += args[i];
if (i != args.length) command += " ";
}
try {
long startTime = System.currentTimeMillis();
Process process = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("stdout : " + line);
}
System.out.println("Done reading standard output");
System.out.println("It took " + (System.currentTimeMillis() - startTime)
+ " milliseconds to read output");
} catch(Exception e) {
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
(Review ID: 183327)
======================================================================