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(tests): don't use pytest.exe as __main__.__file__ on Windows (#9265)

Akshay Agrawal506b7aae5745
diff --git a/tests/_messaging/test_streams.py b/tests/_messaging/test_streams.py
index c85f9b32ae..52198bac84 100644
--- a/tests/_messaging/test_streams.py
+++ b/tests/_messaging/test_streams.py
@@ -177,6 +177,12 @@ async def test_import_multiprocessing(
mocked_kernel: MockedKernel, exec_req: ExecReqProvider
) -> None:
# https://github.com/marimo-team/marimo/issues/684
+ #
+ # On Windows this also guards against the pytest-spawn interaction:
+ # __main__.__file__ must not point at the pytest.exe zipapp, or the
+ # Manager child's multiprocessing.spawn._fixup_main_from_path will
+ # runpy.run_path() pytest itself and the parent hangs on reader.recv().
+ # See the _fake_main_file fixture in tests/conftest.py.
await mocked_kernel.k.run(
[
exec_req.get(
diff --git a/tests/conftest.py b/tests/conftest.py
index f9ba93114c..9b13f522ea 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -86,13 +86,35 @@ def pytest_collection_modifyitems(
item.add_marker(pytest.mark.skip(reason=reason))
[email protected](scope="session")
+def _fake_main_file(tmp_path_factory: pytest.TempPathFactory) -> Path:
+ """A benign empty .py file to assign as __main__.__file__ in tests.
+
+ Why not reuse the real pytest launcher path? On Windows,
+ shutil.which("pytest") returns pytest.exe — a zipapp. If a cell
+ spawns a child process via multiprocessing.Manager() (or anything
+ using the "spawn" start method), the child runs
+ multiprocessing.spawn._fixup_main_from_path(__main__.__file__),
+ which calls runpy.run_path() on the parent's __file__. For a zipapp
+ pytest.exe that means pytest starts running inside the child
+ process, never writes the Manager's server address back, and the
+ parent hangs at reader.recv(). An empty .py file has no side
+ effects when runpy executes it.
+ """
+ path = tmp_path_factory.mktemp("marimo_test_main") / "pytest_main.py"
+ path.write_text("")
+ return path
+
+
@pytest.fixture(autouse=True)
-def _ensure_main_has_file() -> Generator[None, None, None]:
- """Ensure __main__.__file__ is set for pytest-xdist workers.
+def _ensure_main_has_file(
+ _fake_main_file: Path,
+) -> Generator[None, None, None]:
+ """Make sure main has a file ...
xdist workers don't always set __file__ on __main__, which causes
- marimo's kernel (create_main_module) to set __file__=None, breaking
- tests that rely on __file__ being a real path.
+ marimo's kernel (create_main_module) to set __file__=None,
+ breaking tests that rely on __file__ being a real path.
"""
main = sys.modules.get("__main__")
if main is None:
@@ -103,16 +125,7 @@ def _ensure_main_has_file() -> Generator[None, None, None]:
original_file = getattr(main, "__file__", None)
if not had_file_attr or original_file is None:
- # Find a valid pytest path: prefer shutil.which, fall back to
- # sys.argv[0], then construct one from sys.executable.
- pytest_path = shutil.which("pytest")
- if pytest_path and Path(pytest_path).exists():
- new_file = pytest_path
- elif sys.argv and sys.argv[0] and Path(sys.argv[0]).exists():
- new_file = sys.argv[0]
- else:
- new_file = str(Path(sys.executable).parent / "pytest")
- main.__file__ = new_file
+ main.__file__ = str(_fake_main_file)
try:
yield