Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process does not terminate after calling System.exit in daemon thread #10255

Closed
quijote opened this issue Jul 27, 2020 · 6 comments · Fixed by #10273
Closed

Process does not terminate after calling System.exit in daemon thread #10255

quijote opened this issue Jul 27, 2020 · 6 comments · Fixed by #10273

Comments

@quijote
Copy link

quijote commented Jul 27, 2020

There seems to be a regression introduced with 0.21 that results in process not terminating after user code calls System.exit in a daemon thread.

Probably related to:
#9516
#9558

I can reproduce this with the following code:

public class Launchee {
	public static void main(String[] args) throws Exception {
		final CountDownLatch latch = new CountDownLatch(1);
		Thread t = new Thread() {
			@Override
			public void run() {
				latch.countDown();
				System.exit(1);
			}
		};
		t.setDaemon(true);
		t.start();
		latch.await();
	}
}

This sometimes hangs when executed with AdoptOpenJDK OpenJ9 8.0.262.10. Similar problem was observed with 11.0.8.10.0 and 14.0.2.12.0.

I simply launched this in a loop:

public class Launcher {
    public static void main(String[] args) throws Exception {
        int i = 0;
        while (true) {
            System.out.println(i++);
            List<String> commandLine = new ArrayList<>();
            commandLine.add("adoptopenjdk-openj9_64-8.0.262.10.0\\bin\\java.exe");
            commandLine.add("-cp");
            commandLine.add("bin");
            commandLine.add("Launchee");
            ProcessBuilder pb = new ProcessBuilder(commandLine);
            pb.inheritIO();
            Process process = pb.start();
            process.waitFor();
        }
    }        
}

With Hotspot and previous JVMs I get

0
1
2
...

With OpenJ9 0.21 I sometimes just get:

0

and then it hangs forever.

@pshipton
Copy link
Member

@gacholio fyi

If I run Launchee on Linux with a recent build, it hangs, needs to be killed with -9.

@gacholio
Copy link
Contributor

I can see what's wrong, but I'll need some time to think about how to fix it.

@gacholio gacholio self-assigned this Jul 27, 2020
@gacholio
Copy link
Contributor

gacholio commented Jul 28, 2020

Here's the hang scenario:

main                                        daemon

DestroyJavaVM                               call System.exit()
indicate exit stared
                                            java enters monitor
                                            exitJavaVM()
                                            detect exit started, block forever (as intended)
call Shutdown.shutDown()
java blocks entering monitor

@gacholio
Copy link
Contributor

The solution appears to be moving the "indicate exit started" to after the call to Shutdown.shutdown(). This still protects against multiple VMDeath events and exit during VMDeath.

gacholio added a commit to gacholio/openj9 that referenced this issue Jul 28, 2020
Don't indicate exit until after calling Shutdown.shutDown() in
DestroyJavaVM.

See issue for details.

Fixes: eclipse-openj9#10255

[ci skip]

Signed-off-by: Graham Chapman <graham_chapman@ca.ibm.com>
@pshipton pshipton added the bug label Jul 28, 2020
@quijote
Copy link
Author

quijote commented Aug 4, 2020

Thank you for the quick fix. I did not quite follow the comment that CountDownLatch does not exist in Java 8 (it was introduced with Java 5).

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html

@gacholio
Copy link
Contributor

gacholio commented Aug 4, 2020

OK, my mistake - I had never seen it used until very recently, so I assumed it was new.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants