Skip to content

Avoid starting up the JVM when logging python errors #330

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

Merged
merged 1 commit into from
Jul 21, 2025

Conversation

gselzer
Copy link
Contributor

@gselzer gselzer commented Jul 21, 2025

There are many locations in __init__.py where _log_exception is used to log debug messages during ImageJ startup. log_exception's signature suggests that it is only meant to handle Java exceptions, however in many places it is called with Python exceptions. This has the unintended effect of starting up the JVM whenever debug logging is enabled.

We should thus be careful to either (a) use _log_exception everywhere but add case logic to avoid starting up the JVM early, or (b) use _log_exception ONLY when providing a Java exception.

_log_exception's signature suggests that it is only meant to handle Java
exceptions, however in many places it is called with Python exceptions.
This has the unintended effect of starting up the JVM whenever debug
logging is enabled.

We should thus be careful to either (a) use _log_exception everywhere
but add case logic to avoid starting up the JVM early, or (b) use
_log_exception ONLY when providing a Java exception.
@gselzer gselzer requested a review from elevans July 21, 2025 17:38
@gselzer gselzer added the bug Something isn't working label Jul 21, 2025
Copy link
Member

@ctrueden ctrueden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, @gselzer. Indeed, _log_exception is in the _java area, and like you said, from the type annotations, clearly meant to be passed only Java exceptions. We should enforce this at runtime with an assert or type check with TypeError to cut down on future programming mistakes like this.

The only gray area is if you somehow have an exception that might be a Java exception, but might not? Are there any cases like that currently in the codebase?

@ctrueden ctrueden merged commit 40d1b24 into main Jul 21, 2025
6 checks passed
@ctrueden ctrueden deleted the use-log-exception-less branch July 21, 2025 20:42
@gselzer
Copy link
Contributor Author

gselzer commented Jul 21, 2025

We should enforce this at runtime with an assert or type check with TypeError to cut down on future programming mistakes like this.

The only gray area is if you somehow have an exception that might be a Java exception, but might not? Are there any cases like that currently in the codebase?

Yeah, I thought about this a little bit but not deeply. I suppose you could check if the JVM is already running (it couldn't be a Java Exception if the JVM hasn't started, right?), and if so whether it's a java exception? Does checking if it's a JPype JObject not start the JVM?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants