Fix ErrorContext ThreadLocal memory leak in lazy loading#3636
Merged
hazendaz merged 1 commit intomybatis:masterfrom Feb 15, 2026
Merged
Fix ErrorContext ThreadLocal memory leak in lazy loading#3636hazendaz merged 1 commit intomybatis:masterfrom
hazendaz merged 1 commit intomybatis:masterfrom
Conversation
When lazy loading is triggered through proxy method interception, ErrorContext was not being cleaned up, causing ThreadLocal memory leaks. Add ErrorContext.reset() in proxy finally blocks to prevent the leak. Fixes mybatisgh-1967 Signed-off-by: jisub-dev <[email protected]>
Member
|
@jisub-dev Thank you! I used AI as not super familiar with this area of code. Gist I got from that is that the normal locations that is reset were properly in try finally blocks but with proxies, those are long done due to the lazy loading so there is no finally to cleanup. Where you added it then is correct in all three spots as the two proxies both interact and override parts of that base class. Thanks for picking up such an old issue and taking care of it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes a ThreadLocal memory leak in ErrorContext when lazy loading is triggered through
proxy method interception.
Problem
When lazy loading is invoked via proxy objects (Javassist/CGLIB proxies or
deserialization proxies), the
ErrorContextpopulated during query execution was notbeing cleaned up from ThreadLocal. This caused memory leaks in thread pool environments
like Tomcat, as reported in #1967.
The leak occurred because:
BaseExecutor.query()which populatesErrorContext.instance()ErrorContext.reset()in their finally blocksSolution
Added
ErrorContext.instance().reset()calls in the finally blocks of:JavassistProxyFactory.EnhancedResultObjectProxyImpl.invoke()CglibProxyFactory.EnhancedResultObjectProxyImpl.intercept()AbstractEnhancedDeserializationProxy.invoke()Testing
ErrorContextTest)Closes #1967