apache / apache/logging-log4j2

Removing all keys from `ThreadContextMap` does not clear the `ThreadLocal`

Open
#2,523 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
3.6k
Forks
1.7k
Avg merge
21h 30m
Merged PRs (30d)
27

Description

The implementations of [`ThreadContextMap#remove`](https://logging.apache.org/log4j/2.x/javadoc/log4j-api/org/apache/logging/log4j/spi/ThreadContextMap#remove(java.lang.String)) and [`CleanableThreadContextMap#removeAll`](https://logging.apache.org/log4j/2.x/javadoc/log4j-api/org/apache/logging/log4j/spi/CleanableThreadContextMap#removeAll(java.lang.Iterable)) in the `DefaultThreadContextMap` and `CopyOnWriteSortedArrayThreadContextMap` implementations do not clear the underlying `ThreadLocal`, after all keys have been removed from the context map.

This issue prevents `CopyOnWriteSortedArrayThreadContextMap` to be safely used in a web application, if code like this is used:

```java
ThreadContext.put("key", "value");
try {
...
} finally {
ThreadContext.remove("key");
}
```

**Remark**: this issue does not affect the `GarbageFreeSortedArrayThreadContextMap` implementation, since leaving a non-null value of `ThreadContext` is part of its garbage-free design.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.