DaScheid opened a new issue, #4508: URL: https://github.com/apache/logging-log4j2/issues/4508
## Description With: 1. `-Dlog4j2.garbagefreeThreadContextMap=true` and 2. reused log events (`-Dlog4j2.enable.threadlocals=true` or async logging) and 3. an additional `ContextDataProvider` (e.g. instrumentation via OpenTelemetry agent) logging can fail with `ArrayIndexOutOfBoundsException` - which is catched by `AbstractLogger`, thereby "losing" single log events. Removing one of these three conditions avoids this scenario, but I want to keep all three at the same time. `SortedArrayStringMap::initFrom0` always copies `other.threshold`: https://github.com/apache/logging-log4j2/blob/140ff0af9c1b56268bf42af2e48503fe1a6ae99a/log4j-api/src/main/java/org/apache/logging/log4j/util/SortedArrayStringMap.java#L250 Log event context map is reused between events: 1. Event A has empty thread context. Provider adds 3 entries, so reused map arrays grow to length of 4. 2. Event B has 4 entries in thread context. `initFrom0` copies them into the reused map. Because 4 entries fit into existing length of 4 for the arrays, arrays are kept, but `threshold` is set to thread context map value of 16 (see `GarbageFreeSortedArrayThreadContextMap.DEFAULT_INITIAL_CAPACITY`). 4. Provider tries to add its entries. Map is full (4/4), but `putValue` only resizes when `size >= threshold` (4 < 16). So no resize, instead inserting past end of array -> throws exception. ## Configuration **Version:** 2.26.1 **Operating system:** macOS / Windows 11 **JDK:** OpenJDK 17.0.18 / 25.0.5 ## Logs ```bash 2026-11-20T10:22:59.020432300Z main WARN org.apache.logging.log4j.spi.AbstractLogger caught java.lang.ArrayIndexOutOfBoundsException logging ReusableSimpleMessage: Second java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 5 out of bounds for object array[4] at java.base/java.lang.System.arraycopy(Native Method) at org.apache.logging.log4j.util.SortedArrayStringMap.insertAt(SortedArrayStringMap.java:223) at org.apache.logging.log4j.util.SortedArrayStringMap.putValue(SortedArrayStringMap.java:217) at org.apache.logging.log4j.util.SortedArrayStringMap.lambda$static$0(SortedArrayStringMap.java:64) at org.apache.logging.log4j.core.impl.JdkMapAdapterStringMap.forEach(JdkMapAdapterStringMap.java:127) at org.apache.logging.log4j.util.SortedArrayStringMap.putAll(SortedArrayStringMap.java:248) at org.apache.logging.log4j.core.impl.ThreadContextDataInjector$ForGarbageFreeThreadContextMap.injectContextData(ThreadContextDataInjector.java:205) at org.apache.logging.log4j.core.impl.ReusableLogEventFactory.createEvent(ReusableLogEventFactory.java:117) at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:683) at org.apache.logging.log4j.core.config.DefaultReliabilityStrategy.log(DefaultReliabilityStrategy.java:73) at org.apache.logging.log4j.core.Logger.log(Logger.java:187) at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2970) at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2922) at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2904) at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2648) at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:2587) at org.apache.logging.log4j.spi.AbstractLogger.error(AbstractLogger.java:814) at Repro.main(Repro.java:19) ``` ## Reproduction Adding 4x files in same folder: - log4j-api-2.26.1.jar - log4j-core-2.26.1.jar - `META-INF/services/org.apache.logging.log4j.core.util.ContextDataProvider` containing `Repro$ExtraProvider` - `Repro.java`: ```java import java.util.Map; import org.apache.logging.log4j.CloseableThreadContext; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.util.ContextDataProvider; public class Repro { public static class ExtraProvider implements ContextDataProvider { @Override public Map<String, String> supplyContextData() { return Map.of("trace_id", "t", "span_id", "s", "trace_flags", "f"); } } public static void main(String[] args) { Logger logger = LogManager.getLogger(Repro.class); logger.error("First"); // empty thread context, reused map gets capacity 4 try (CloseableThreadContext.Instance ignored = CloseableThreadContext.put("a", "1").put("b", "2").put("c", "3").put("d", "4")) { logger.error("Second"); // throws } } } ``` Then compile + run (repro classes must come after log4j-core so ExtraProvider runs after ThreadContextDataProvider): ```bash javac -cp "log4j-api-2.26.1.jar;log4j-core-2.26.1.jar" Repro.java java -Dlog4j2.status.LoggerLevel=WARN -Dlog4j2.garbagefreeThreadContextMap=true -Dlog4j2.enable.threadlocals=true -cp "log4j-api-2.26.1.jar;log4j-core-2.26.1.jar;." Repro ``` (Hint: commands use Windows path separator `;`, replace with `:` for e.g. macOS) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
