Repository navigation
fix: clear empty DefaultThreadContextMap ThreadLocal state - #4524
Open
abu-tahir-0 wants to merge 1 commit into
Open
abu-tahir-0 wants to merge 1 commit into
abu-tahir-0 wants to merge 1 commit into
Conversation
Removing the last key or all keys currently retains an empty backing array in the ThreadLocal. Remove the ThreadLocal entry instead and cover last-key, multi-key, and repeated removal paths.
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.
Problem
DefaultThreadContextMap.remove()andremoveAll()retain an empty backing array in aThreadLocalafter the final entry is removed. Long-lived threads keep that state alive unnecessarily.Root cause
Both methods unconditionally replaced the
ThreadLocalvalue with the copied map's backing array, even when the resulting immutable map was empty.Solution
Remove the
ThreadLocalentry when the resulting map is empty; otherwise retain the non-empty backing array. Add tests for single-key, multi-key,removeAll, partial removal, and repeated removal, plus the 2.x changelog entry.Testing
JAVA_HOME=<Java 17> ./mvnw -pl log4j-api-test -am -Dtest=DefaultThreadContextMapTest -Dsurefire.failIfNoSpecifiedTests=false test— 20/20 passed, independently rerun.ThreadContextTest14 passed; garbage-free map tests 9 passed in four executions.git diff --checkpassed.log4j-api-testreactor run was also attempted: 961 tests ran, with 15 failures/errors confined to untouched stack-location, service-loader, and status-listener tests under the Windows/classes-directory setup (including the existing multi-release-JAR warning). The focused ThreadContext suite remains green. Fullverify sitewas not run.Fixes #2523