-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Properly handle exceptions in threads created by MongoClient #1764
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
base: main
Are you sure you want to change the base?
Conversation
Thanks @abalanonline for the PR. I've added JAVA-5913 to track this. It'll be triaged in due course. Ross |
Hi @abalanonline, thank you for the PR.
@abalanonline, @rozza please take a look, but hide whitespaces when reviewing to see the meaningful changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, catching the error, logging and rethrowing it will work. I can give verbal approval.
But since you asked for my opinion, I would kindly suggest removing all Error catching from the code and letting the JVM handle them. Alternatively, the user of the client library can handle such errors themselves if they are confident enough.
The only behavioral difference between my approach and the approach you propose is logging. The issue with not logging is that when an internal |
I agree. Logging the error seems to be the best option. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR addresses a critical bug where catching Throwable
in background threads could prevent the JVM from properly handling fatal errors like OutOfMemoryError
, leaving the application in a zombie state. The fix ensures fatal errors can propagate to terminate the JVM while still handling recoverable exceptions appropriately.
- Replaces broad
Throwable
catches with more specificException
catches in thread monitoring loops - Adds proper exception handling with logging and re-throwing for background worker threads
- Improves error messages to provide more context about which component stopped working
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
MongoClient.java | Adds exception handling to cursor cleaning thread with logging and re-throwing |
TlsChannelStreamFactoryFactory.java | Adds exception handling to TLS selector loop with logging and re-throwing |
PowerOfTwoBufferPool.java | Adds exception handling to buffer pool pruning thread with logging and re-throwing |
LoadBalancedCluster.java | Adds exception handling to wait queue handler thread with logging and re-throwing |
DefaultServerMonitor.java | Narrows exception handling from Throwable to Exception in monitoring loops, adds exception handling to main monitor thread |
DefaultDnsSrvRecordMonitor.java | Adds exception handling to DNS SRV record monitoring thread with logging and re-throwing |
DefaultConnectionPool.java | Updates error message to include object context |
BaseCluster.java | Adds exception handling to wait queue handler thread with logging and re-throwing |
AsynchronousClusterEventListener.java | Adds exception handling to event publishing thread with logging and re-throwing |
driver-core/src/main/com/mongodb/internal/connection/DefaultServerMonitor.java
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <[email protected]>
…rverMonitor.java Co-authored-by: Copilot <[email protected]>
Hello dear mongo-java-driver maintainers,
Please allow me to contribute this bugfix. The problem with Throwable in this line is that
it catches errors (OutOfMemoryError in particular) and doesn't let JVM crash leaving it
in zombie state - unable to recover, unable to exit.
JAVA-5913