microsoft / vscode-jupyter

VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
MIT License
1.3k stars 292 forks source link

Cell execution hangs if sys.stderr reassigned with wrapper #16009

Closed djoshea closed 1 month ago

djoshea commented 2 months ago

Applies To

What happened?

If I have the following in a .ipynb notebook opened in VS Code, and I run the following cell, the code executes (got here and got to end both print), but execution never completes, and I need to restart the kernel.

import sys

class _FilteredStderr:
  def __init__(self, stderr):
    self.stderr = stderr

  def write(self, data: str):
    if "_EquinoxRuntimeError" not in data:
      self.stderr.write(data)

stderr = sys.stderr
print("got here")
sys.stderr = _FilteredStderr(stderr) # commenting this out fixes the issue
print("got to end")

The key line is reassigning sys.stderr; all runs fine if I comment this out.

I discovered this in trying to figure out what was causing the Python debugger to hang when debugging a cell in Jupyter notebook, which was running jax / equinox code, that overwrites the stderr and causes havoc. If I step through this code in the debugger, the debugger also hangs.

VS Code Version

Version: 1.93.0 Commit: 4849ca9bdf9666755eb463db297b69e5385090e3 Date: 2024-09-04T13:02:38.431Z (2 days ago) Browser: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Code/1.93.0 Chrome/124.0.6367.243 Electron/30.4.0 Safari/537.36

Jupyter Extension Version

v2024.8.0

Jupyter logs

Visual Studio Code (1.93.0, undefined, desktop)
Jupyter Extension Version: 2024.8.0.
Python Extension Version: 2024.14.0.
Pylance Extension not installed.
Platform: darwin (arm64).
Temp Storage folder ~/Library/Application Support/Code/User/globalStorage/ms-toolsai.jupyter/version-2024.8.0
Workspace folder ~/omitted, Home = /Users/omitted
14:25:10.347 [info] Telemetry level is off
14:25:10.347 [info] Experiments are disabled, only manually opted experiments are active.
...
14:25:10.558 [info] Starting Kernel (Python Path: ~/omitted/.venv/bin/python, Venv, 3.12.3) for '~/omitted/test_debugpy_issue.ipynb' (disableUI=true)
14:25:11.973 [info] Kernel successfully started
14:25:11.975 [info] Process Execution: ~/omitted/.venv/bin/python /Users/~/.vscode/extensions/ms-toolsai.jupyter-2024.8.0-darwin-arm64/pythonFiles/printJupyterDataDir.py
14:25:12.039 [info] Kernel successfully started
14:25:13.865 [warn] Disposing old controller startUsingPythonInterpreter:'.jvsc74a57bd03fb703eb2f6653431b5f6d07f12fd2b9f2f8764e88f7a390ab6e686bcac66174./Users/~/.rye/shims/rye./Users/~/.rye/shims/rye.-m#ipykernel_launcher' for view = 'jupyter-notebook'
14:25:13.865 [warn] Disposing old controller startUsingPythonInterpreter:'.jvsc74a57bd03fb703eb2f6653431b5f6d07f12fd2b9f2f8764e88f7a390ab6e686bcac66174./Users/~/.rye/shims/rye./Users/~/.rye/shims/rye.-m#ipykernel_launcher (Interactive)' for view = 'interactive'

Coding Language and Runtime Version

Python 3.12 in a venv managed by Rye

Language Extension Version (if applicable)

Python v2024.14.0

Anaconda Version (if applicable)

No response

Running Jupyter locally or remotely?

Local

DonJayamanne commented 1 month ago

Given that we run into the exact same issue with Jupyter Lab/Notebook I'm closing this issue. Jupyter extension builds on top of the protocol implementation of Jupyter, as this issue exists outside of VS Code, I'm closing this.