# Fix VS Code Jupyter Notebook Kernel Disconnected From Session

You are working in a Jupyter Notebook inside VS Code, and suddenly all cells show a "Not Connected" status. The kernel indicator in the top right shows "Disconnected" or "Starting" indefinitely. Output from previously running cells is lost, and new cells cannot be executed.

Step 1: Check the Kernel Status

Look at the kernel indicator in the top-right corner of the notebook editor. It shows:

  • Python 3.11.x -- Kernel is connected
  • Starting... -- Kernel is launching
  • Disconnected -- Connection lost
  • Select Kernel -- No kernel selected

Click on the kernel indicator to see available kernels and the connection status.

Step 2: Check the Jupyter Output Panel

Open the Jupyter output panel:

bash
View > Output > Select "Jupyter" from dropdown

This shows the complete kernel lifecycle:

bash
Info: Starting kernel python311-jvsc74a57bd9-...
Info: Kernel started: abc123
Error: Kernel connection timed out after 60 seconds
Error: Kernel process exited with code 1

Common errors: - Timeout: Kernel took too long to start - Process exited: Python process crashed - Connection lost: Network or IPC issue

Step 3: Kernel Process Crashed

If the kernel process crashed, check the Python environment:

```bash # Check if Python is working /path/to/venv/bin/python --version

# Check if ipykernel is installed /path/to/venv/bin/python -m ipykernel --version

# If not installed /path/to/venv/bin/python -m pip install ipykernel ```

The Jupyter kernel requires the ipykernel package. Without it, the kernel cannot start.

Step 4: Reconnect the Kernel

Try reconnecting without restarting:

  1. 1.Click the kernel indicator in the top right
  2. 2.Select the same kernel again
  3. 3.VS Code attempts to reconnect

If that fails, restart the kernel:

bash
Jupyter: Restart Kernel

From the Command Palette. This kills the current kernel process and starts a new one.

Step 5: Memory Exhaustion

The most common cause of kernel crashes is memory exhaustion. If a cell loads a large dataset:

python
import pandas as pd
df = pd.read_csv('huge_dataset.csv')  # 4GB file on a 8GB machine

The kernel process exceeds available memory and gets killed by the OS.

Check system memory:

bash
free -h

If memory is critically low, the OOM killer terminates the kernel:

bash
sudo dmesg -T | grep -i "killed process" | grep python

Fix by: - Using chunked reading: pd.read_csv('file.csv', chunksize=10000) - Increasing system memory or swap - Using a more memory-efficient library (Dask, Polars)

Step 6: Python Environment Changed

If you updated Python, changed virtual environments, or removed packages, the kernel may no longer work:

bash
# Check if the kernel's Python still exists
cat ~/.local/share/jupyter/kernels/python3/kernel.json
# Shows the Python executable path

If the executable no longer exists, reinstall ipykernel:

bash
/path/to/venv/bin/python -m ipykernel install --user --name=my-venv --display-name="Python (my-venv)"

Then select the newly registered kernel in VS Code.

Step 7: Jupyter Extension Issues

The VS Code Jupyter extension may need an update or restart:

bash
Extensions > Search: Jupyter

Check for updates. If the extension was recently updated, reload the window:

bash
Developer: Reload Window

Step 8: Clear Notebook Output

Corrupted cell output can cause kernel connection issues. Clear all output:

bash
Jupyter: Clear All Outputs

Then restart the kernel. If the connection stabilizes, a specific cell's output was causing the problem.

Step 9: Disable Pre-warming

VS Code pre-warms kernels to speed up notebook opening. This can cause connection issues:

json
{
    "jupyter.disableJupyterAutoStart": true
}

With this setting, kernels only start when you explicitly run a cell. This is slower but more reliable.

Step 10: Remote Kernel Connection

For remote Jupyter kernels:

json
{
    "jupyter.jupyterServerType": "remote",
    "jupyter.remoteJupyterServers": [
        {
            "baseUrl": "http://remote-server:8888/",
            "token": "your-token-here"
        }
    ]
}

If the remote server is unreachable, the kernel shows as disconnected. Verify:

bash
curl http://remote-server:8888/api/kernels

If this fails, the remote Jupyter server is down or the network is unreachable.