Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 of 2 tasks
djoshea opened this issue Sep 6, 2024 · 1 comment
Closed
1 of 2 tasks

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

djoshea opened this issue Sep 6, 2024 · 1 comment
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug notebook-kernel Kernels issues (start/restart/switch/execution, install ipykernel) user-setup Issues caused by incorrectly configured python environments, packages, or the like

Comments

@djoshea
Copy link

djoshea commented Sep 6, 2024

Applies To

  • Notebooks (.ipynb files)
  • Interactive Window and/or Cell Scripts (.py files with #%% markers)

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

@djoshea djoshea added the bug Issue identified by VS Code Team member as probable bug label Sep 6, 2024
@djoshea djoshea changed the title Cell execution hangs if sys.stderr assigned to wrapper Cell execution hangs if sys.stderr reassigned with wrapper Sep 9, 2024
@DonJayamanne
Copy link
Contributor

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.

@DonJayamanne DonJayamanne added notebook-kernel Kernels issues (start/restart/switch/execution, install ipykernel) user-setup Issues caused by incorrectly configured python environments, packages, or the like labels Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug notebook-kernel Kernels issues (start/restart/switch/execution, install ipykernel) user-setup Issues caused by incorrectly configured python environments, packages, or the like
Projects
None yet
Development

No branches or pull requests

2 participants