Skip to content

Commit

Permalink
Improve gui mode gateway clean up
Browse files Browse the repository at this point in the history
This commit improves how we clean up the gateway once the UI
is closed in gui mode. Although it is highly unlikely, it is possible
for a user to run into a NameError when deleting the gateway if a user
enters gui mode but is NOT on macos and successfully exits the gui
without crashing the session (see next below, this requires some playing
around). This is resolved by assigning the gateway to None, ensuring
there is *something* to delete if no gateway object is assigned.

This commit also adds an additional check for a running JVM instance
when in gui mode on non-macos systems. This prevents a
`jpype._core.JVMNotRunning: Java Virtual Machine is not running`
error that is produced when the imagej ui visbility is polled but
is no longer available because the JVM has been shut down via
gui interactions.
  • Loading branch information
elevans committed Oct 15, 2024
1 parent b2ce4cb commit 45c1448
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/imagej/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,7 @@ def run_callbacks(ij):
if mode == Mode.GUI:
# Show the GUI and block.
global gateway
gateway = None

def show_gui_and_run_callbacks():
global gateway
Expand Down Expand Up @@ -1261,10 +1262,11 @@ def show_gui_and_run_callbacks():
gateway = show_gui_and_run_callbacks()
# We are responsible for our own blocking.
# TODO: Poll using something better than ui().isVisible().
while gateway.ui().isVisible():
while sj.jvm_started() and gateway.ui().isVisible():
time.sleep(1)

return gateway
del gateway
return None

# HEADLESS or INTERACTIVE mode: create the gateway and return it.
return run_callbacks(_create_gateway())
Expand Down

0 comments on commit 45c1448

Please sign in to comment.