Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -52,6 +53,69 @@ void singleValue() {
singleValue(createThreadContextMap());
}

@Test
void removeLastKeyClearsThreadLocal() {
final DefaultThreadContextMap map = new DefaultThreadContextMap();
map.put("key", "value");
assertThat(map.getImmutableMapOrNull()).isNotNull();

map.remove("key");

assertTrue(map.isEmpty());
assertThat(map.getImmutableMapOrNull()).isNull();
}

@Test
void removeMultipleKeysClearsThreadLocal() {
final DefaultThreadContextMap map = createMap();

map.remove("key");

assertFalse(map.isEmpty());
assertEquals("value2", map.get("key2"));
assertThat(map.getImmutableMapOrNull()).isNotNull();

map.remove("key2");

assertTrue(map.isEmpty());
assertThat(map.getImmutableMapOrNull()).isNull();
}

@Test
void removeAllKeysClearsThreadLocal() {
final DefaultThreadContextMap map = createMap();

map.removeAll(Arrays.asList("key", "key2"));

assertTrue(map.isEmpty());
assertThat(map.getImmutableMapOrNull()).isNull();
}

@Test
void removeAllRetainsThreadLocalForRemainingKeys() {
final DefaultThreadContextMap map = createMap();

map.removeAll(Arrays.asList("key"));

assertFalse(map.isEmpty());
assertEquals("value2", map.get("key2"));
assertThat(map.getImmutableMapOrNull()).isNotNull();
}

@Test
void repeatedRemovalKeepsThreadLocalCleared() {
final DefaultThreadContextMap map = new DefaultThreadContextMap();
map.put("key", "value");

map.remove("key");
map.remove("key");
map.removeAll(Arrays.asList("key"));
map.removeAll(Arrays.asList("key"));

assertTrue(map.isEmpty());
assertThat(map.getImmutableMapOrNull()).isNull();
}

@Test
void testPutAll() {
final DefaultThreadContextMap map = new DefaultThreadContextMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.apache.logging.log4j.internal.map.UnmodifiableArrayBackedMap;
import org.apache.logging.log4j.util.BiConsumer;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.logging.log4j.util.ReadOnlyStringMap;
Expand Down Expand Up @@ -93,7 +94,12 @@ public String get(final String key) {
public void remove(final String key) {
final Object[] state = localState.get();
if (state != null) {
localState.set(getMap(state).copyAndRemove(key).getBackingArray());
final UnmodifiableArrayBackedMap map = getMap(state).copyAndRemove(key);
if (map.isEmpty()) {
localState.remove();
} else {
localState.set(map.getBackingArray());
}
}
}

Expand All @@ -103,7 +109,12 @@ public void remove(final String key) {
public void removeAll(final Iterable<String> keys) {
final Object[] state = localState.get();
if (state != null) {
localState.set(getMap(state).copyAndRemoveAll(keys).getBackingArray());
final UnmodifiableArrayBackedMap map = getMap(state).copyAndRemoveAll(keys);
if (map.isEmpty()) {
localState.remove();
} else {
localState.set(map.getBackingArray());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="2523" link="https://github.com/apache/logging-log4j2/issues/2523"/>
<description format="asciidoc">
Fix `DefaultThreadContextMap` retaining a `ThreadLocal` after its last entry is removed.
</description>
</entry>