Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
288e4db
Use pidfd_open() and save the FD as an attribute
giampaolo Jan 18, 2026
6e494ca
Create _busy_wait() and move code in there
giampaolo Jan 18, 2026
aaf193c
Move existing code in new _blocking_wait() method
giampaolo Jan 18, 2026
7cbb0ad
Use _wait_pidfd()
giampaolo Jan 18, 2026
d9760c3
Use pidfd_open() not at class level but at method level
giampaolo Jan 18, 2026
5fd8eec
Add kqueue() implementation for macOS and BSD
giampaolo Jan 18, 2026
100b111
Add docstrings
giampaolo Jan 18, 2026
74ac2f4
Be conservative and check for specific error codes
giampaolo Jan 18, 2026
fc0cfd6
Document possible failures
giampaolo Jan 18, 2026
f282459
Add missing import
giampaolo Jan 18, 2026
6125976
Write test for pidfd_open() failing
giampaolo Jan 18, 2026
41dc6c0
Write test case for pidfd_open() / kqueue failing. Assert fallback is…
giampaolo Jan 18, 2026
73ba380
Write test case for kqueue failing. Assert fallback is used.
giampaolo Jan 18, 2026
3c8e603
Move tests in their own class
giampaolo Jan 18, 2026
932ae58
Add test for terminated PID race
giampaolo Jan 18, 2026
bb5080a
Add test_kqueue_race()
giampaolo Jan 19, 2026
f067073
Add test_kqueue_control_error
giampaolo Jan 19, 2026
4d96c2f
Add docstring
giampaolo Jan 19, 2026
fe05acc
Guard against possible slow test
giampaolo Jan 19, 2026
adb444e
Timeout: use math.ceil to avoid truncation
giampaolo Jan 19, 2026
4ec17c1
Remove unused exception var
giampaolo Jan 19, 2026
e807ba9
Timeout: use math.ceil to avoid truncation
giampaolo Jan 19, 2026
1ddc52b
Replace _can_use_kqueue() -> _CAN_USE_KQUEUE
giampaolo Jan 19, 2026
645ef6c
Shorten code
giampaolo Jan 19, 2026
6ee771b
Use waitpid() + WNOHANG even if process exited to avoid rare race
giampaolo Jan 19, 2026
61c6b99
Revert prev change
giampaolo Jan 19, 2026
527646d
Use ceil(timeout) to avoid truncation
giampaolo Jan 19, 2026
0a8a1b2
Remove check for timeout < 0 + rm test case which didn't make sense.
giampaolo Jan 19, 2026
73b97dc
Don't use _blocking_wait() as it has a while loop
giampaolo Jan 19, 2026
2d3c3f7
Add docstring
giampaolo Jan 19, 2026
4eac42f
Rm _busy_wait()
giampaolo Jan 19, 2026
3c156a9
Add comment
giampaolo Jan 19, 2026
dac7d3b
Add assert
giampaolo Jan 19, 2026
5c7ec2f
Update comments
giampaolo Jan 19, 2026
5c29144
Add test for timeout=0
giampaolo Jan 19, 2026
4359b07
Update comment about PID reuse race
giampaolo Jan 19, 2026
81275c8
Update comment
giampaolo Jan 19, 2026
43b500f
Handle rare case where poll() says we're done, but waitpid() doesn't
giampaolo Jan 19, 2026
b64e42b
Update Doc/library/subprocess.rst
giampaolo Jan 19, 2026
a101406
Add news entry
giampaolo Jan 19, 2026
452f8c4
Add entry in Doc/whatsnew/3.15.rst
giampaolo Jan 19, 2026
b0c9890
Fix typo
giampaolo Jan 19, 2026
27b7c9f
Re-wording
giampaolo Jan 19, 2026
e1da996
Raise on timeout < 0 and re-add test case
giampaolo Jan 19, 2026
5c78acc
Check if can really use can_use_pidfd() in unit tests
giampaolo Jan 19, 2026
df0538a
Check if can really use kqueue() in unit tests
giampaolo Jan 19, 2026
6d8e36c
Pre-emptively check whether to use the fast way methods
giampaolo Jan 19, 2026
6ba7465
Add test_fast_path_avoid_busy_loop
giampaolo Jan 19, 2026
e3c7977
Update comments
giampaolo Jan 19, 2026
97cc3be
Fix missing import on Windows
giampaolo Jan 19, 2026
c4342e3
Merge branch 'main' into subprocess-fast-wait
giampaolo Jan 19, 2026
5d78d24
Try to fix doc build error
giampaolo Jan 19, 2026
a916da3
Try to fix doc build error 2
giampaolo Jan 19, 2026
86200bd
Try to fix doc build error 3
giampaolo Jan 19, 2026
85c38bc
Try to fix doc build error 4
giampaolo Jan 19, 2026
3c92c1d
Try to fix doc build error 5
giampaolo Jan 19, 2026
33c8b1f
Minor rewordings
giampaolo Jan 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Doc/library/subprocess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -803,14 +803,29 @@ Instances of the :class:`Popen` class have the following methods:

.. note::

When the ``timeout`` parameter is not ``None``, then (on POSIX) the
function is implemented using a busy loop (non-blocking call and short
sleeps). Use the :mod:`asyncio` module for an asynchronous wait: see
When ``timeout`` is not ``None`` and the platform supports it, an
efficient event-driven mechanism is used to wait for process termination:

- Linux ≥= 5.3 uses :func:`os.pidfd_open` + :func:`select.poll`
- macOS and other BSD variants use :func:`select.kqueue` +
``KQ_FILTER_PROC`` + ``KQ_NOTE_EXIT``
- Windows uses ``WaitForSingleObject``

If none of these mechanisms are available, the function falls back to a
busy loop (non-blocking call and short sleeps).

.. note::

Use the :mod:`asyncio` module for an asynchronous wait: see
:class:`asyncio.create_subprocess_exec`.

.. versionchanged:: 3.3
*timeout* was added.

.. versionchanged:: 3.15
if *timeout* is not ``None``, use efficient event-driven implementation
on Linux >= 5.3 and macOS / BSD.

.. method:: Popen.communicate(input=None, timeout=None)

Interact with process: Send data to stdin. Read data from stdout and stderr,
Expand Down
14 changes: 14 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,20 @@ ssl

(Contributed by Ron Frederick in :gh:`138252`.)

subprocess
----------

* :meth:`subprocess.Popen.wait`: when ``timeout`` is not ``None`` and the
platform supports it, an efficient event-driven mechanism is used to wait for
process termination:

- Linux ≥= 5.3 uses :func:`os.pidfd_open` + :func:`select.poll`
- macOS and other BSD variants use :func:`select.kqueue` + ``KQ_FILTER_PROC`` + ``KQ_NOTE_EXIT``
- Windows keeps using ``WaitForSingleObject`` (unchanged)

If none of these mechanisms are available, the function falls back to the
traditional busy loop (non-blocking call and short sleeps).
(Contributed by Giampaolo Rodola in :gh:`83069`).

sys
---
Expand Down
151 changes: 148 additions & 3 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import threading
import warnings
import contextlib
import math
import select
from time import monotonic as _time
import types

Expand Down Expand Up @@ -121,7 +123,6 @@ class _del_safe:
WNOHANG = None
ECHILD = errno.ECHILD

import select
import selectors


Expand Down Expand Up @@ -748,6 +749,63 @@ def _use_posix_spawn():
return False


def _can_use_pidfd_open():
# Availability: Linux >= 5.3
if not hasattr(os, "pidfd_open"):
return False
try:
pidfd = os.pidfd_open(os.getpid(), 0)
except OSError as err:
if err.errno in {errno.EMFILE, errno.ENFILE}:
# transitory 'too many open files'
return True
# likely blocked by security policy like SECCOMP (EPERM,
# EACCES, ENOSYS)
return False
else:
os.close(pidfd)
return True


def _can_use_kqueue():
# Availability: macOS, BSD
if not all(
hasattr(select, x)
for x in (
"kqueue",
"KQ_EV_ADD",
"KQ_EV_ONESHOT",
"KQ_FILTER_PROC",
"KQ_NOTE_EXIT",
)
):
return False

kq = None
try:
kq = select.kqueue()
kev = select.kevent(
os.getpid(),
filter=select.KQ_FILTER_PROC,
flags=select.KQ_EV_ADD | select.KQ_EV_ONESHOT,
fflags=select.KQ_NOTE_EXIT,
)
events = kq.control([kev], 1, 0)
return True
except OSError as err:
if err.errno in {errno.EMFILE, errno.ENFILE}:
# transitory 'too many open files'
return True
return False
finally:
if kq is not None:
kq.close()


_CAN_USE_PIDFD_OPEN = _can_use_pidfd_open()
_CAN_USE_KQUEUE = _can_use_kqueue()


# These are primarily fail-safe knobs for negatives. A True value does not
# guarantee the given libc/syscall API will be used.
_USE_POSIX_SPAWN = _use_posix_spawn()
Expand Down Expand Up @@ -2046,14 +2104,100 @@ def _try_wait(self, wait_flags):
sts = 0
return (pid, sts)

def _wait_pidfd(self, timeout):
"""Wait for PID to terminate using pidfd_open() + poll().
Linux >= 5.3 only.
"""
if not _CAN_USE_PIDFD_OPEN:
return False
try:
pidfd = os.pidfd_open(self.pid, 0)
except OSError:
# May be:
# - ESRCH: no such process
# - EMFILE, ENFILE: too many open files (usually 1024)
# - ENODEV: anonymous inode filesystem not supported
# - EPERM, EACCES, ENOSYS: undocumented; may happen if
# blocked by security policy like SECCOMP
return False

try:
poller = select.poll()
poller.register(pidfd, select.POLLIN)
events = poller.poll(math.ceil(timeout * 1000))
if not events:
raise TimeoutExpired(self.args, timeout)
return True
finally:
os.close(pidfd)

def _wait_kqueue(self, timeout):
"""Wait for PID to terminate using kqueue(). macOS and BSD only."""
if not _CAN_USE_KQUEUE:
return False
try:
kq = select.kqueue()
except OSError:
# likely EMFILE / ENFILE (too many open files)
return False

try:
kev = select.kevent(
self.pid,
filter=select.KQ_FILTER_PROC,
flags=select.KQ_EV_ADD | select.KQ_EV_ONESHOT,
fflags=select.KQ_NOTE_EXIT,
)
try:
events = kq.control([kev], 1, timeout) # wait
except OSError as err: # should never happen
return False
else:
if not events:
raise TimeoutExpired(self.args, timeout)
return True
finally:
kq.close()

def _wait(self, timeout):
"""Internal implementation of wait() on POSIX."""
"""Internal implementation of wait() on POSIX.

Uses efficient pidfd_open() + poll() on Linux or kqueue()
on macOS/BSD when available. Falls back to polling
waitpid(WNOHANG) otherwise.
"""
if self.returncode is not None:
return self.returncode

if timeout is not None:
endtime = _time() + timeout
if timeout < 0:
raise TimeoutExpired(self.args, timeout)
started = _time()
endtime = started + timeout

# Try efficient wait first.
if self._wait_pidfd(timeout) or self._wait_kqueue(timeout):
# Process is gone. At this point os.waitpid(pid, 0)
# will return immediately, but in very rare races
# the PID may have been reused.
# os.waitpid(pid, WNOHANG) ensures we attempt a
# non-blocking reap without blocking indefinitely.
with self._waitpid_lock:
if self.returncode is not None:
return self.returncode # Another thread waited.
(pid, sts) = self._try_wait(os.WNOHANG)
assert pid == self.pid or pid == 0
if pid == self.pid:
self._handle_exitstatus(sts)
return self.returncode
# os.waitpid(pid, WNOHANG) returned 0 instead
# of our PID, meaning PID has not yet exited,
# even though poll() / kqueue() said so. Very
# rare and mostly theoretical. Fallback to busy
# polling.
elapsed = _time() - started
endtime -= elapsed

# Enter a busy loop if we have a timeout. This busy loop was
# cribbed from Lib/threading.py in Thread.wait() at r71065.
delay = 0.0005 # 500 us -> initial delay of 1 ms
Expand Down Expand Up @@ -2085,6 +2229,7 @@ def _wait(self, timeout):
# http://bugs.python.org/issue14396.
if pid == self.pid:
self._handle_exitstatus(sts)

return self.returncode


Expand Down
122 changes: 122 additions & 0 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,8 @@ def test_wait(self):
def test_wait_timeout(self):
p = subprocess.Popen([sys.executable,
"-c", "import time; time.sleep(0.3)"])
with self.assertRaises(subprocess.TimeoutExpired) as c:
p.wait(timeout=0)
with self.assertRaises(subprocess.TimeoutExpired) as c:
p.wait(timeout=0.0001)
self.assertIn("0.0001", str(c.exception)) # For coverage of __str__.
Expand Down Expand Up @@ -4094,5 +4096,125 @@ def test_broken_pipe_cleanup(self):
self.assertTrue(proc.stdin.closed)



class FastWaitTestCase(BaseTestCase):
"""Tests for efficient (pidfd_open() + poll() / kqueue()) process
waiting in subprocess.Popen.wait().
"""
CAN_USE_PIDFD_OPEN = subprocess._CAN_USE_PIDFD_OPEN
CAN_USE_KQUEUE = subprocess._CAN_USE_KQUEUE

def assert_fast_waitpid_error(self, patch_point):
# Emulate a case where pidfd_open() or kqueue() fails.
# Busy-poll wait should be used as fallback.
exc = OSError(errno.EMFILE, os.strerror(errno.EMFILE))
with mock.patch(patch_point, side_effect=exc) as m:
p = subprocess.Popen([sys.executable,
"-c", "import time; time.sleep(0.3)"])
with self.assertRaises(subprocess.TimeoutExpired):
p.wait(timeout=0.0001)
self.assertEqual(p.wait(timeout=support.SHORT_TIMEOUT), 0)
assert m.called

@unittest.skipIf(not CAN_USE_PIDFD_OPEN, reason="needs pidfd_open()")
def test_wait_pidfd_open_error(self):
self.assert_fast_waitpid_error("os.pidfd_open")

@unittest.skipIf(not CAN_USE_KQUEUE, reason="needs kqueue() for proc")
def test_wait_kqueue_error(self):
self.assert_fast_waitpid_error("select.kqueue")

@unittest.skipIf(not CAN_USE_KQUEUE, reason="needs kqueue() for proc")
def test_kqueue_control_error(self):
# Emulate a case where kqueue.control() fails. Busy-poll wait
# should be used as fallback.
p = subprocess.Popen([sys.executable,
"-c", "import time; time.sleep(0.3)"])
kq_mock = mock.Mock()
kq_mock.control.side_effect = OSError(
errno.EPERM, os.strerror(errno.EPERM)
)
kq_mock.close = mock.Mock()

with mock.patch("select.kqueue", return_value=kq_mock) as m:
with self.assertRaises(subprocess.TimeoutExpired):
p.wait(timeout=0.0001)
self.assertEqual(p.wait(timeout=support.SHORT_TIMEOUT), 0)
assert m.called

def assert_wait_race_condition(self, patch_target, real_func):
# Call pidfd_open() / kqueue(), then terminate the process.
# Make sure that the wait call (poll() / kqueue.control())
# still works for a terminated PID.
p = subprocess.Popen([sys.executable,
"-c", "import time; time.sleep(0.3)"])

def wrapper(*args, **kwargs):
ret = real_func(*args, **kwargs)
try:
os.kill(p.pid, signal.SIGTERM)
os.waitpid(p.pid, 0)
except OSError:
pass
return ret

with mock.patch(patch_target, side_effect=wrapper) as m:
status = p.wait(timeout=support.SHORT_TIMEOUT)
assert m.called
self.assertEqual(status, 0)

@unittest.skipIf(not CAN_USE_PIDFD_OPEN, reason="needs pidfd_open()")
def test_pidfd_open_race(self):
self.assert_wait_race_condition("os.pidfd_open", os.pidfd_open)

@unittest.skipIf(not CAN_USE_KQUEUE, reason="needs kqueue() for proc")
def test_kqueue_race(self):
self.assert_wait_race_condition("select.kqueue", select.kqueue)

def assert_notification_without_immediate_reap(self, patch_target):
# Verify fallback to busy polling when poll() / kqueue()
# succeeds, but waitpid(pid, WNOHANG) returns (0, 0).
def waitpid_wrapper(pid, flags):
nonlocal ncalls
ncalls += 1
if ncalls == 1:
return (0, 0)
return real_waitpid(pid, flags)

ncalls = 0
real_waitpid = os.waitpid
with mock.patch.object(subprocess.Popen, patch_target, return_value=True) as m1:
with mock.patch("os.waitpid", side_effect=waitpid_wrapper) as m2:
p = subprocess.Popen([sys.executable,
"-c", "import time; time.sleep(0.3)"])
with self.assertRaises(subprocess.TimeoutExpired):
p.wait(timeout=0.0001)
self.assertEqual(p.wait(timeout=support.SHORT_TIMEOUT), 0)
assert m1.called
assert m2.called

@unittest.skipIf(not CAN_USE_PIDFD_OPEN, reason="needs pidfd_open()")
def test_pidfd_open_notification_without_immediate_reap(self):
self.assert_notification_without_immediate_reap("_wait_pidfd")

@unittest.skipIf(not CAN_USE_KQUEUE, reason="needs kqueue() for proc")
def test_kqueue_notification_without_immediate_reap(self):
self.assert_notification_without_immediate_reap("_wait_kqueue")

@unittest.skipUnless(
CAN_USE_PIDFD_OPEN or CAN_USE_KQUEUE,
"fast wait mechanism not available"
)
def test_fast_path_avoid_busy_loop(self):
# assert that the busy loop is not called as long as the fast
# wait is available
with mock.patch('time.sleep') as m:
p = subprocess.Popen([sys.executable,
"-c", "import time; time.sleep(0.3)"])
with self.assertRaises(subprocess.TimeoutExpired):
p.wait(timeout=0.0001)
self.assertEqual(p.wait(timeout=support.LONG_TIMEOUT), 0)
assert not m.called

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:meth:`subprocess.Popen.wait`: when ``timeout`` is not ``None``, an efficient
event-driven mechanism now waits for process termination, if available. Linux
≥= 5.3 uses :func:`os.pidfd_open` + :func:`select.poll`. macOS and other BSD
variants use :func:`select.kqueue` + ``KQ_FILTER_PROC`` + ``KQ_NOTE_EXIT``.
Windows keeps using ``WaitForSingleObject`` (unchanged). If none of these
mechanisms are available, the function falls back to the traditional busy loop
(non-blocking call and short sleeps). Patch by Giampaolo Rodola.
Loading