Add shutdown hook
Is your feature request related to a problem? Please describe.
I'm unable to register a callback to perform custom cleanup when the application exits. Specifically, I want to gracefully terminate long-running non-daemon threads started by the application. See topic: https://discourse.holoviz.org/t/shutdown-hook-in-panel/8672/5
Describe the solution you'd like
I suggest to implement a pn.state.on_exit method to register a callback that will be invoked when the application is shut down. A possible implementation is sketched in the linked topic.
Describe alternatives you've considered
An alternative is to use daemon threads, as suggested in the note at the end of the Manual Threading guide, but this is not always desirable because daemon threads are abruptly stopped at shutdown.
Additional context
https://discourse.holoviz.org/t/shutdown-hook-in-panel/8672/5
This issue has been mentioned on HoloViz Discourse. There might be relevant details there:
https://discourse.holoviz.org/t/shutdown-hook-in-panel/8672/7