8000 fix send_signal race · python/cpython@e0d6de5 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0d6de5

Browse files
fix send_signal race
1 parent ef3c400 commit e0d6de5

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Lib/asyncio/base_subprocess.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import collections
22
import subprocess
33
import warnings
4+
import os
5+
import signal
46

57
from . import protocols
68
from . import transports
@@ -144,15 +146,16 @@ def _check_proc(self):
144146

145147
def send_signal(self, signal):
146148
self._check_proc()
147-
self._proc.send_signal(signal)
149+
try:
150+
os.kill(self._proc.pid, signal)
151+
except ProcessLookupError:
152+
pass
148153

149154
def terminate(self):
150-
self._check_proc()
151-
self._proc.terminate()
155+
self.send_signal(signal.SIGTERM)
152156

153157
def kill(self):
154-
self._check_proc()
155-
self._proc.kill()
158+
self.send_signal(signal.SIGKILL)
156159

157160
async def _connect_pipes(self, waiter):
158161
try:

Lib/test/test_asyncio/test_subprocess.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,20 @@ async def main():
864864

865865
self.loop.run_until_complete(main())
866866

867+
def test_subprocess_send_signal_race(self):
868+
# See https://github.com/python/cpython/issues/87744
869+
async def main():
870+
for _ in range(10):
871+
proc = await asyncio.create_subprocess_exec('sleep', '0.1')
872+
await asyncio.sleep(0.1)
873+
try:
874+
proc.send_signal(signal.SIGUSR1)
875+
except ProcessLookupError:
876+
pass
877+
self.assertNotEqual(await proc.wait(), 255)
878+
879+
self.loop.run_until_complete(main())
880+
867881

868882
if sys.platform != 'win32':
869883
# Unix

0 commit comments

Comments
 (0)
0