-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-20002 Fix connection leak on concurrent session close #11468
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
if this is a way to detect that there is concurrent access to the session going on, then the correct response to that would be to blow up with a big error telling the programmer they are doing something very wrong. |
Or can it actually also occur in a different way? |
|
I actually don't understand what behavior is being claimed here. If Please don't tell me that you are:
Is that the scenario here? |
|
Thanks for the questions — let me clarify the scenario. This is not about catching or swallowing exceptions from Session.close(). The concurrent scenario being tested is not intentional unsynchronized access In this case, an exception during resource cleanup can currently prevent the |
|
You are receiving an error notifying you that there is a very severe programming error in your code, and that you must fix your code. Cleaning up a leaked connection is the absolute least of your problems here. |
|
I'm not saying we can't do the connection closing in a But the fact that you're concerned about a leaked connection in a scenario where you're using Hibernate in a completely broken and illegal way is worrying me a lot more. |
|
I would much rather that you run out of connections and are forced to fix your broken session handling than that we "fix" this and you take that as permission to leave your program in its current broken state. |
|
Understood. Thanks for the clarification — that makes sense. |
Understood, agreed. One minor observability thought only: would a WARN when releaseResources() fails help make this failure mode more explicit, without changing behavior? |
I believe our dogma is that "log and then rethrow" is an antipattern. You report the problem one way or the other, not both. |
| CompletableFuture<Void> asyncTask = CompletableFuture.runAsync(() -> { | ||
| try { | ||
| Thread.sleep(ASYNC_DELAY_MS); | ||
| String childName = parent.getChild().getName(); | ||
| } | ||
| catch (Exception ignored) { | ||
| } | ||
| }, asyncExecutor); |
Check notice
Code scanning / CodeQL
Unread local variable Note test
| CompletableFuture<Void> asyncTask = CompletableFuture.runAsync(() -> { | ||
| try { | ||
| Thread.sleep(ASYNC_DELAY_MS); | ||
| String childName = parent.getChild().getName(); |
Check notice
Code scanning / CodeQL
Unread local variable Note test
This PR fixes a physical connection leak that occurs in OSIV-like scenarios when concurrent access to a session
causes an exception during session close.
Problem
In OSIV (Open Session In View) environments, when async threads access lazy-loaded proxies while the main thread
closes the session, a
ConcurrentModificationExceptioncan occur inLogicalConnectionManagedImpl.close().When the exception happens during
releaseResources(), the method exits early and never callsreleaseConnectionIfNeeded(), leaving the physical JDBC connection unreleased in the pool, eventually causingconnection pool exhaustion.
Solution
Wrap
releaseResources()in a try-catch block to ensurereleaseConnectionIfNeeded()is always executed, evenwhen concurrent modification exceptions occur.
Changes
LogicalConnectionManagedImpl.close()to handle exceptions during resource releaseLogicalConnectionLoggingfor connection release failuresConcurrentCloseConnectionLeakTestto reproduce the OSIV-like concurrent scenario and verify the fixTesting
The new test simulates an OSIV-like scenario with:
ConcurrentModificationExceptionThe test verifies:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.
https://hibernate.atlassian.net/browse/HHH-20002