Delay shutdown of logging facilities until the application has completed its shutdown process #310
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #219.
Steps to reproduce
java -jar war/target/jenkins.war
.Expected results
Note: These are the actual results with this PR.
The application shutdown process prints the following logs:
Actual results
The application shutdown process does not print any logs:
Evaluation
As described in JDK-8161253, there is no way to control the order of execution of shutdown hooks. When the
LogManager#Cleaner
shutdown hook runs before Winstone's shutdown hook, it shuts down logging facilities. Logging facilities are subsequently unavailable to the application shutdown process, which is invoked from Winstone's shutdown hook.Solution
In the comments to JDK-8161253, Jason Mehrens suggests a workaround: create a custom log handler and install it on the root logger before all other log handlers. Since the first action of
LogManager#Cleaner
is to close all the installed log handlers on the root logger, it will invoke the custom log handler'sclose()
method. At this point, the custom log handler has intercepted control and can delay shutdown of logging facilities until the application has completed its shutdown process.Implementation
Added a 5-second sleep to ensure that we don't wait forever if the shutdown process is not making progress.
Testing disconnection
Ran the steps to reproduce before and after this change. Before this change, I could reproduce this problem. After this change, I could not.
Also ran this change against core with an artificial modification to add a 30-second sleep to the application shutdown process. Verified that we stopped getting logs after 5 seconds, ensuring that the timeout logic was working.