diff options
author | Kumar Aditya <kumaraditya@python.org> | 2024-07-01 04:47:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-01 04:47:36 (GMT) |
commit | bd473aa598c5161521a7018896dc124728214a6c (patch) | |
tree | 804fb037a9ee7efed79c9207075179bb2d548100 /Lib/asyncio/base_subprocess.py | |
parent | 1a84bdc2371ada60c01c72493caba62c9860007b (diff) | |
download | cpython-bd473aa598c5161521a7018896dc124728214a6c.zip cpython-bd473aa598c5161521a7018896dc124728214a6c.tar.gz cpython-bd473aa598c5161521a7018896dc124728214a6c.tar.bz2 |
gh-87744: fix waitpid race while calling send_signal in asyncio (#121126)
asyncio earlier relied on subprocess module to send signals to the process, this has some drawbacks one being that subprocess module unnecessarily calls waitpid on child processes and hence it races with asyncio implementation which internally uses child watchers. To mitigate this, now asyncio sends signals directly to the process without going through the subprocess on non windows systems. On Windows it fallbacks to subprocess module handling but on windows there are no child watchers so this issue doesn't exists altogether.
Diffstat (limited to 'Lib/asyncio/base_subprocess.py')
-rw-r--r-- | Lib/asyncio/base_subprocess.py | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/Lib/asyncio/base_subprocess.py b/Lib/asyncio/base_subprocess.py index 6dbde2b..9c2ba67 100644 --- a/Lib/asyncio/base_subprocess.py +++ b/Lib/asyncio/base_subprocess.py @@ -1,6 +1,9 @@ import collections import subprocess import warnings +import os +import signal +import sys from . import protocols from . import transports @@ -142,17 +145,31 @@ class BaseSubprocessTransport(transports.SubprocessTransport): if self._proc is None: raise ProcessLookupError() - def send_signal(self, signal): - self._check_proc() - self._proc.send_signal(signal) + if sys.platform == 'win32': + def send_signal(self, signal): + self._check_proc() + self._proc.send_signal(signal) + + def terminate(self): + self._check_proc() + self._proc.terminate() + + def kill(self): + self._check_proc() + self._proc.kill() + else: + def send_signal(self, signal): + self._check_proc() + try: + os.kill(self._proc.pid, signal) + except ProcessLookupError: + pass - def terminate(self): - self._check_proc() - self._proc.terminate() + def terminate(self): + self.send_signal(signal.SIGTERM) - def kill(self): - self._check_proc() - self._proc.kill() + def kill(self): + self.send_signal(signal.SIGKILL) async def _connect_pipes(self, waiter): try: |