Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.11] gh-113538: Don't error in stream reader protocol callback when task is cancelled (GH-113690)#113714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-113538: Don't error in stream reader protocol callback when task i…
…s cancelled (GH-113690)(cherry picked from commit4681a52)Co-authored-by: Guido van Rossum <guido@python.org>
  • Loading branch information
@gvanrossum@miss-islington
gvanrossum authored andmiss-islington committedJan 4, 2024
commit6429b4688b211a2a758991a370680d073df0423b
3 changes: 3 additions & 0 deletionsLib/asyncio/streams.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -246,6 +246,9 @@ def connection_made(self, transport):
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({
Expand Down
20 changes: 13 additions & 7 deletionsLib/test/test_asyncio/test_streams.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1123,7 +1123,7 @@ async def inner(httpd):

self.assertEqual(messages, [])

deftest_unhandled_exceptions(self) -> None:
def_basetest_unhandled_exceptions(self, handle_echo):
port = socket_helper.find_unused_port()

messages = []
Expand All@@ -1137,9 +1137,6 @@ async def client():
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()
Expand All@@ -1148,11 +1145,20 @@ async def handle_echo(reader, writer):
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__':
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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.

[8]ページ先頭

©2009-2025 Movatter.jp