From 00bc254fbd271387f9e6e8e6c660879d8e3af3b9 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Mon, 23 Oct 2023 15:11:55 +0200 Subject: [PATCH] Update kernel env to reflect changes in session. This rely on jupyter/jupyter_client#987 and fixes ipython/ipykernel#1102 When a user rename the notebook in the UI, it will update the kernel env, so that the ``__session__`` variable reflect the new name on the next restart. It _does_ seem that each rename of a notebook in JupyterLab creates 4 update_session (2 with name, and 2 with path), but that does not seem to be a problem in this repository. --- jupyter_server/services/sessions/sessionmanager.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/jupyter_server/services/sessions/sessionmanager.py b/jupyter_server/services/sessions/sessionmanager.py index ba55cc2836..398490a8d4 100644 --- a/jupyter_server/services/sessions/sessionmanager.py +++ b/jupyter_server/services/sessions/sessionmanager.py @@ -461,6 +461,13 @@ async def update_session(self, session_id, **kwargs): query = "UPDATE session SET %s WHERE session_id=?" % (", ".join(sets)) # noqa self.cursor.execute(query, [*list(kwargs.values()), session_id]) + if hasattr(self.kernel_manager, "update_env"): + self.cursor.execute( + "SELECT path, name, kernel_id FROM session WHERE session_id=?", [session_id] + ) + path, name, kernel_id = self.cursor.fetchone() + self.kernel_manager.update_env(kernel_id=kernel_id, env=self.get_kernel_env(path, name)) + async def kernel_culled(self, kernel_id: str) -> bool: """Checks if the kernel is still considered alive and returns true if its not found.""" return kernel_id not in self.kernel_manager