From 243a6dcda0b8bc84dd2dae97f347b829c729c1ba Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 18 Mar 2025 12:52:52 +0000 Subject: [PATCH 1/2] gh-131233: remove return-in-finally in multiprocessing/connection.py --- Lib/multiprocessing/connection.py | 38 +++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py index d429212d447380..9ee8a06372e77a 100644 --- a/Lib/multiprocessing/connection.py +++ b/Lib/multiprocessing/connection.py @@ -322,22 +322,32 @@ def _recv_bytes(self, maxsize=None): try: ov, err = _winapi.ReadFile(self._handle, bsize, overlapped=True) + + sentinel = object() + return_value = sentinel try: - if err == _winapi.ERROR_IO_PENDING: - waitres = _winapi.WaitForMultipleObjects( - [ov.event], False, INFINITE) - assert waitres == WAIT_OBJECT_0 + try: + if err == _winapi.ERROR_IO_PENDING: + waitres = _winapi.WaitForMultipleObjects( + [ov.event], False, INFINITE) + assert waitres == WAIT_OBJECT_0 + except: + ov.cancel() + raise + finally: + nread, err = ov.GetOverlappedResult(True) + if err == 0: + f = io.BytesIO() + f.write(ov.getbuffer()) + return_value = f + elif err == _winapi.ERROR_MORE_DATA: + return_value = self._get_more_data(ov, maxsize) except: - ov.cancel() - raise - finally: - nread, err = ov.GetOverlappedResult(True) - if err == 0: - f = io.BytesIO() - f.write(ov.getbuffer()) - return f - elif err == _winapi.ERROR_MORE_DATA: - return self._get_more_data(ov, maxsize) + if return_value == sentinel: + raise + + if return_value != sentinel: + return return_value except OSError as e: if e.winerror == _winapi.ERROR_BROKEN_PIPE: raise EOFError From c7099e7224f3457992ed7df8e2ea28b62e5b0e20 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Fri, 21 Mar 2025 17:40:18 +0000 Subject: [PATCH 2/2] Apply suggestions from code review --- Lib/multiprocessing/connection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py index 9ee8a06372e77a..5f288a8d393240 100644 --- a/Lib/multiprocessing/connection.py +++ b/Lib/multiprocessing/connection.py @@ -343,10 +343,10 @@ def _recv_bytes(self, maxsize=None): elif err == _winapi.ERROR_MORE_DATA: return_value = self._get_more_data(ov, maxsize) except: - if return_value == sentinel: + if return_value is sentinel: raise - if return_value != sentinel: + if return_value is not sentinel: return return_value except OSError as e: if e.winerror == _winapi.ERROR_BROKEN_PIPE: