Skip to content

Commit c7b01e3

Browse files
authored
[3.11] gh-90872: Fix subprocess.Popen.wait() for negative timeout (#116989) (#117003)
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 299b6d0 commit c7b01e3

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
@@ -1620,6 +1620,22 @@ def test__use_vfork(self, mock_fork_exec):
16201620
subprocess.run([sys.executable, "-c", "pass"])
16211621
self.assertFalse(mock_fork_exec.call_args_list[-1].args[-1])
16221622

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

16241640
class RunFuncTestCase(BaseTestCase):
16251641
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)