]> granicus.if.org Git - python/commitdiff
bpo-36916: asyncio: Swallow unhandled write() exception (GH-13313)
authorAndrew Svetlov <andrew.svetlov@gmail.com>
Tue, 14 May 2019 16:09:44 +0000 (19:09 +0300)
committerVictor Stinner <vstinner@redhat.com>
Tue, 14 May 2019 16:09:44 +0000 (18:09 +0200)
Lib/asyncio/streams.py
Lib/test/test_asyncio/test_streams.py
Misc/NEWS.d/next/Library/2019-05-14-15-39-34.bpo-36916._GPsTt.rst [new file with mode: 0644]

index d9a9f5e72d3b798e023dec54313111a2a4bf8b16..146a33818d952a0a1e944db4e5d04d809213adc9 100644 (file)
@@ -329,6 +329,13 @@ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
             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:
     """Wraps a Transport.
 
@@ -393,7 +400,9 @@ class StreamWriter:
                 # fast path, the stream is not paused
                 # no need to wait for resume signal
                 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):
         return self._transport.write_eof()
index bf93f30e1aafb6d917c5250610957cb727ec5805..8d6a1d26ac1934e8d4b62f6488d6a3c208c4ff3c 100644 (file)
@@ -851,6 +851,8 @@ os.close(fd)
         # where it never gives up the event loop but the socket is
         # closed on the  server side.
 
+        messages = []
+        self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
         q = queue.Queue()
 
         def server():
@@ -883,6 +885,7 @@ os.close(fd)
         # Clean up the thread.  (Only on success; on failure, it may
         # be stuck in accept().)
         thread.join()
+        self.assertEqual([], messages)
 
     def test___repr__(self):
         stream = asyncio.StreamReader(loop=self.loop,
diff --git a/Misc/NEWS.d/next/Library/2019-05-14-15-39-34.bpo-36916._GPsTt.rst b/Misc/NEWS.d/next/Library/2019-05-14-15-39-34.bpo-36916._GPsTt.rst
new file mode 100644 (file)
index 0000000..8726bb2
--- /dev/null
@@ -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.