Skip to content

Commit

Permalink
Avoid calling System.exit from the EDT
Browse files Browse the repository at this point in the history
Unfortunately, calling it from the EDT creates a deadlock.

SciJava Common registers a JVM shutdown hook thread that calls
dispose() on AWT windows it manages (e.g. the main ImageJ2 window),
but Window#dispose() internally runs some logic via EventQueue.invokeAndWait.

But meanwhile, the System.exit call triggers the shutdown hooks, which
use Thread#join to wait for them to finish, thus blocking the EDT.
So the invokeAndWait disposing the window can never finish.

Therefore, as a workaround here, we avoid the problem by exiting the JVM
from a new thread instead.
  • Loading branch information
ctrueden committed Apr 16, 2024
1 parent f91f457 commit f1fabc9
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/kotlin/sc/iview/SciView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import java.util.stream.Collectors
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.LinkedHashMap
import kotlin.concurrent.thread
import javax.swing.JOptionPane
import kotlin.math.cos
import kotlin.math.sin
Expand Down Expand Up @@ -1246,7 +1247,7 @@ class SciView : SceneryBase, CalibratedRealInterval<CalibratedAxis> {
// if scijavaContext was not created by ImageJ, then system exit
if( objectService.getObjects(Utils.SciviewStandalone::class.java).size > 0 ) {
log.info("Was running as sciview standalone, shutting down JVM")
System.exit(0)
thread { System.exit(0) }
}
}

Expand Down

0 comments on commit f1fabc9

Please sign in to comment.