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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
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.pyindex 4406d9647c..6f862aebc4 100644--- a/tests/_server/test_asgi.py+++ b/tests/_server/test_asgi.py@@ -1,6 +1,7 @@from __future__ import annotationsimport os+import sysimport tempfileimport unittestfrom 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 testsself.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 directoryself.temp_dir.cleanup()+ # Restore __main__ in case a kernel thread modified it+ sys.modules["__main__"] = self._saved_maindef 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.pyindex 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 sysfrom textwrap import dedentfrom typing import TYPE_CHECKINGfrom unittest.mock import MagicMock, Mock@@ -22,9 +23,26 @@ from marimo._session.notebook import AppFileManagerfrom marimo._types.ids import SessionIdif TYPE_CHECKING:+ from collections.abc import Iteratorfrom 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.fixturedef mock_session_consumer():mock = Mock(spec=SessionConsumer)