Skip to content

Fix shutdown hook timeout when starting with a configuration - #4520

Open
SebTardif wants to merge 1 commit into
apache:mainfrom
SebTardif:fix/shutdown-hook-timeout
Open

SebTardif wants to merge 1 commit into
apache:mainfrom
SebTardif:fix/shutdown-hook-timeout

Conversation

@SebTardif

Copy link
Copy Markdown
Contributor

Summary

LoggerContext.start(Configuration) registers the JVM shutdown hook before setConfiguration installs that configuration. The hook copied shutdownTimeoutMillis from the DefaultConfiguration still in place, which is 0. ExecutorServices.shutdown does not wait when the timeout is 0, so a configured shutdownTimeout was ignored on JVM exit.

start() with no arguments still calls reconfigure() first and then registers the hook, so that path already used the installed configuration. AsyncLoggerContext.start(Configuration) calls super.start(config) and picks up this change.

On 2.x, start(Configuration) calls reconfigure(config) before the hook is registered, so this regression is on main only. Introduced when setConfiguration(config) stayed after the hook registration (02560820ef, Ralph Goers, 2013) and the timeout snapshot was added in 705e27253c (Mikael Staldal, 2016). The Log4j 3 reorder never moved the snapshot onto the configuration argument.

Test plan

  • Red: LoggerContextShutdownHookTimeoutTest against unfixed log4j-core failed with expected: <5000> but was: <0>
  • Green: the same test passed after the fix
  • ./mvnw -pl log4j-core,log4j-core-test spotless:apply
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
./mvnw -pl log4j-core install -DskipTests
./mvnw -pl log4j-core-test test -Dtest=LoggerContextShutdownHookTimeoutTest -Dsurefire.failIfNoSpecifiedTests=false

LoggerContext.start(Configuration) registered the JVM shutdown hook
before setConfiguration installed that configuration. The hook copied
shutdownTimeoutMillis from the DefaultConfiguration still in place,
which is 0, so a configured shutdownTimeout did not reach stop.

Pass the configuration being started into setUpShutdownHook. The no-arg
start() still passes the configuration installed by reconfigure().

LoggerContextShutdownHookTimeoutTest failed on the unfixed core with
expected 5000 but was 0, and passed after the fix.
@ramanathan1504 ramanathan1504 added bug Incorrect, unexpected, or unintended behavior of existing code java Pull requests that update Java code labels Nov 24, 2026
@ramanathan1504 ramanathan1504 added this to the 3.0.0-beta4 milestone Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Incorrect, unexpected, or unintended behavior of existing code java Pull requests that update Java code

Projects

Development

Successfully merging this pull request may close these issues.

2 participants