Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion Lib/asyncio/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'open_connection', 'start_server')

import collections
import copy
import socket
import sys
import warnings
Expand Down Expand Up @@ -280,7 +281,29 @@ def connection_lost(self, exc):
if exc is None:
self._closed.set_result(None)
else:
self._closed.set_exception(exc)
# Avoid sharing the same exception object between the
# reader future and the close waiter. Future.result()
# restores the traceback with `with_traceback()`, which
# mutates the exception in place; sharing one object
# between two futures rewrites the traceback of the
# in-flight exception being handled (gh-156278).
try:
exc_copy = copy.copy(exc)
except Exception:
try:
exc_copy = type(exc)(*exc.args)
# Preserve context attributes where possible.
if hasattr(exc, "__cause__"):
exc_copy.__cause__ = exc.__cause__
if hasattr(exc, "__context__"):
exc_copy.__context__ = exc.__context__
if hasattr(exc, "__suppress_context__"):
exc_copy.__suppress_context__ = exc.__suppress_context__
if exc.__traceback__ is not None:
exc_copy = exc_copy.with_traceback(exc.__traceback__)
except Exception:
exc_copy = exc
self._closed.set_exception(exc_copy)
super().connection_lost(exc)
self._stream_reader_wr = None
self._task = None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Fix :func:`asyncio.StreamWriter.wait_closed` traceback rewriting.

``StreamReaderProtocol.connection_lost`` no longer shares the same
exception object between the reader and the close waiter, which
previously caused ``await writer.wait_closed()`` in an ``except``
block to rewrite the ``__traceback__`` of the in-flight exception
being handled.
Loading