Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Fix: stream restoration order (#660)

Akshay Agrawal893c48c3950d
diff --git a/marimo/_runtime/redirect_streams.py b/marimo/_runtime/redirect_streams.py
index 4be70d2ebe..0cf7b298fc 100644
--- a/marimo/_runtime/redirect_streams.py
+++ b/marimo/_runtime/redirect_streams.py
@@ -66,10 +66,14 @@ def redirect_streams(
sys.stderr = stderr # type: ignore
sys.stdin = stdin # type: ignore
- with redirect(stdout, py_stdout.fileno()), redirect(
- stderr, py_stderr.fileno()
- ):
- yield
+ try:
+ with redirect(stdout, py_stdout.fileno()), redirect(
+ stderr, py_stderr.fileno()
+ ):
+ yield
+ finally:
+ # These have to be restored after the redirect context managers have
+ # exited
sys.stdout = py_stdout
sys.stderr = py_stderr
sys.stdin = py_stdin