From e0d6de50f22ffe093e47ee37dea93ed301d33f43 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 28 Jun 2024 12:31:37 +0000 Subject: [PATCH 1/5] fix send_signal race --- Lib/asyncio/base_subprocess.py | 13 ++++++++----- Lib/test/test_asyncio/test_subprocess.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Lib/asyncio/base_subprocess.py b/Lib/asyncio/base_subprocess.py index 6dbde2b696ad1f..ca675a1646f09c 100644 --- a/Lib/asyncio/base_subprocess.py +++ b/Lib/asyncio/base_subprocess.py @@ -1,6 +1,8 @@ import collections import subprocess import warnings +import os +import signal from . import protocols from . import transports @@ -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: diff --git a/Lib/test/test_asyncio/test_subprocess.py b/Lib/test/test_asyncio/test_subprocess.py index 23987c70ca7b63..e38c6eab4a4f00 100644 --- a/Lib/test/test_asyncio/test_subprocess.py +++ b/Lib/test/test_asyncio/test_subprocess.py @@ -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 From dc4924ae316392dd09f46051911a97964e219004 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 29 Jun 2024 05:00:23 +0000 Subject: [PATCH 2/5] fix windows --- Lib/asyncio/base_subprocess.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/Lib/asyncio/base_subprocess.py b/Lib/asyncio/base_subprocess.py index ca675a1646f09c..9c2ba679ce2bf1 100644 --- a/Lib/asyncio/base_subprocess.py +++ b/Lib/asyncio/base_subprocess.py @@ -3,6 +3,7 @@ import warnings import os import signal +import sys from . import protocols from . import transports @@ -144,18 +145,31 @@ def _check_proc(self): if self._proc is None: raise ProcessLookupError() - def send_signal(self, signal): - self._check_proc() - try: - os.kill(self._proc.pid, signal) - except ProcessLookupError: - pass + 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.send_signal(signal.SIGTERM) + def terminate(self): + self.send_signal(signal.SIGTERM) - def kill(self): - self.send_signal(signal.SIGKILL) + def kill(self): + self.send_signal(signal.SIGKILL) async def _connect_pipes(self, waiter): try: From a97b759e944d9eb47554001eef059695fcae95bf Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 29 Jun 2024 05:06:43 +0000 Subject: [PATCH 3/5] fix test --- Lib/test/test_asyncio/test_subprocess.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_asyncio/test_subprocess.py b/Lib/test/test_asyncio/test_subprocess.py index e38c6eab4a4f00..54501300a29cf7 100644 --- a/Lib/test/test_asyncio/test_subprocess.py +++ b/Lib/test/test_asyncio/test_subprocess.py @@ -864,6 +864,7 @@ 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(): From b9b6d29522c86143c81e55c31a1c1354002222b8 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 29 Jun 2024 05:09:03 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst diff --git a/Misc/NEWS.d/next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst b/Misc/NEWS.d/next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst new file mode 100644 index 00000000000000..fc24e201a6aa76 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst @@ -0,0 +1 @@ +Fix waitpid race while calling :meth`~asyncio.subprocess.Process.send_signal` in asyncio. Patch by Kumar Aditya. From 9136f3fde91287e4756d58c3f3132001f5091f1e Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 29 Jun 2024 05:22:08 +0000 Subject: [PATCH 5/5] fix news --- .../next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst b/Misc/NEWS.d/next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst index fc24e201a6aa76..c0b4f349fb6dac 100644 --- a/Misc/NEWS.d/next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst +++ b/Misc/NEWS.d/next/Library/2024-06-29-05-08-59.gh-issue-87744.rpF6Jw.rst @@ -1 +1 @@ -Fix waitpid race while calling :meth`~asyncio.subprocess.Process.send_signal` in asyncio. Patch by Kumar Aditya. +Fix waitpid race while calling :meth:`~asyncio.subprocess.Process.send_signal` in asyncio. Patch by Kumar Aditya.