From e3427a7290babab299da3d09f685feefa237e8cb Mon Sep 17 00:00:00 2001 From: Luke Zhang Date: Tue, 16 May 2023 17:55:04 +0800 Subject: [PATCH 1/5] fix issue 104536, modify _cleanup function logic --- Lib/multiprocessing/process.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index c03c859baa795b..aae18e0e645458 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -61,7 +61,7 @@ def parent_process(): def _cleanup(): # check for processes which have finished for p in list(_children): - if p._popen.poll() is not None: + if p._popen is not None and p._popen.poll() is not None: _children.discard(p) # From 627732a51cb1d1bcb3f9c022aa50b01c5715706c Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 16 May 2023 10:07:17 +0000 Subject: [PATCH 2/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/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst diff --git a/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst b/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst new file mode 100644 index 00000000000000..23d65ae3d9cb59 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst @@ -0,0 +1 @@ +fix multiprocess.process._cleanup logic From 75b6deb6b93ca889a21cda201d2606de259e691e Mon Sep 17 00:00:00 2001 From: Luccccifer Date: Fri, 19 May 2023 10:40:26 +0800 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Oleg Iarygin --- .../next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst b/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst index 23d65ae3d9cb59..662da2f3cdca40 100644 --- a/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst +++ b/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst @@ -1 +1 @@ -fix multiprocess.process._cleanup logic +Fix :meth:`!multiprocess.process._cleanup` logic. From 9cc282fcc6b08ee24e7d5999dd3fb2204798d3dc Mon Sep 17 00:00:00 2001 From: Luke Zhang Date: Mon, 22 May 2023 11:08:54 +0800 Subject: [PATCH 4/5] avoid race condition by assigning p._popen once --- Lib/multiprocessing/process.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index aae18e0e645458..271ba3fd325138 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -61,7 +61,7 @@ def parent_process(): def _cleanup(): # check for processes which have finished for p in list(_children): - if p._popen is not None and p._popen.poll() is not None: + if (child_popen := p._popen) and child_popen.poll() is not None: _children.discard(p) # From 849e3ba1d7a5e8a9c7fade89295651fec72b8b12 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 21 May 2023 20:22:23 -0700 Subject: [PATCH 5/5] reword the news entry --- .../Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst b/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst index 662da2f3cdca40..b0f5d78f7e61da 100644 --- a/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst +++ b/Misc/NEWS.d/next/Library/2023-05-16-10-07-16.gh-issue-104536.hFWD8f.rst @@ -1 +1,3 @@ -Fix :meth:`!multiprocess.process._cleanup` logic. +Fix a race condition in the internal :mod:`multiprocessing.process` cleanup +logic that could manifest as an unintended ``AttributeError`` when calling +``process.close()``.