JDK-4730846 : (process) Wrong exit code may be returned if process ends too quickly (lnx)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2002-08-13
  • Updated: 2002-11-23
  • Resolved: 2002-11-23
Related Reports
Duplicate :  
Description

Name: nt126004			Date: 08/13/2002


FULL PRODUCT VERSION :
java version "1.4.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b14)
Java HotSpot(TM) Client VM (build 1.4.1-beta-b14, mixed mode)

FULL OPERATING SYSTEM VERSION :
glibc: glibc-2.2.5-34
kernel: Linux NetworkTester2 2.4.18-3 #1 Thu Apr 18
07:37:53 EDT 2002 i686 unknown
distribution: Red Hat Linux release 7.3 (Valhalla)


A DESCRIPTION OF THE PROBLEM :
When you launch a process to execute a (bash) shell script
on Linux, if the script uses the "exit" command to return
its exit status, that status is not returned by
Process.waitFor() correctly (as it does w/ previous
JREs).  Appears to be returning 0 almost always.

REGRESSION.  Last worked in version 1.4

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Place Test.class and test.sh in same directory.
2. Execute the Test class
3. See the results

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected (sample):

% uname -sr
Linux 2.4.18-3
% cat test.sh
#!/bin/bash
#
exit 2
% java -showversion Test
java version "1.4.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build
1.4.1-beta-b14)
Java HotSpot(TM) Client VM (build 1.4.1-beta-b14, mixed
mode)

Exit val: 2


Actual (sample):

% uname -sr
Linux 2.4.18-3
% cat test.sh
#!/bin/bash
#
exit 2
% java -showversion Test
java version "1.4.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build
1.4.1-beta-b14)
Java HotSpot(TM) Client VM (build 1.4.1-beta-b14, mixed
mode)

Exit val: 0

REPRODUCIBILITY :
This bug can be reproduced often.

---------- BEGIN SOURCE ----------
************ start Test.java **************
public class Test {
    public static void main(String args[]) {
        try {
            String[] cmd= { "/bin/bash", "-c", "./test.sh" };
            Process p = Runtime.getRuntime().exec(cmd);
            int exitVal = p.waitFor();
            System.out.println("Exit val: " + exitVal);
        }
        catch (Exception e ) {
            e.printStackTrace();
        }
    }
}
************ end Test.java **************


************ start test.sh **************
#!/bin/bash
#
exit 2
************ end test.sh **************

---------- END SOURCE ----------

Release Regression From : 1.4
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Review ID: 160096) 
======================================================================

Comments
EVALUATION The problem appears in jdk1.4.1-rc-b19. This appears to be a timing issue, a minor delay of less than 1 microsecond will consistently allow the correct exit code to be returned. -- iag@sfbay 2002-08-13
13-08-2002

SUGGESTED FIX Introduce minor (less than 1 microsecond) delay in exec'd program. -- iag@sfbay 2002-08-13
13-08-2002

WORK AROUND Change test.sh to: #!/bin/bash sleep 0 exit 2 If the exec'd program is a very short c program, a call to usleep(0) will also allow the correct exit code to return consistently. -- iag@sfbay 2002-08-13
13-08-2002