Skip to content

Conversation

tristantarrant
Copy link

@tristantarrant tristantarrant commented Jan 31, 2025

This PR includes and supersedes #3418
It fixes the issue of dangling WeakReferences, which are not being replaced causing excessive write lock acquisition by introducing a (very limited) WeakValuesHashMap to store the name -> logger mappings.

I've tested this successfully with Infinispan's core testsuite.

I've kept @ppkarwasz 's original commit and mine separate, but they should really be squashed together.

Closes #3399

ppkarwasz and others added 2 commits January 31, 2025 08:05
It has been reported that holding a lock on `InternalLoggerRegistry` during the creation of a logger can cause performance loss and deadlocks. The logger constructor can trigger property lookups and other pluggable operations, we don't entirely control. The fix to apache#3252 only covered one of these cases.

This change moves the instantiation of new `Logger`s outside the write lock. While in some cases, this will cause multiple instantiations of loggers with the same parameters, all such loggers are functionally equivalent. On the other hand, the change allows the creation of different loggers in parallel.

Closes apache#3399
@tristantarrant
Copy link
Author

Some additional info: here are the timings for running Infinispan's core testsuite on my laptop:

  • Log4j 2.23.1: 5m50s
  • Log4j 2.24.4-SNAPSHOT including this PR: 5m45s

Any difference is just attributable to noise.

@SuppressWarnings("unchecked")
private void processQueue() {
for (WeakValue<V> v = (WeakValue<V>) queue.poll(); v != null; v = (WeakValue<V>) queue.poll()) {
map.remove(v.getKey());
Copy link
Member

@vy vy Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tristantarrant, methods accessed while holding a read-lock (e.g., getLogger()) might end up mutating the map, which is not thread-safe. Am I mistaken?

@vy vy deleted the branch apache:2.24.x September 6, 2025 15:21
@vy vy closed this Sep 6, 2025
@github-project-automation github-project-automation bot moved this from To triage to Done in Log4j bug tracker Sep 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants