Skip to content

Possible bug in LoggerContext.updateLoggers throwing ConcurrentModificationException #3551

@pmk75

Description

@pmk75

Description

LoggerContext.updateLoggers throws a ConcurrentModificationException randomly when called by javamelody filter initialisation during tomcat startup. Result is that tomcat is incorrectly started, process is there but does not serve any request

Configuration

Version: 2.24.2

Operating system: Ubuntu 24.04

JDK: amazon-corretto-17.0.10.8.1-linux-x64

Logs

19-Mar-2025 05:35:40.941 SEVERE [main] org.apache.catalina.core.StandardContext.filterStart Exception starting filter [javamelody]
	java.util.ConcurrentModificationException
		at java.base/java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1784)
		at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
		at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:276)
		at java.base/java.util.WeakHashMap$ValueSpliterator.forEachRemaining(WeakHashMap.java:1217)
		at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
		at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
		at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
		at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
		at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
		at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
		at org.apache.logging.log4j.core.LoggerContext.updateLoggers(LoggerContext.java:776)
		at org.apache.logging.log4j.core.LoggerContext.updateLoggers(LoggerContext.java:766)
		at net.bull.javamelody.Log4J2Appender.register(Log4J2Appender.java:86)
		at net.bull.javamelody.FilterContext.initLogs(FilterContext.java:333)
		at net.bull.javamelody.FilterContext.<init>(FilterContext.java:98)
		at net.bull.javamelody.MonitoringFilter.init(MonitoringFilter.java:148)
		at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:244)

Reproduction

Randomly appears at startup (every 5 to 10 starts)

Discussion

The related javamelody code is here

https://github.com/javamelody/javamelody/blob/6bb2b275b7dc77cded89ac661352094e10449e16/javamelody-core/src/main/java/net/bull/javamelody/Log4J2Appender.java#L86

And the exception is thrown here

loggerRegistry.getLoggers().forEach(logger -> logger.updateConfiguration(config));

May be it would be safer to wrap the loggerRegistry.getLoggers() collection, in a separate one such as

new ArrayList<>(loggerRegistry.getLoggers()).forEach(....

to avoid potential concurrent modification.
Not really sure of the cause and therefore the potential solution.

Did anybody else noticed this issue ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicatePull requests or issues with another instance elsewhere

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions