Skip to content

Commit

Permalink
fix send_signal race
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 committed Jun 28, 2024
1 parent ef3c400 commit e0d6de5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Lib/asyncio/base_subprocess.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import collections
import subprocess
import warnings
import os
import signal

from . import protocols
from . import transports
Expand Down Expand Up @@ -144,15 +146,16 @@ def _check_proc(self):

def send_signal(self, signal):
self._check_proc()
self._proc.send_signal(signal)
try:
os.kill(self._proc.pid, signal)
except ProcessLookupError:
pass

def terminate(self):
self._check_proc()
self._proc.terminate()
self.send_signal(signal.SIGTERM)

def kill(self):
self._check_proc()
self._proc.kill()
self.send_signal(signal.SIGKILL)

async def _connect_pipes(self, waiter):
try:
Expand Down
14 changes: 14 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,20 @@ async def main():

self.loop.run_until_complete(main())

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

0 comments on commit e0d6de5

Please sign in to comment.