summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>2022-10-08 00:29:09 (GMT)
committerGitHub <noreply@github.com>2022-10-08 00:29:09 (GMT)
commit8ba9378b168ad330c158a001afca03d6c028d39b (patch)
tree3bd0eee04709afec69d16402c9eeb08a43f49f59 /Lib/asyncio
parent3d8b22454728b6d54cc476cfb59e47ab40f3f527 (diff)
downloadcpython-8ba9378b168ad330c158a001afca03d6c028d39b.zip
cpython-8ba9378b168ad330c158a001afca03d6c028d39b.tar.gz
cpython-8ba9378b168ad330c158a001afca03d6c028d39b.tar.bz2
GH-98023: Change default child watcher to PidfdChildWatcher on supported systems (#98024)
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/unix_events.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py
index 0f67b4d..7fc75cd 100644
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -1403,6 +1403,17 @@ class ThreadedChildWatcher(AbstractChildWatcher):
self._threads.pop(expected_pid)
+def can_use_pidfd():
+ if not hasattr(os, 'pidfd_open'):
+ return False
+ try:
+ pid = os.getpid()
+ os.close(os.pidfd_open(pid, 0))
+ except OSError:
+ # blocked by security policy like SECCOMP
+ return False
+ return True
+
class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
"""UNIX event loop policy with a watcher for child processes."""
@@ -1415,7 +1426,10 @@ class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
def _init_watcher(self):
with events._lock:
if self._watcher is None: # pragma: no branch
- self._watcher = ThreadedChildWatcher()
+ if can_use_pidfd():
+ self._watcher = PidfdChildWatcher()
+ else:
+ self._watcher = ThreadedChildWatcher()
if threading.current_thread() is threading.main_thread():
self._watcher.attach_loop(self._local._loop)