diff --git a/docs/changelog/+probe-close-fds.bugfix.rst b/docs/changelog/+probe-close-fds.bugfix.rst new file mode 100644 index 0000000..5a5123a --- /dev/null +++ b/docs/changelog/+probe-close-fds.bugfix.rst @@ -0,0 +1,2 @@ +The interpreter probe no longer hangs on macOS when the forked child blocks closing an inherited file descriptor +before exec. diff --git a/src/python_discovery/_cached_py_info.py b/src/python_discovery/_cached_py_info.py index 503b147..9070c89 100644 --- a/src/python_discovery/_cached_py_info.py +++ b/src/python_discovery/_cached_py_info.py @@ -231,6 +231,8 @@ def _run_subprocess( env=env, encoding="utf-8", errors="backslashreplace", + # on macOS the child's pre-exec close() loop can block forever; Python fds are non-inheritable anyway + close_fds=False, ) out, err, code = _collect_query_output(process, timeout) except OSError as os_error: diff --git a/tests/python_discovery/test_cached_py_info.py b/tests/python_discovery/test_cached_py_info.py index 550b953..c0e120c 100644 --- a/tests/python_discovery/test_cached_py_info.py +++ b/tests/python_discovery/test_cached_py_info.py @@ -85,6 +85,12 @@ def test_run_subprocess_success() -> None: assert isinstance(result, PythonInfo) +def test_run_subprocess_skips_child_fd_close_loop(mocker: MockerFixture) -> None: + popen: Final[MockType] = mocker.patch("python_discovery._cached_py_info.Popen", autospec=True, side_effect=Popen) + failure, result = _run_subprocess(PythonInfo, sys.executable, dict(os.environ)) + assert (failure, isinstance(result, PythonInfo), popen.call_args.kwargs["close_fds"]) == (None, True, False) + + def test_run_subprocess_bad_exe() -> None: failure, result = _run_subprocess(PythonInfo, "/nonexistent/python", dict(os.environ)) assert failure is not None