fix: ensure Observation.Scope is closed even if ExecutionInfo loses its state #92
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a potential memory leak in
DataSourceObservationListenerthat can occur during query execution whenExecutionInfoloses its context (for example, due to connection eviction or forced closure).It addresses the issue reported in #91
Changes
Fallback logic in
stopQueryObservationIf the
Observation.Scopecannot be retrieved fromExecutionInfo, the listener now performs a defensive cleanup by ensuring that any active observation scope associated with the current thread is properly closed. This guarantees that the scope opened inbeforeQueryis always closed, preventingThreadLocalleaks.Null safety improvement
Added a null check for
QueryContextbefore callingsetAffectedRowCountto prevent a potentialNullPointerExceptionin edge cases where the context may be missing.Test case added
Added
memoryLeakWhenConnectionForciblyClosedAndExecutionInfoLosttoDataSourceObservationListenerTests.This test simulates a scenario where
ExecutionInfofails to return the stored scope and verifies that the fallback logic correctly cleans up the observation registry.Verification
Added a new unit test:
DataSourceObservationListenerTests#memoryLeakWhenConnectionForciblyClosedAndExecutionInfoLostVerified that all tests pass by running:
./mvnw test