Skip to content

Commit 35e8f04

Browse files
authored
[3.12] gh-90872: Fix subprocess.Popen.wait() for negative timeout (#116989) (#117002)
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. (cherry picked from commit 27cf3ed)
1 parent 23f9732 commit 35e8f04

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
@@ -1581,6 +1581,8 @@ def _wait(self, timeout):
15811581
"""Internal implementation of wait() on Windows."""
15821582
if timeout is None:
15831583
timeout_millis = _winapi.INFINITE
1584+
elif timeout <= 0:
1585+
timeout_millis = 0
15841586
else:
15851587
timeout_millis = int(timeout * 1000)
15861588
if self.returncode is None:

Lib/test/test_subprocess.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,22 @@ def test__use_vfork(self, mock_fork_exec):
16221622
subprocess.run([sys.executable, "-c", "pass"])
16231623
self.assertFalse(mock_fork_exec.call_args_list[-1].args[-1])
16241624

1625+
@unittest.skipUnless(hasattr(subprocess, '_winapi'),
1626+
'need subprocess._winapi')
1627+
def test_wait_negative_timeout(self):
1628+
proc = subprocess.Popen(ZERO_RETURN_CMD)
1629+
with proc:
1630+
patch = mock.patch.object(
1631+
subprocess._winapi,
1632+
'WaitForSingleObject',
1633+
return_value=subprocess._winapi.WAIT_OBJECT_0)
1634+
with patch as mock_wait:
1635+
proc.wait(-1) # negative timeout
1636+
mock_wait.assert_called_once_with(proc._handle, 0)
1637+
proc.returncode = None
1638+
1639+
self.assertEqual(proc.wait(), 0)
1640+
16251641

16261642
class RunFuncTestCase(BaseTestCase):
16271643
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)