Skip to content

gh-87744: fix waitpid race while calling send_signal in asyncio #121126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 26 additions & 9 deletions Lib/asyncio/base_subprocess.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import collections
import subprocess
import warnings
import os
import signal
import sys

from . import protocols
from . import transports
Expand Down Expand Up @@ -142,17 +145,31 @@ def _check_proc(self):
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:
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_asyncio/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,21 @@ async def main():

self.loop.run_until_complete(main())

@unittest.skipIf(sys.platform != 'linux', "Linux only")
def test_subprocess_send_signal_race(self):
# See https://github.com/python/cpython/issues/87744
async def main():
for _ in range(10):
proc = await asyncio.create_subprocess_exec('sleep', '0.1')
await asyncio.sleep(0.1)
try:
proc.send_signal(signal.SIGUSR1)
except ProcessLookupError:
pass
self.assertNotEqual(await proc.wait(), 255)

self.loop.run_until_complete(main())


if sys.platform != 'win32':
# Unix
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix waitpid race while calling :meth:`~asyncio.subprocess.Process.send_signal` in asyncio. Patch by Kumar Aditya.
Loading