diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-01-04 20:39:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-04 20:39:52 (GMT) |
commit | 456e2ecdd24f372bffd3083b9e4e51ca973f81f6 (patch) | |
tree | 0335fc376f6c98b0a56b35924e75162fe1d23707 /Lib | |
parent | c828dbc196519f942418953aa9758e2df3a0bd9b (diff) | |
download | cpython-456e2ecdd24f372bffd3083b9e4e51ca973f81f6.zip cpython-456e2ecdd24f372bffd3083b9e4e51ca973f81f6.tar.gz cpython-456e2ecdd24f372bffd3083b9e4e51ca973f81f6.tar.bz2 |
[3.11] gh-113538: Don't error in stream reader protocol callback when task is cancelled (GH-113690) (#113714)
(cherry picked from commit 4681a5271a8598b46021cbc556ac8098ab8a1d81)
Co-authored-by: Guido van Rossum <guido@python.org>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/asyncio/streams.py | 3 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_streams.py | 20 |
2 files changed, 16 insertions, 7 deletions
diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py index 23b6e4c..26ffc86 100644 --- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -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({ diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index f5decbe..59ff89e 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -1123,7 +1123,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 = [] @@ -1137,9 +1137,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() @@ -1148,11 +1145,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__': |