-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expunge stale entries in InternalLoggerRegistry #3681
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
Conversation
…so added InternalLoggerRegistryGCTest
Use ReferenceQueue to track and remove GC'd Logger WeakReferences from the registry map to prevent potential memory leaks. Includes improved unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jhl221123, thank you for picking up this pull request!
The main code looks good to me overall. I do have a few suggestions regarding the unit tests:
...test/src/main/java/org/apache/logging/log4j/core/test/util/InternalLoggerRegistryGCTest.java
Outdated
Show resolved
Hide resolved
...test/src/main/java/org/apache/logging/log4j/core/test/util/InternalLoggerRegistryGCTest.java
Outdated
Show resolved
Hide resolved
...test/src/main/java/org/apache/logging/log4j/core/test/util/InternalLoggerRegistryGCTest.java
Outdated
Show resolved
Hide resolved
* Adjusted test class file name and its package location. * Used MockLogger in logger suppliers to avoid nested computeIfAbsent. * Employed a local SimpleMessageFactory instance in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look solid—nice work! There's just one final task before we can wrap this up: could you please add a changelog entry under src/changelog/.2.x.x
, as outlined in the changelog documentation?
Also, just a quick note on loggerRefByNameByMessageFactory
: since it's a WeakHashMap
, it doesn’t follow all the typical Map
invariants. From what I can tell, you're using it safely, and I didn’t spot any issues that would lead to unexpected behavior. That said, given the sensitivity of this part of the codebase, I’d like a second pair of eyes on it.
@vy, could you take a moment to review this as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements a mechanism to expunge stale logger entries in InternalLoggerRegistry by tracking WeakReferences using a ReferenceQueue. Key changes include:
- Adding a ReferenceQueue field and an expungeStaleEntries() method.
- Updating logger retrieval and creation methods to trigger stale entry cleanup.
- Introducing unit tests to verify the expunging behavior under various GC and concurrent scenarios.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java | Added ReferenceQueue and expungeStaleEntries() method, plus calls to it in getLogger/hasLogger/computeIfAbsent |
log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistryTest.java | Added unit tests to cover stale entry removal and concurrent access scenarios |
...j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java
Show resolved
Hide resolved
I would like to and I will. Please allow me some time.
Op vr 23 mei 2025 om 21:27 schreef Piotr P. Karwasz <
***@***.***>
… ***@***.**** approved this pull request.
The changes look solid—nice work! There's just one final task before we
can wrap this up: could you please add a changelog entry under
src/changelog/.2.x.x, as outlined in the changelog documentation
<https://logging.apache.org/log4j/tools/log4j-changelog.html#changelog-entry-file>
?
Also, just a quick note on loggerRefByNameByMessageFactory: since it's a
WeakHashMap, it doesn’t follow all the typical Map invariants. From what
I can tell, you're using it safely, and I didn’t spot any issues that would
lead to unexpected behavior. That said, given the sensitivity of this part
of the codebase, I’d like a second pair of eyes on it.
@vy <https://github.com/vy>, could you take a moment to review this as
well?
—
Reply to this email directly, view it on GitHub
<#3681 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAARTSIM67PBK326RDUMPTD275ZDDAVCNFSM6AAAAAB5TRLZL6VHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDQNRVGQ4TQNJRGY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
...j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java
Outdated
Show resolved
Hide resolved
...j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java
Outdated
Show resolved
Hide resolved
...j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java
Outdated
Show resolved
Hide resolved
...j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java
Outdated
Show resolved
Hide resolved
...j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java
Outdated
Show resolved
Hide resolved
...st/src/test/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistryTest.java
Show resolved
Hide resolved
...st/src/test/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistryTest.java
Outdated
Show resolved
Hide resolved
...st/src/test/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistryTest.java
Outdated
Show resolved
Hide resolved
...st/src/test/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistryTest.java
Outdated
Show resolved
Hide resolved
...st/src/test/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistryTest.java
Outdated
Show resolved
Hide resolved
* Rename variables in InternalLoggerRegistry * Add final keyword in InternalLoggerRegistry and tests * Remove/clarify comments in InternalLoggerRegistry * Remove MockLogger class, using anonymous Logger instances in tests * Remove System.runFinalization() calls from tests * Improve assertion logic in testExpungeStaleWeakReferenceEntries * Remove testConcurrentAccess (lacked specific scenario)
This pull request addresses issue #3430 by implementing a mechanism to expunge stale entries in
InternalLoggerRegistry
.It builds upon PR #3474 and incorporates feedback from @vy and @ppkarwasz.
Key changes include:
ReferenceQueue
: When aLogger
is GC'd, itsWeakReference
is enqueued. Subsequent calls to methods likegetLogger()
orcomputeIfAbsent()
triggerexpungeStaleEntries()
to process it.expungeStaleEntries
logic: If theReferenceQueue
is not empty, a write lock is acquired, the queue is drained, and map entries are scanned once to removeWeakReferences
with null referents and clean up any empty maps. No action or locking occurs if the queue is initially empty.@vy,
ReferenceQueue
has nopeek()
/isEmpty()
, so I couldn’t do a proper double-check and used an initialpoll()
instead. Feedback is welcome if there's a better way or if I missed something. Thanks!Fixes #3430