I am trying to use pn.state.on_session_destroyed to clean up. And i received an error of “RuntimeError: Could not add session destroyed callback since no document to attach it to could be found.”
Do we have any example about how to the on_session_destroyed?
I’ve made a PR to describe the required callback signature. Please take a look @CongTang and let me know if this improvement would have solved your issue up front.
Just noticed an issue(not too sure if it is a mechanism or a bug).
The pn.state.on_session_destroyed is only working with the --atuoreload , otherwise it will end up with
<bokeh.server.contexts.BokehSessionContext object at 0x0000025CA72F3BE0>
2023-03-26 18:41:37,316 DocumentLifeCycleHandler on_session_destroyed callback <function user_logout at 0x0000025CA72EBA30> failed with following error: name 'pn' is not defined
pn.state.on_session_destroyed callback also doesn’t work when you use lifecycle hooks in a separate file (even though it’s in the state’s callbacks). However, the on_session_destroyed callback does get called from within the app_hooks.py file
Extra FYI:
The session unloading seems to happen more aggressively when using --num-threads
This is actually a nice bug (feature? ) as you can get around memory leaks. Even better if you periodically restart your cluster workers to release memory back to the OS:
Haven’t had issues with memory leaks since.
Ah, btw - this also makes watcher callbacks fail. Can only let them fail silently as sessions are being unloaded.