]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-113538: Don't error in stream reader protocol callback when task is cancelled...
authorGuido van Rossum <guido@python.org>
Thu, 4 Jan 2024 20:20:21 +0000 (12:20 -0800)
committerGitHub <noreply@github.com>
Thu, 4 Jan 2024 20:20:21 +0000 (12:20 -0800)
Lib/asyncio/streams.py
Lib/test/test_asyncio/test_streams.py
Misc/NEWS.d/next/Library/2024-01-03-14-19-26.gh-issue-113538.ahuBCo.rst [new file with mode: 0644]

index ffb48b9cd6cb70abd2be2b62dcfc6557855b32a5..df58b7a799a5add3a67fa013ef0c14ee2c4b711d 100644 (file)
@@ -246,6 +246,9 @@ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
                                             self._stream_writer)
             if coroutines.iscoroutine(res):
                 def callback(task):
+                    if task.cancelled():
+                        transport.close()
+                        return
                     exc = task.exception()
                     if exc is not None:
                         self._loop.call_exception_handler({
index b408cd1f7da205a4e8c1d095ab5155a2600bb5a0..3c8cc5f3649180d15ddb1a5a00f26008f25d8351 100644 (file)
@@ -1129,7 +1129,7 @@ os.close(fd)
 
         self.assertEqual(messages, [])
 
-    def test_unhandled_exceptions(self) -> None:
+    def _basetest_unhandled_exceptions(self, handle_echo):
         port = socket_helper.find_unused_port()
 
         messages = []
@@ -1143,9 +1143,6 @@ os.close(fd)
             await wr.wait_closed()
 
         async def main():
-            async def handle_echo(reader, writer):
-                raise Exception('test')
-
             server = await asyncio.start_server(
                 handle_echo, 'localhost', port)
             await server.start_serving()
@@ -1154,11 +1151,20 @@ os.close(fd)
             await server.wait_closed()
 
         self.loop.run_until_complete(main())
+        return messages
 
+    def test_unhandled_exception(self):
+        async def handle_echo(reader, writer):
+            raise Exception('test')
+        messages = self._basetest_unhandled_exceptions(handle_echo)
         self.assertEqual(messages[0]['message'],
-                         'Unhandled exception in client_connected_cb')
-        # Break explicitly reference cycle
-        messages = None
+                    'Unhandled exception in client_connected_cb')
+
+    def test_unhandled_cancel(self):
+        async def handle_echo(reader, writer):
+            asyncio.current_task().cancel()
+        messages = self._basetest_unhandled_exceptions(handle_echo)
+        self.assertEqual(messages, [])
 
 
 if __name__ == '__main__':
diff --git a/Misc/NEWS.d/next/Library/2024-01-03-14-19-26.gh-issue-113538.ahuBCo.rst b/Misc/NEWS.d/next/Library/2024-01-03-14-19-26.gh-issue-113538.ahuBCo.rst
new file mode 100644 (file)
index 0000000..a520765
--- /dev/null
@@ -0,0 +1,5 @@
+In :meth:`asyncio.StreamReaderProtocol.connection_made`, there is callback
+that logs an error if the task wrapping the "connected callback" fails. This
+callback would itself fail if the task was cancelled. Prevent this by
+checking whether the task was cancelled first. If so, close the transport
+but don't log an error.