On Thu, 3 Sep 2026 13:56:15 GMT, Jorn Vernee <[email protected]> wrote:
> See the JBS issue for the extended problem description. > > Native threads that were attached to the JVM through an FFM upcall are > automatically detached from the VM when they join/terminate. However, if a > native thread tries to terminate after the VM has exited without going > through `DestroyJavaVm` (e.g. as a result of calling `System.exit`), they > will block in `VM_Exit::block_if_vm_exited` inside `DetachCurrentThread`. > This may happen for instance when they try to join after the JVM has exited > in an `atexit` handler. > > This patch adds a check before trying to detach the thread to see if the VM > has exited and bails out if it has. This does not prevent issues as a result > of a race between the VM exiting and the thread joining, but it does prevent > issues in the more comment case where a thread simply outlives the VM. > > --------- > - [X] I confirm that I make this contribution in accordance with the [OpenJDK > Interim AI Policy](https://openjdk.org/legal/ai). Just to clarify the problem a little. When the process is being terminated due to VM shutdown (due to halt()/exit()) then we will block detaching threads indefinitely. In this case the application tries to join the thread in an atexit handler, which thus prevents the atexit handler from completing and so thwarts process termination. To me this is a bug in the application as it is making unreasonable assumptions about what it can do in the atexit handler (blocking in such handlers is a "Bad Idea" (TM)). That said if the fix is constrained, as in this case, to just these upcall threads, then that seems a reasonable workaround. But I suspect there may be other potential races/deadlocks possible when the application operates this way. test/jdk/java/foreign/detachafterexit/TestDetachAfterExit.java line 63: > 61: > 62: output.shouldHaveExitValue(0) > 63: .stdoutShouldContain("[await_join] done joining"); Suggestion: output.shouldHaveExitValue(0) .stdoutShouldContain("[await_join] done joining"); ------------- Marked as reviewed by dholmes (Reviewer). PR Review: https://git.openjdk.org/jdk/pull/32686#pullrequestreview-5126903463 PR Review Comment: https://git.openjdk.org/jdk/pull/32686#discussion_r3945557994
