Skip to content

Commit 27cf3ed

Browse files
authored
gh-90872: Fix subprocess.Popen.wait() for negative timeout (#116989)
On Windows, subprocess.Popen.wait() no longer calls WaitForSingleObject() with a negative timeout: pass 0 ms if the timeout is negative.
1 parent 408e127 commit 27cf3ed

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Lib/subprocess.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,8 @@ def _wait(self, timeout):
15861586
"""Internal implementation of wait() on Windows."""
15871587
if timeout is None:
15881588
timeout_millis = _winapi.INFINITE
1589+
elif timeout <= 0:
1590+
timeout_millis = 0
15891591
else:
15901592
timeout_millis = int(timeout * 1000)
15911593
if self.returncode is None:

Lib/test/test_subprocess.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,22 @@ def test_class_getitems(self):
16071607
self.assertIsInstance(subprocess.Popen[bytes], types.GenericAlias)
16081608
self.assertIsInstance(subprocess.CompletedProcess[str], types.GenericAlias)
16091609

1610+
@unittest.skipUnless(hasattr(subprocess, '_winapi'),
1611+
'need subprocess._winapi')
1612+
def test_wait_negative_timeout(self):
1613+
proc = subprocess.Popen(ZERO_RETURN_CMD)
1614+
with proc:
1615+
patch = mock.patch.object(
1616+
subprocess._winapi,
1617+
'WaitForSingleObject',
1618+
return_value=subprocess._winapi.WAIT_OBJECT_0)
1619+
with patch as mock_wait:
1620+
proc.wait(-1) # negative timeout
1621+
mock_wait.assert_called_once_with(proc._handle, 0)
1622+
proc.returncode = None
1623+
1624+
self.assertEqual(proc.wait(), 0)
1625+
16101626

16111627
class RunFuncTestCase(BaseTestCase):
16121628
def run_python(self, code, **kwargs):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
On Windows, :meth:`subprocess.Popen.wait` no longer calls
2+
``WaitForSingleObject()`` with a negative timeout: pass ``0`` ms if the
3+
timeout is negative. Patch by Victor Stinner.

0 commit comments

Comments
 (0)