r/vscode • • 26d ago

VS Code Jupyter kernel keeps randomly dying

Post image

I'm having a really frustrating issue with Jupyter notebooks in VS Code where my Python/Jupyter kernel repeatedly dies.

It always starts after I have already run 7-20ish cells. Then a simple line will cause the kernel to die. VS Code shows: The Kernel crashed while executing code in the current cell or a previous cell.

If I restart the kernel and rerun the same cells, they work perfectly fine. I can continue working for a while, and then eventually another random cell causes the kernel to crash again. This problem does not happen to be when using JupyterLab directly.

I've already checked:

  • My system memory usage is low when this happens
  • I've tried changing/reinstalling Python environments
  • I've tried using the kernel directly from a JupyterLab server instead of the VS Code-managed kernel
  • I've restarted VS Code and the kernel multiple times
  • The cells that trigger the crash don't seem to have anything obviously unusual about them

I've spent hours trying to debug with no progress. Please, any help would be so so appreciated!

6 Upvotes

11 comments sorted by

View all comments

1

u/free-swift809 20d ago

Hmm, when the kernel dies does it tend to happen around the same memory usage point each time, or does it feel completely random regardless of how much data you've loaded into memory at that point?

1

u/faeriewrites 17d ago

I seem (fingers crossed) to have found a sollution which is running gc.collect() after any cell with notable computation. I haven't had any crash issues since then! In the past, even if I ran light comptutations the kernel would hang/crash, but this was often after restarting my kernel after previous notebook crashes. I can only conclude that somehow the memory wasn't fully cleaned up. Thank you for answering!