reconnecting to an existing kernel also creates a new kernel #5862
Labels
bug
Issue identified by VS Code Team member as probable bug
notebook-kernel
Kernels issues (start/restart/switch/execution, install ipykernel)
Summary
vscode interacts badly with existing (external to vscode) jupyter servers, spawning and never cleaning up numerous unwanted kernels. There are also various other incidental bugs that also pop up related to the kernel picker and existing jupyter servers, noted below.
Context: In order to work around #1378 and #1654, I start up my own jupyter server rather than letting vscode start one up. This way, reconnecting to a remote session (or having to reload the vscode window, etc.) doesn't terminate and restart all the running kernels every open notebook.
Specifically, there is feature (mentioned in the above bug reports) whereby if you are running your own jupyter server, then vscode remembers which kernel ID a particular notebook was connected to, and attempts to connect to that kernel when re-opening the notebook (or reloading the UI etc).
However, every time vscode tries to reconnect to a kernel of an existing jupyter server, it also spawns a new kernel. This occurs whether the the server is on localhost or is "local" to a remote-ssh session (i.e. running local to the machine that vscode is connected to over remote-ssh); I have not tested an actual remote server. The vscode insider build has the same problem.
Environment data
Version: 1.56.1 (Universal)
Commit: e713fe9b05fc24facbec8f34fb1017133858842b
Date: 2021-05-06T10:08:24.325Z
Electron: 12.0.4
Chrome: 89.0.4389.114
Node.js: 14.16.0
V8: 8.9.255.24-electron.0
OS: Darwin x64 20.4.0
Expected behaviour
(Re)connecting to an existing kernel will not spawn new kernels.
Actual behaviour
(Re)connecting to an existing kernel spawns a new kernel for each connection attempt.
This can be easily seen from the "Running Notebooks" list on the server's webpage. These kernels are invisible to the vscode kernel picker list until the window is reloaded.
Steps to reproduce:
jupyter notebook
from a terminal, and note the server URL.jupyter: create new blank notebook
from the command palette. (Opening an existing notebook from the filesystem works too.)jupyter: specify local or remote jupyter server for connections
from the command palette). Choose "existing" and then paste in the server URL. Allow the window to reload when prompted.jupyter: select kernel
or click on the python name next to the server name at the top of the vscode window) and note that it shows two kernels -- one with a long ID, and one unlabeled. The long ID matches the first kernel listed in the jupyter server page, and choosing the unlabeled one will is supposed to start a new kernel. Note further that the second kernel on the server page is not visible in the vscode picker.Make it possible to distinguish between the different kernels. Simplest is to type e.g.
foo = 5
into one cell, run it, and typeprint(foo)
into the next cell. If you reconnect to the same kernel, then running this cell will output 5. If you connect to a new kernel in whichfoo
hasn't yet been defined, then running only that second cell will produce an error.[Optional; exposes a different bug] Try to create a new kernel by choosing the unlabeled kernel from the vscode picker. At this point you're not actually in a new kernel: running the
print(foo)
cell yields no error. This is unexpected; it should have created a new kernel (and in fact this same action will create a new kernel, so long as you run the next step below (of reconnecting to the current kernel) first.Choose the existing kernel from the vscode kernel picker (top line). Again verify that running the
print(foo)
cell yields no error. Examining the running list in the jupyter server webpage shows that the second (unused) kernel from above has been shut down and a new (unused) one started.print(foo)
cell yields no error. Now a third, new kernel has been spawned, visible to the jupyter server webpage but still invisible to vscode. Repeating this step will spawn a new kernel each time.print(foo)
cell yields the expected error. Observe that two additional kernels are now running in the jupyter server webpage. The first time you summon the vscode kernel picker after this, the new kernel is not visible (another bug!), but dismissing it and re-opening the kernel picker finally now shows two kernels to choose among, and correctly indicates that the newer one has a single active connection (the notebook that is open in vscode).Notes
I think this is related to #4442, or maybe even the same, but it's hard to tell because there's not a lot of detail. In any case, there are at least three different incorrect behaviors illustrated by the steps above. First is the spawning of new kernels that are invisible to and unkillable from vscode. Second is the fact that you can't intentionally create a new kernel from vscode without at least once selecting the existing kernel in the kernel picker first (step 7 vs. step 9 should not have different results). Third is the fact that once you do manage to create a new kernel, the picker list doesn't update to show it until the second time it is displayed.
There's also a minor feature-request in that it is very difficult to determine which kernel is actively connected to the open notebook from the kernel picker.
Also, note that the steps above are using a new, blank, un-saved notebook. If you use an existing notebook that you open from the filesystem, the same thing happens. Likewise, the basic gist of the bug is identical when using today's insider build, though the kernel-picker UI is harder to get to.
The text was updated successfully, but these errors were encountered: