mirror of
https://github.com/python/cpython.git
synced 2025-10-03 05:35:59 +00:00
bpo-36916: asyncio: Swallow unhandled write() exception (GH-13313)
This commit is contained in:
parent
c96be811fa
commit
f12ba7cd0a
3 changed files with 15 additions and 1 deletions
|
@ -329,6 +329,13 @@ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
|
||||||
closed.exception()
|
closed.exception()
|
||||||
|
|
||||||
|
|
||||||
|
def _swallow_unhandled_exception(task):
|
||||||
|
# Do a trick to suppress unhandled exception
|
||||||
|
# if stream.write() was used without await and
|
||||||
|
# stream.drain() was paused and resumed with an exception
|
||||||
|
task.exception()
|
||||||
|
|
||||||
|
|
||||||
class StreamWriter:
|
class StreamWriter:
|
||||||
"""Wraps a Transport.
|
"""Wraps a Transport.
|
||||||
|
|
||||||
|
@ -393,7 +400,9 @@ class StreamWriter:
|
||||||
# fast path, the stream is not paused
|
# fast path, the stream is not paused
|
||||||
# no need to wait for resume signal
|
# no need to wait for resume signal
|
||||||
return self._complete_fut
|
return self._complete_fut
|
||||||
return self._loop.create_task(self.drain())
|
ret = self._loop.create_task(self.drain())
|
||||||
|
ret.add_done_callback(_swallow_unhandled_exception)
|
||||||
|
return ret
|
||||||
|
|
||||||
def write_eof(self):
|
def write_eof(self):
|
||||||
return self._transport.write_eof()
|
return self._transport.write_eof()
|
||||||
|
|
|
@ -851,6 +851,8 @@ os.close(fd)
|
||||||
# where it never gives up the event loop but the socket is
|
# where it never gives up the event loop but the socket is
|
||||||
# closed on the server side.
|
# closed on the server side.
|
||||||
|
|
||||||
|
messages = []
|
||||||
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
||||||
q = queue.Queue()
|
q = queue.Queue()
|
||||||
|
|
||||||
def server():
|
def server():
|
||||||
|
@ -883,6 +885,7 @@ os.close(fd)
|
||||||
# Clean up the thread. (Only on success; on failure, it may
|
# Clean up the thread. (Only on success; on failure, it may
|
||||||
# be stuck in accept().)
|
# be stuck in accept().)
|
||||||
thread.join()
|
thread.join()
|
||||||
|
self.assertEqual([], messages)
|
||||||
|
|
||||||
def test___repr__(self):
|
def test___repr__(self):
|
||||||
stream = asyncio.StreamReader(loop=self.loop,
|
stream = asyncio.StreamReader(loop=self.loop,
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Remove a message about an unhandled exception in a task when writer.write()
|
||||||
|
is used without await and writer.drain() fails with an exception.
|
Loading…
Add table
Add a link
Reference in a new issue