Fix data race on LoggerContext.size (#1038) - #1055
Open
seonwooj0810 wants to merge 1 commit into
Open
Conversation
Loggers are created in getLogger(String) under the monitor of their parent node, so concurrent creations below distinct parents run under different locks. incSize() then performed an unsynchronized size++ on the shared counter, so increments were lost under contention and size() could report fewer loggers than actually exist (reported via ThreadSanitizer). Make the counter an AtomicInteger so it is updated atomically regardless of which parent monitor is held. Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>
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.
Fixes #1038
Root cause
LoggerContext.getLogger(String)creates each new logger under the monitor of its parent node (synchronized (logger)). Concurrent callers creating loggers below different parents therefore hold different locks, yet they all update the same counter viaincSize(), which performed an unsynchronizedsize++. That read-modify-write races, so increments are lost under contention andsize()reports fewer loggers than actually exist. This is exactly the ThreadSanitizer data race reported in the issue.Change
Make the counter an
AtomicIntegerand useincrementAndGet()/get(), so the count is maintained atomically regardless of which parent monitor a thread happens to hold. No locking is added to the hot path.Test evidence
Added
LoggerContextTest#concurrentGetLoggerKeepsSizeConsistent: 16 threads each create 500 loggers under their own distinct parent (so theirincSize()calls hold distinct monitors, maximising contention), then the test assertssize()matches the real logger count.expected: <8017> but was: <7970>(lost increments).LoggerContextTest(17 tests) is green.Verification done: built and ran
mvn -pl logback-classic -am -Dtest=LoggerContextTest teston the branch (all pass), and confirmed the new test fails on unpatchedmaster.