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.

test patch to fix windows session tests in CI (#8515)

Shahmir Varqha699a36b705b9
diff --git a/tests/_server/test_asgi.py b/tests/_server/test_asgi.py
index 4406d9647c..6f862aebc4 100644
--- a/tests/_server/test_asgi.py
+++ b/tests/_server/test_asgi.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import os
+import sys
import tempfile
import unittest
from pathlib import Path
@@ -43,6 +44,11 @@ if __name__ == "__main__":
class TestASGIAppBuilder(unittest.TestCase):
def setUp(self) -> None:
+ # Kernel threads running in RUN mode may modify sys.modules["__main__"]
+ # via patch_main_module. Save it so tearDown can restore it and
+ # prevent contamination of later tests (e.g. multiprocessing.Process
+ # on Windows reads __main__.__file__ during spawn).
+ self._saved_main = sys.modules["__main__"]
# Create a temporary directory for the tests
self.temp_dir = tempfile.TemporaryDirectory()
self.app1 = os.path.join(self.temp_dir.name, "app1.py")
@@ -55,6 +61,8 @@ class TestASGIAppBuilder(unittest.TestCase):
def tearDown(self) -> None:
# Clean up the temporary directory
self.temp_dir.cleanup()
+ # Restore __main__ in case a kernel thread modified it
+ sys.modules["__main__"] = self._saved_main
def test_create_asgi_app(self) -> None:
builder = create_asgi_app(quiet=True, include_code=True)
diff --git a/tests/_server/test_session_manager.py b/tests/_server/test_session_manager.py
index 2f3149047f..1d67633410 100644
--- a/tests/_server/test_session_manager.py
+++ b/tests/_server/test_session_manager.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import sys
from textwrap import dedent
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, Mock
@@ -22,9 +23,26 @@ from marimo._session.notebook import AppFileManager
from marimo._types.ids import SessionId
if TYPE_CHECKING:
+ from collections.abc import Iterator
from pathlib import Path
[email protected](autouse=True)
+def _preserve_main_module() -> Iterator[None]:
+ """Restore sys.modules["__main__"] after each test.
+
+ Earlier tests (e.g. in test_asgi.py) may start kernel threads in RUN
+ mode that call patch_main_module(), permanently replacing __main__
+ with a module whose __file__ points to a now-deleted temp file.
+ On Windows the multiprocessing 'spawn' start method reads
+ __main__.__file__ to bootstrap the child process, so a stale path
+ causes FileNotFoundError and the parent hangs on listener.accept().
+ """
+ saved = sys.modules["__main__"]
+ yield
+ sys.modules["__main__"] = saved
+
+
@pytest.fixture
def mock_session_consumer():
mock = Mock(spec=SessionConsumer)