Skip to content

Commit 7ca6766

Browse files
committed
✨ feat(interpreter): increase query timeout to 15s with environment override
The 5s timeout was too aggressive for slower Windows environments where Python startup can exceed this duration. Increase the default to 15s to accommodate real-world conditions while still preventing infinite hangs. Users experiencing timeouts can further customize the timeout via the PY_DISCOVERY_TIMEOUT environment variable for their specific environment.
1 parent 7d30958 commit 7ca6766

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/python_discovery/_cached_py_info.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def _run_subprocess(
189189
) -> tuple[Exception | None, PythonInfo | None]:
190190
start_cookie = gen_cookie()
191191
end_cookie = gen_cookie()
192+
timeout = float(env.get("PY_DISCOVERY_TIMEOUT", "15"))
192193
with _resolve_py_info_script() as py_info_script:
193194
cmd = [exe, str(py_info_script), start_cookie, end_cookie]
194195
env = dict(env)
@@ -206,7 +207,7 @@ def _run_subprocess(
206207
encoding="utf-8",
207208
errors="backslashreplace",
208209
)
209-
out, err = process.communicate(timeout=5)
210+
out, err = process.communicate(timeout=timeout)
210211
code = process.returncode
211212
except TimeoutExpired:
212213
process.kill()

tests/test_cached_py_info.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ def test_run_subprocess_timeout(mocker: MockerFixture) -> None:
123123
assert mock_process.communicate.call_count == 2
124124

125125

126+
def test_run_subprocess_custom_timeout(mocker: MockerFixture) -> None:
127+
mock_process = MagicMock()
128+
mock_process.communicate.return_value = (json.dumps(PythonInfo().to_dict()), "")
129+
mock_process.returncode = 0
130+
mocker.patch("python_discovery._cached_py_info.Popen", return_value=mock_process)
131+
env = dict(os.environ)
132+
env["PY_DISCOVERY_TIMEOUT"] = "30"
133+
_run_subprocess(PythonInfo, sys.executable, env)
134+
mock_process.communicate.assert_called_once_with(timeout=30.0)
135+
136+
126137
def test_run_subprocess_nonzero_exit(mocker: MockerFixture) -> None:
127138
mock_process = MagicMock()
128139
mock_process.communicate.return_value = ("some output", "some error")

0 commit comments

Comments
 (0)