imagej / imagej/pyimagej

Auto-dispose/cleanup PyImageJ when Python is shutting down

Open
#153 7 comments 3 reactions 1 assignee Claimed by @elevans View on GitHub
bug
Dominant language
Python
Stars
534
Forks
95
PR merge metrics
No merged PRs in 30d

Description

When PyImageJ is running and is suddenly terminated (_e.g._ interrupting an active GUI session with `Ctrl + C`) can sometimes result in a terminal session waiting for `JPype` Java threads to end. This isn't great behavior -- so we should auto cleanup PyImageJ upon termination and exit cleanly.

It looks like Python has a couple ways to exit:
- [sys.exit()](https://docs.python.org/3/library/sys.html?highlight=sys%20exit#sys.exit)
- [exit()](https://docs.python.org/3/library/constants.html#exit)
- [SIGINT](https://docs.python.org/3/library/signal.html)

We can use [atexit()](https://docs.python.org/3/library/atexit.html) to run the appropriate cleanup routines. This will likely need some new hooks in `scyjava` where the JVM related methods live.

[Here](https://stackoverflow.com/questions/40866576/run-atexit-when-python-process-is-killed) is a great example using `atexit()` in addition to catching `SIGINT` generated from the `Ctrl + C` keyboard interrupt.

And here's the relevant `JPype` docs section regarding handling [JVM shutdown](https://jpype.readthedocs.io/en/latest/userguide.html#shutting-down-the-jvm).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.