From 1861add7384a8d709f83c491c595630b5b38e801 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 16 Apr 2021 16:40:06 +0200 Subject: [PATCH 1/2] bpo-43867: multiprocessing Server catchs SystemExit The multiprocessing Server class now explicitly catchs SystemExit and closes the client connection in this case. It happens when the Server.serve_client() method reachs the end of file (EOF). --- Lib/multiprocessing/managers.py | 9 ++++++++- .../Library/2021-04-16-16-46-44.bpo-43867.xT9QjF.rst | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2021-04-16-16-46-44.bpo-43867.xT9QjF.rst diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index 0eb16c664cfab36..8d43a06e5585471 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -188,10 +188,17 @@ def accepter(self): c = self.listener.accept() except OSError: continue - t = threading.Thread(target=self.handle_request, args=(c,)) + t = threading.Thread(target=self._thread_handle_request, args=(c,)) t.daemon = True t.start() + def _thread_handle_request(self, c): + try: + self.handle_request(c) + except SystemExit: + # Server.serve_client() calls sys.exit(0) on EOF + c.close() + def handle_request(self, c): ''' Handle a new connection diff --git a/Misc/NEWS.d/next/Library/2021-04-16-16-46-44.bpo-43867.xT9QjF.rst b/Misc/NEWS.d/next/Library/2021-04-16-16-46-44.bpo-43867.xT9QjF.rst new file mode 100644 index 000000000000000..1ec914e5ee9d037 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-04-16-16-46-44.bpo-43867.xT9QjF.rst @@ -0,0 +1,3 @@ +The :mod:`multiprocessing` ``Server`` class now explicitly catchs +:exc:`SystemExit` and closes the client connection in this case. It happens +when the ``Server.serve_client()`` method reachs the end of file (EOF). From 8dc42430eb38018c588a21d88a73c1bd70f18426 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 16 Apr 2021 19:15:22 +0200 Subject: [PATCH 2/2] Wrap handle_request() method --- Lib/multiprocessing/managers.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index 8d43a06e5585471..6ee9521c76b48fa 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -188,22 +188,12 @@ def accepter(self): c = self.listener.accept() except OSError: continue - t = threading.Thread(target=self._thread_handle_request, args=(c,)) + t = threading.Thread(target=self.handle_request, args=(c,)) t.daemon = True t.start() - def _thread_handle_request(self, c): - try: - self.handle_request(c) - except SystemExit: - # Server.serve_client() calls sys.exit(0) on EOF - c.close() - - def handle_request(self, c): - ''' - Handle a new connection - ''' - funcname = result = request = None + def _handle_request(self, c): + request = None try: connection.deliver_challenge(c, self.authkey) connection.answer_challenge(c, self.authkey) @@ -220,6 +210,7 @@ def handle_request(self, c): msg = ('#TRACEBACK', format_exc()) else: msg = ('#RETURN', result) + try: c.send(msg) except Exception as e: @@ -231,7 +222,17 @@ def handle_request(self, c): util.info(' ... request was %r', request) util.info(' ... exception was %r', e) - c.close() + def handle_request(self, conn): + ''' + Handle a new connection + ''' + try: + self._handle_request(conn) + except SystemExit: + # Server.serve_client() calls sys.exit(0) on EOF + pass + finally: + conn.close() def serve_client(self, conn): '''