-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Broken logger initialization in 2.24.1 #3143
Comments
Full reproducer: https://gist.github.com/kelunik/be51e5e2fdf746bbc86383b922bbb05e |
Thank you for the report. The |
We hit a similar issues at Apache POI, there it happens reproducibly if log4j-core 2.24.0 and log4j-api 2.24.1 are used. Reproducer: Run "./gradlew runWriteFile -PpoiVersion=5.4.0" in a checkout of https://github.com/centic9/poi-reproduce This version-mismatch happens if a library like Apache POI upgrades "api" to the latest version, but projects using it do not upgrade log4j-core along the way. This is likely a common situation.
Edit: I verified with current 2.25.0-SNAPSHOT and this problem still reproduces, so I reported #3196 to discuss this as a separate issue. |
@ppkarwasz when the version 2.25 will be released? And is 2.23 devoid of this bug? |
As far as I couuld reproduce, it will not be fixed by current 2.25.0-SNAPSHOT, therefore I created bug #3196 for the issue with mixed "api" and "core" versions. |
Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes #3143.
Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes #3143.
Thanks for the change @ppkarwasz when can we expect the release for the same. |
I am planning a As always, you are invited to review PRs, run release candidates in your tests and vote on releases. A motivated |
Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes apache#3143.
) This is a port of #3199 to the 2.24.x branch. Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes #3143. Co-authored-by: Volkan Yazıcı <[email protected]>
Description
LoggerContext / LoggerRegistry don't play well together in using weak references, which can result in a
null
logger if the GC kicks in at a bad timing.Configuration
Version: 2.24.1
Operating system: Linux / macOS
JDK: 21.0.4
Logs
Reproduction
LoggerContext
keeps a local reference tonewLogger
, but this isn't safe to keep a reference (see below).LoggerRegistry.putIfAbsent
takes the instance and creates aWeakReference
from it.getLogger
call returningnull
https://docs.oracle.com/javase/specs/jls/se21/html/jls-12.html#jls-12.6.1
Fix
#2936 should fix this, but is currently unreleased.
Reference.reachabilityFence
would also fix this, but is JDK 9+ only.See also: https://shipilev.net/jvm/anatomy-quarks/8-local-var-reachability/
The text was updated successfully, but these errors were encountered: