vscode-jupyter: VSCode leaves several running processes after exit

Issue Type: Bug

Steps to reproduce:

  1. Launch vscode.
  2. Open or create a Jupyter Notebook.
  3. Do some work on it.
  4. Close vscode.
  5. Optional: Rinse and repeat.

Expected behavior:

Any processes started by vscode are closed.

Actual behavior:

After launching and closing vscode a few times (and realizing I am starting to run out of memory) I notice this:

user@laptop:~$ ps aux | grep -v networkd-dispatcher | grep -v unattended | grep -v grep | grep python
user   344513  0.2  0.6 4115828 102040 ?      Sl   May19   3:19 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   345452  0.4  5.3 5162548 858400 ?      Sl   May19   5:46 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   345607  0.2  0.6 4443632 102484 ?      Sl   May19   3:01 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   353204  0.2  0.6 3788220 105468 ?      Sl   May19   1:55 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   353605  0.0  0.2 117072 46508 ?        S    May19   0:01 /usr/bin/python3 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.jupyter_daemon -v
user   353771  0.3  0.6 3796468 105576 ?      Sl   May19   2:29 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   353933  0.2  0.2 3722708 39000 ?       Sl   May19   1:31 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   354840  0.2  0.2 3796424 35728 ?       Sl   00:35   1:34 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   355331  0.9  5.4 6179224 869260 ?      Sl   00:39   6:06 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   356104  0.3  0.6 3862044 111216 ?      Sl   00:52   1:52 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   366116  1.6  0.4 3796252 76032 ?       Sl   10:27   0:35 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   366803  2.3  0.4 1007044 69596 ?       Sl   10:32   0:44 /usr/bin/python3 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.jupyter_daemon -v
user   366808 10.1  5.4 5509256 871684 ?      Sl   10:32   3:16 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/languageServer.0.5.50/Microsoft.Python.LanguageServer
user   366831  0.0  0.2 551048 35328 ?        Ssl  10:32   0:01 /usr/bin/python3 -m ipykernel_launcher -f /home/user/.local/share/jupyter/runtime/kernel-33b1ca6c-b09c-41dd-b2fc-c466b2511581.json
user   366855  0.0  0.2 550640 35408 ?        Ssl  10:32   0:01 /usr/bin/python3 -m ipykernel_launcher -f /home/user/.local/share/jupyter/runtime/kernel-a5a7d55e-0ebc-4b4b-aac4-4d1dddff8450.json
user   368190  7.6  1.2 2471816 200372 ?      Ssl  10:45   1:27 /usr/bin/python3 -m ipykernel_launcher -f /home/user/.local/share/jupyter/runtime/kernel-b00685be-189e-4f07-80e0-237ab6772827.json
user   368212  0.0  0.2 550648 43728 ?        Ssl  10:45   0:00 /usr/bin/python3 -m ipykernel_launcher -f /home/user/.local/share/jupyter/runtime/kernel-918a11e0-f81e-4740-a3c1-e5d5ca074570.json
user   369055  4.4  1.3 2076996 219232 pts/0  Sl+  10:57   0:19 /usr/bin/python3 /home/user/.local/bin/ipython
user   369168  2.0  0.3 709300 62112 ?        Sl   10:58   0:08 /usr/bin/python3 /home/user/.vscode-oss/extensions/ms-python.python-2020.5.80290/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.jupyter_daemon -v
user@laptop:~$ 

Extension version: 2020.5.80290 VS Code version: Code - OSS 1.46.0 (3984852b33bd6897dd83108ac305c567d49e8fda, 2020-05-15T14:46:12.527Z) OS version: Linux x64 5.4.0-29-generic

System Info
Item Value
CPUs Intel® Core™ i7-1065G7 CPU @ 1.30GHz (8 x 1137)
GPU Status 2d_canvas: enabled
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: disabled_off
protected_video_decode: unavailable_off
rasterization: disabled_software
skia_renderer: disabled_off_ok
video_decode: unavailable_off
viz_display_compositor: enabled_on
viz_hit_test_surface_layer: disabled_off_ok
webgl: enabled
webgl2: enabled
Load (avg) 1, 2, 2
Memory (System) 15.23GB (0.93GB free)
Process Argv –no-sandbox --unity-launch
Screen Reader no
VM 0%
DESKTOP_SESSION ubuntu
XDG_CURRENT_DESKTOP Unity
XDG_SESSION_DESKTOP ubuntu
XDG_SESSION_TYPE x11

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 11
  • Comments: 54 (34 by maintainers)

Most upvoted comments

Perhaps if we change the daemon to start as a process group, it should kill the child process automatically.

The root cause of this bug is the daemon’s we spin up to make kernel’s launch faster.

I believe you can workaround the problem by eliminating these files here (copy them elsewhere first in case that breaks something else):

<extension dir>/pythonFiles/lib/python/pyls_jsonrpc

Downside is longer startup times for kernels. But that’s relative. On some windows machines this can cost you 3-5 seconds for startup. On a linux box, it’s a lot less time.

@greazer this was never fixed. I’ve observed it few weeks ago. Please reopen.

I can confirm this behavior of leaving a stale python instance of -m ipykernel_launcher still happens with current versions of vscode on an up to date Ubuntu 20.04 OS:

code --version
1.59.0
379476f0e13988d90fab105c5c19e7abc8b1dea8
x64

And

$ code --list-extensions --show-versions | grep jupyter
ms-toolsai.jupyter@2021.8.1236758218

By the way code -s is useful to see the ipykernel_launcher instances started. Close vscode, and the python ipykernel_launcher PIDs get orphaned and picked up/owned by the systemd user session pid because vscode failed to reap those processes when closing.

To find details of stuff left behind/not cleaned up by vscode:

ps -f -p $(pgrep -f ipykernel_launcher) | more

To see exactly which IPython orphans have ended up owned by the user init instance of systemd:

ps -f -p $(pgrep -P $(pgrep -u $UID systemd) -f ipykernel_launcher) | more

Note pgrep only matches immediate child with -P; no recursion down to deeper child process of running vscode instances.

To kill/cleanup just the orphaned IPython instances (and reclaim your RAM!):

pkill -P $(pgrep -u $UID systemd) -f ipykernel_launcher

Old bug with no new comments for nearly a year. Closing.

Next week sometime it will be in stable. Pre-release is certainly less stable. It ships every night after running our tests.

You need the latest jupyter prerelease extension. As of me writing this comment it would be:

image

And disabling python daemons definitely fixes the problem.

Oh no wait, I can repro if I close VS code

I am experiencing a similar problem. Here are my specs

Version: 1.61.2
Date: 2021-10-19T14:58:13.605Z
Electron: 13.5.1
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Linux x64 5.14.14-200.fc34.x86_64

and I am using Fedora 34.

When I run a .ipynb code, I see these instances

before and after closing the VSCode

after

So the VSCode and its related things are being closed in couple of seconds however the python instance still runs.

Details of the python process details

Another major problem is that, another ’ python’ process appears after closing the VSCode and when I run another/same .ipynb file. For example, I run a .ipynb code, closed the VSCode, and rerun the same code. After doing this 4 times, I am seeing this.

rerun

As you can see, there are 4 .ipykernel_launcer instances, and each of them is taking about 130 MB RAM, which I believe is a lot. I think this is a severe problem.

root cause of this bug is the daemon’s we spin up to make kernel’s launch faster.

Thanks @rchiodo

So far, removing pyls_jsonrpc seems to work:

mkdir /tmp/ipy_buggy_launch
mv ~/.vscode/extensions/ms-toolsai.jupyter-2021.8.1236758218/pythonFiles/lib/python/pyls_jsonrpc /tmp/ipy_buggy_launch

Since doing this, haven’t yet reproduced orphaned IPython instances after vscode closes.

Same issue here, two users SSHing into a GCP ubuntu server. Closing VSCode will leave many suspended processes for python language server and the ipykernel image

Both users are running the official version vscode downloaded from code.visualstudio.com

Well, this is pretty rad… So, I’ve tried the official version of vscode and it was correctly cleaning up all the processes as you said.

Next, I went ahead and completely nuked all the directories associated with vscode-oss that I could find. IIRC they were: ~/.vscode-oss ~/.cache/Microsoft and ~/.config/Code - OSS.

Then, launched vscode-oss, reinstalled the Python extension and now it seems to work fine… nothing is left behind… 😕

Now, the interesting part is that the ~/.cache/Microsoft dir is no longer being created. Maybe some bad juju inside it was causing this?..

@rchiodo if you don’t mind, I’d like to keep this issue open for a few more days while I do some more intensive testing. I will come back here and close if all is good.

Thanks. This may be unavoidable depending upon the machine. VS code gives all extensions 5 seconds to finish shutdown. If we don’t get called to close our processes before that 5 seconds is up, they’ll stay running.

We’ll investigate though. We might not be closing the processes in the first place.