Skip to content

Commit

Permalink
Fix child process watchers racing to free PIDs managed by Popen
Browse files Browse the repository at this point in the history
Note that we call Popen.poll/wait in the event loop thread to avoid
Popen's thread-unsafety. Without this workaround, when testing
_ThreadedChildWatcher with the reproducer shared in the linked issue on my
machine:
* Case 1 of the known race condition ignored in pythonGH-20010 is met (and an
  unsafe kill call is issued) about 1 in 10 times.
* The race condition pythonGH-127050 is met (causing _process_exited's assert
  returncode is not None to raise) about 1 in 30 times.
  • Loading branch information
gschaffner committed Nov 19, 2024
1 parent a50f0b8 commit 65cce5c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 deletions.
4 changes: 0 additions & 4 deletions Lib/asyncio/base_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ def _process_exited(self, returncode):
if self._loop.get_debug():
logger.info('%r exited with return code %r', self, returncode)
self._returncode = returncode
if self._proc.returncode is None:
# asyncio uses a child watcher: copy the status into the Popen
# object. On Python 3.6, it is required to avoid a ResourceWarning.
self._proc.returncode = returncode
self._call(self._protocol.process_exited)

self._try_finish()
Expand Down
81 changes: 42 additions & 39 deletions Lib/asyncio/unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ async def _make_subprocess_transport(self, protocol, args, shell,
waiter=waiter, extra=extra,
**kwargs)
watcher.add_child_handler(transp.get_pid(),
self._child_watcher_callback, transp)
self._child_watcher_callback_threadsafe, transp._proc, transp)
try:
await waiter
except (SystemExit, KeyboardInterrupt):
Expand All @@ -217,8 +217,34 @@ async def _make_subprocess_transport(self, protocol, args, shell,

return transp

def _child_watcher_callback(self, pid, returncode, transp):
self.call_soon_threadsafe(transp._process_exited, returncode)
def _child_watcher_callback_threadsafe(self, pid, proc, transp):
if self.is_closed():
logger.warning("Loop %r that handles pid %r is closed", loop, pid)
# Bump the Popen to reap the PID so it doesn't emit a
# ResourceWarning later.
proc.poll()
else:
self.call_soon_threadsafe(self._child_watcher_callback, pid, proc, transp)

def _child_watcher_callback(self, pid, proc, transp):
# N.B.: This poll call must happen in the event loop thread to avoid
# both case 1 of the known race condition ignored in GH-20010 and the
# race condition GH-127050.
returncode = proc.poll()
if proc._returncode_is_a_lie:
# A buggy reap call (i.e. one external to Popen) stole the return code from
# Popen. Popen will silently fail and report return code zero.
returncode = 255
logger.warning(
"child process pid %d exit status already read: "
" will report returncode 255",
pid)
else:
if self.get_debug():
logger.debug('process %s exited with returncode %s',
pid, returncode)

transp._process_exited(returncode)

async def create_unix_connection(
self, protocol_factory, path=None, *,
Expand Down Expand Up @@ -870,26 +896,13 @@ class _PidfdChildWatcher:
def add_child_handler(self, pid, callback, *args):
loop = events.get_running_loop()
pidfd = os.pidfd_open(pid)
loop._add_reader(pidfd, self._do_wait, pid, pidfd, callback, args)
loop._add_reader(pidfd, self._reader_callback, pid, pidfd, callback, args)

def _do_wait(self, pid, pidfd, callback, args):
def _reader_callback(self, pid, pidfd, callback, args):
loop = events.get_running_loop()
loop._remove_reader(pidfd)
try:
_, status = os.waitpid(pid, 0)
except ChildProcessError:
# The child process is already reaped
# (may happen if waitpid() is called elsewhere).
returncode = 255
logger.warning(
"child process pid %d exit status already read: "
" will report returncode 255",
pid)
else:
returncode = waitstatus_to_exitcode(status)

os.close(pidfd)
callback(pid, returncode, *args)
callback(pid, *args)

class _ThreadedChildWatcher:
"""Threaded child watcher implementation.
Expand Down Expand Up @@ -918,38 +931,28 @@ def __del__(self, _warn=warnings.warn):

def add_child_handler(self, pid, callback, *args):
loop = events.get_running_loop()
thread = threading.Thread(target=self._do_waitpid,
name=f"asyncio-waitpid-{next(self._pid_counter)}",
thread = threading.Thread(target=self._do_waitid_WNOWAIT,
name=f"asyncio-waitid+WNOWAIT-{next(self._pid_counter)}",
args=(loop, pid, callback, args),
daemon=True)
self._threads[pid] = thread
thread.start()

def _do_waitpid(self, loop, expected_pid, callback, args):
def _do_waitid_WNOWAIT(self, loop, expected_pid, callback, args):
assert expected_pid > 0

try:
pid, status = os.waitpid(expected_pid, 0)
# Do not reap---Popen is responsible for reaping and we mustn't have
# a second reap call racing with it.
os.waitid(os.P_PID, expected_pid, os.WEXITED | os.WNOWAIT)
except ChildProcessError:
# The child process is already reaped
# (may happen if waitpid() is called elsewhere).
pid = expected_pid
returncode = 255
logger.warning(
"Unknown child process pid %d, will report returncode 255",
pid)
else:
returncode = waitstatus_to_exitcode(status)
if loop.get_debug():
logger.debug('process %s exited with returncode %s',
expected_pid, returncode)

if loop.is_closed():
logger.warning("Loop %r that handles pid %r is closed", loop, pid)
else:
loop.call_soon_threadsafe(callback, pid, returncode, *args)
# (may happen if waitpid() is called elsewhere, e.g. by
# Process.send_signal() in the event loop thread).
pass

self._threads.pop(expected_pid)
callback(expected_pid, *args)

def can_use_pidfd():
if not hasattr(os, 'pidfd_open'):
Expand Down
3 changes: 3 additions & 0 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ def __init__(self, args, bufsize=-1, executable=None,
self.stderr = None
self.pid = None
self.returncode = None
self._returncode_is_a_lie = False
self.encoding = encoding
self.errors = errors
self.pipesize = pipesize
Expand Down Expand Up @@ -2005,6 +2006,7 @@ def _internal_poll(self, _deadstate=None, _del_safe=_del_safe):
# disabled for our process. This child is dead, we
# can't get the status.
# http://bugs.python.org/issue15756
self._returncode_is_a_lie = True
self.returncode = 0
finally:
self._waitpid_lock.release()
Expand All @@ -2019,6 +2021,7 @@ def _try_wait(self, wait_flags):
# This happens if SIGCLD is set to be ignored or waiting
# for child processes has otherwise been disabled for our
# process. This child is dead, we can't get the status.
self._returncode_is_a_lie = True
pid = self.pid
sts = 0
return (pid, sts)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,7 @@ Ben Sayer
Luca Sbardella
Marco Scataglini
Andrew Schaaf
Ganden Schaffner
Michael Scharf
Andreas Schawo
Neil Schemenauer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix race condition where :meth:`asyncio.subprocess.Process.send_signal`,
:meth:`asyncio.subprocess.Process.terminate`, and
:meth:`asyncio.subprocess.Process.kill` could signal an already-freed PID on
non-Windows platforms. Patch by Ganden Schaffner.

0 comments on commit 65cce5c

Please sign in to comment.