Skip to content

Commit 2bbcaed

Browse files
Zheaoligpshead
andauthored
gh-133089: Use original timeout value for TimeoutExpired when the func subprocess.run is called with a timeout (GH-133103)
Signed-off-by: Manjusaka <[email protected]> Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 51d2459 commit 2bbcaed

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

Doc/library/subprocess.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,24 @@ handling consistency are valid for these functions.
15251525
Notes
15261526
-----
15271527

1528+
.. _subprocess-timeout-behavior:
1529+
1530+
Timeout Behavior
1531+
^^^^^^^^^^^^^^^^
1532+
1533+
When using the ``timeout`` parameter in functions like :func:`run`,
1534+
:meth:`Popen.wait`, or :meth:`Popen.communicate`,
1535+
users should be aware of the following behaviors:
1536+
1537+
1. **Process Creation Delay**: The initial process creation itself cannot be interrupted
1538+
on many platform APIs. This means that even when specifying a timeout, you are not
1539+
guaranteed to see a timeout exception until at least after however long process
1540+
creation takes.
1541+
1542+
2. **Extremely Small Timeout Values**: Setting very small timeout values (such as a few
1543+
milliseconds) may result in almost immediate :exc:`TimeoutExpired` exceptions because
1544+
process creation and system scheduling inherently require time.
1545+
15281546
.. _converting-argument-sequence:
15291547

15301548
Converting an argument sequence to a string on Windows

Lib/subprocess.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,8 +1235,11 @@ def communicate(self, input=None, timeout=None):
12351235

12361236
finally:
12371237
self._communication_started = True
1238-
1239-
sts = self.wait(timeout=self._remaining_time(endtime))
1238+
try:
1239+
sts = self.wait(timeout=self._remaining_time(endtime))
1240+
except TimeoutExpired as exc:
1241+
exc.timeout = timeout
1242+
raise
12401243

12411244
return (stdout, stderr)
12421245

@@ -2145,8 +2148,11 @@ def _communicate(self, input, endtime, orig_timeout):
21452148
selector.unregister(key.fileobj)
21462149
key.fileobj.close()
21472150
self._fileobj2output[key.fileobj].append(data)
2148-
2149-
self.wait(timeout=self._remaining_time(endtime))
2151+
try:
2152+
self.wait(timeout=self._remaining_time(endtime))
2153+
except TimeoutExpired as exc:
2154+
exc.timeout = orig_timeout
2155+
raise
21502156

21512157
# All data exchanged. Translate lists into strings.
21522158
if stdout is not None:

Lib/test/test_subprocess.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ def test_call_timeout(self):
162162
[sys.executable, "-c", "while True: pass"],
163163
timeout=0.1)
164164

165+
def test_timeout_exception(self):
166+
try:
167+
subprocess.run(['echo', 'hi'], timeout = -1)
168+
except subprocess.TimeoutExpired as e:
169+
self.assertIn("-1 seconds", str(e))
170+
else:
171+
self.fail("Expected TimeoutExpired exception not raised")
172+
try:
173+
subprocess.run(['echo', 'hi'], timeout = 0)
174+
except subprocess.TimeoutExpired as e:
175+
self.assertIn("0 seconds", str(e))
176+
else:
177+
self.fail("Expected TimeoutExpired exception not raised")
178+
165179
def test_check_call_zero(self):
166180
# check_call() function with zero return code
167181
rc = subprocess.check_call(ZERO_RETURN_CMD)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Use original timeout value for :exc:`subprocess.TimeoutExpired`
2+
when the func :meth:`subprocess.run` is called with a timeout
3+
instead of sometimes a confusing partial remaining time out value
4+
used internally on the final ``wait()``.

0 commit comments

Comments
 (0)