From 3d7dfbc1c19280dd19693d8d8e0574590c0c1bee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Fri, 8 May 2020 22:18:05 +0100 Subject: [PATCH 1/2] bpo-40550: fix time-of-check/time-of-action issue in multiprocessing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe LaĆ­ns --- Lib/subprocess.py | 8 +++++++- Lib/test/test_subprocess.py | 13 +++++++++++++ .../2020-05-08-21-30-54.bpo-40550.i7GWkb.rst | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-05-08-21-30-54.bpo-40550.i7GWkb.rst diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 13600c28cf7117..b40d1c38ff09d3 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -2061,7 +2061,13 @@ def send_signal(self, sig): # The race condition can still happen if the race condition # described above happens between the returncode test # and the kill() call. - os.kill(self.pid, sig) + try: + os.kill(self.pid, sig) + except ProcessLookupError as e: + # supress the process not found error in case the race + # condition happens, see bpo-40550 + if e.errno != errno.ESRCH: + raise e def terminate(self): """Terminate the process with SIGTERM diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index aced87694cf7b6..3a63869231a3bd 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -3145,6 +3145,19 @@ def test_send_signal_race(self): # so Popen failed to read it and uses a default returncode instead. self.assertIsNotNone(proc.returncode) + def test_send_signal_race2(self): + # bpo-40550: the process might exist between the returncode check and + # the kill operation + p = subprocess.Popen([sys.executable, '-c', 'exit(1)']) + + # wait for process to exit + while not p.returncode: + p.poll() + + with mock.patch.object(p, 'poll', new=lambda: None): + p.returncode = None + p.send_signal(signal.SIGTERM) + def test_communicate_repeated_call_after_stdout_close(self): proc = subprocess.Popen([sys.executable, '-c', 'import os, time; os.close(1), time.sleep(2)'], diff --git a/Misc/NEWS.d/next/Library/2020-05-08-21-30-54.bpo-40550.i7GWkb.rst b/Misc/NEWS.d/next/Library/2020-05-08-21-30-54.bpo-40550.i7GWkb.rst new file mode 100644 index 00000000000000..b0f3f03c34bbc5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-08-21-30-54.bpo-40550.i7GWkb.rst @@ -0,0 +1 @@ +Fix time-of-check/time-of-action issue in subprocess.Popen.send_signal. From 1605031b9a5eeb922feb7d76ebfad583a9637a96 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 21 Nov 2020 00:48:42 -0800 Subject: [PATCH 2/2] Remove a redundant check of errno. The ProcessLookupError already means errno == ESRCH. --- Lib/subprocess.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index b40d1c38ff09d3..f1d829a6f1724e 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -2063,11 +2063,9 @@ def send_signal(self, sig): # and the kill() call. try: os.kill(self.pid, sig) - except ProcessLookupError as e: - # supress the process not found error in case the race - # condition happens, see bpo-40550 - if e.errno != errno.ESRCH: - raise e + except ProcessLookupError: + # Supress the race condition error; bpo-40550. + pass def terminate(self): """Terminate the process with SIGTERM