Detect Kernel Restart inside extension

I am trying to detect a kernel restart inside an Extension to trigger an action in the Extension.

The top-level application object provides a reference to the service manager for session from which I can listen to changes in session: app.serviceManager.sessions.runningChanged

But this does not help to reliably detect a kernel restart. Would appreciate any pointers towards identifying change in kernel state to ‘restarting’. Thanks.

Jupyterlab version: 2.x

1 Like

Would be interested in hearing your method of detecting kernel restarts as I’ve been trying to do the same thing myself

There is some recent discussion about kernel hooks (Kernel actions hooks · Issue #10501 · jupyterlab/jupyterlab · GitHub) and implementation of restart commands (Implemented Restart and debug by JohanMabille · Pull Request #10462 · jupyterlab/jupyterlab · GitHub) which appears related.

Currently debugger already reacts to kernel restarts (in a way), but it seems to be listening on kernelChanged instead (see handler.ts). I wonder if this is emitted on restarts too? It would be good to experiment if you are interested in this (I don’t have time right now and cannot guarantee it will succeed but if you really need that it might be worth a try).

1 Like

thanks! If I have some time this weekend I’ll take a look at debugger/how it handles restarts. I’m guessing I’ll need to pip install xeus-python notebook to get the debugger working properly?

I found this line in sessioncontext which looks like it produces a signal when restarting the kernel, but not quite sure how to watch for that signal to change.

Additionally - and not sure if this is relevant to your use case @Nishchay - is there a way to check if a notebook has finished executing cells? I’m interested in having a dialog box show up after restarting and running all cells, which I’m realizing is slightly trickier than just checking whether a notebook has restarted… :sweat_smile:

ipykernel now supports the debugger, so no need to switch to xeus-python.

You can find an example of how to listen to the restarting event in the debugger package