Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mkurz
Copy link
Member

@mkurz mkurz commented Nov 10, 2022

This reverts the changes from #8407 / #8277.
I am sure we don't want logback to shut down the logger context by using<shutdownHook /> (anymore), because Play already handlest that (at least since v2.7). Actually with <shutdownHook /> in place, a Play app closes the logger context to early and logs are lost (!)...

So what does <shutdownHook /> actually do?
It registers a JVM shutdown hoook. So when the JVM shuts down, this hook will automatically close the logger context after a delay... By default that delay is 0, so it will do that immediately. So we end up here and then here.

That might was desirable back when #8407 was merged (but not sure if back then this also made sense), but meanwhile we switched to Akka's Coordinated Shutdown hooks in #8406 (see this line). So now when akka-http or netty shutdown, they also stop the logger context, see here, which calls this, which then shuts down the logger context.

So for production mode, that means, that when you add <shutdownHook /> to your logback config, what happens is, that when you signal your application to shutdown, at first the JVM shutdown hook registered by logback runs and shuts down the logger context. Only later the Akka Coordinated Shutdown hook kicks in (but then the context is closed already).
The bad thing about that is that when logbacks hooks runs, a Play app didn't finish it's shutdown yet, so if there are log statements (which there are), they will be lost.
You can easily reproduce that:

$ sbt new playframework/play-scala-seed.g8
...
Template applied in ./play-scala-seed

$ cd play-scala-seed/

$ sbt "playUpdateSecret; stage" # stage the app for production
...

$ ./target/universal/stage/bin/play-scala-seed # Run the production app 
...
2022-11-10 20:56:03 INFO  play.api.Play  Application started (Prod) (no global state)
2022-11-10 20:56:04 INFO  play.core.server.AkkaHttpServer  Listening for HTTP on /127.0.0.1:9000

### AFTER THE APP STARTED STOP IT WITH CTRL+C
### NOW WE WILL SEE FOLLOWING LOG STATEMENTS:

2022-11-10 20:56:06 INFO  p.a.i.l.c.CoordinatedShutdownSupport  Starting synchronous coordinated shutdown with ServerStoppedReason reason and 2147508000 milliseconds timeout
2022-11-10 20:56:06 INFO  play.core.server.AkkaHttpServer  Stopping Akka HTTP server...
2022-11-10 20:56:06 INFO  play.core.server.AkkaHttpServer  Terminating server binding for /127.0.0.1:9000
2022-11-10 20:56:06 INFO  play.core.server.AkkaHttpServer  Running provided shutdown stop hooks

$ vim conf/logback.xml # Let's add <shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook" />

$ sbt stage # stage the app for productin again with updated logback config
...

$ ./target/universal/stage/bin/play-scala-seed # again run it in prod mode
...
2022-11-10 20:56:42 INFO  play.api.Play  Application started (Prod) (no global state)
2022-11-10 20:56:42 INFO  play.core.server.AkkaHttpServer  Listening for HTTP on /127.0.0.1:9000

### AFTER THE APP STARTED STOP IT WITH CTRL+C
### NO MORE LOG STATEMENTS! BECAUSE THE LOGBACK STOP HOOK ALREADY CLOSED THE LOG CONTEXT WAY TOO EARLY

$ 

I also debugged this behaviour thoroughly: I am 100% sure both stop hooks always call the stop method on the same object, both calls ending up this method (It does not fail when close is called twice, it does not care).

So I am 100%, in a Play app, <shutdownHook /> should never bet set, because it will alway be closed by the backend server.
(for dev mode that's a different story, see #10939, but unrelated to <shutdownHook />, which isn't set in Play's default dev logback config)

@mkurz mkurz added this to the 2.8.19 milestone Nov 10, 2022
@mkurz mkurz merged commit b48f7f7 into playframework:2.8.x Nov 11, 2022
@mkurz mkurz deleted the rm_shutdownHook branch November 11, 2022 07:39
@mkurz mkurz changed the title Do not use/recommend shutdownHook in Logback, Play handles that [2.8.x] Do not use/recommend shutdownHook in Logback, Play handles that Nov 11, 2022
@mkurz
Copy link
Member Author

mkurz commented Nov 11, 2022

@Mergifyio backport main

@mergify
Copy link
Contributor

mergify bot commented Nov 11, 2022

backport main

✅ Backports have been created

Details

mergify bot added a commit that referenced this pull request Nov 11, 2022
[main] Do not use/recommend shutdownHook in Logback, Play handles that (backport #11532) by @mkurz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant