Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit76a0f34

Browse files
kumaraditya303miss-islington
authored andcommitted
pythongh-87744: fix waitpid race while calling send_signal in asyncio (pythonGH-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.(cherry picked from commitbd473aa)Co-authored-by: Kumar Aditya <kumaraditya@python.org>
1 parentaf89237 commit76a0f34

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

‎Lib/asyncio/base_subprocess.py‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
importcollections
22
importsubprocess
33
importwarnings
4+
importos
5+
importsignal
6+
importsys
47

58
from .importprotocols
69
from .importtransports
@@ -142,17 +145,31 @@ def _check_proc(self):
142145
ifself._procisNone:
143146
raiseProcessLookupError()
144147

145-
defsend_signal(self,signal):
146-
self._check_proc()
147-
self._proc.send_signal(signal)
148+
ifsys.platform=='win32':
149+
defsend_signal(self,signal):
150+
self._check_proc()
151+
self._proc.send_signal(signal)
152+
153+
defterminate(self):
154+
self._check_proc()
155+
self._proc.terminate()
156+
157+
defkill(self):
158+
self._check_proc()
159+
self._proc.kill()
160+
else:
161+
defsend_signal(self,signal):
162+
self._check_proc()
163+
try:
164+
os.kill(self._proc.pid,signal)
165+
exceptProcessLookupError:
166+
pass
148167

149-
defterminate(self):
150-
self._check_proc()
151-
self._proc.terminate()
168+
defterminate(self):
169+
self.send_signal(signal.SIGTERM)
152170

153-
defkill(self):
154-
self._check_proc()
155-
self._proc.kill()
171+
defkill(self):
172+
self.send_signal(signal.SIGKILL)
156173

157174
asyncdef_connect_pipes(self,waiter):
158175
try:

‎Lib/test/test_asyncio/test_subprocess.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,21 @@ async def main():
873873

874874
self.loop.run_until_complete(main())
875875

876+
@unittest.skipIf(sys.platform!='linux',"Linux only")
877+
deftest_subprocess_send_signal_race(self):
878+
# See https://github.com/python/cpython/issues/87744
879+
asyncdefmain():
880+
for_inrange(10):
881+
proc=awaitasyncio.create_subprocess_exec('sleep','0.1')
882+
awaitasyncio.sleep(0.1)
883+
try:
884+
proc.send_signal(signal.SIGUSR1)
885+
exceptProcessLookupError:
886+
pass
887+
self.assertNotEqual(awaitproc.wait(),255)
888+
889+
self.loop.run_until_complete(main())
890+
876891

877892
ifsys.platform!='win32':
878893
# Unix
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix waitpid race while calling:meth:`~asyncio.subprocess.Process.send_signal` in asyncio. Patch by Kumar Aditya.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp