[Python-checkins] bpo-38529: Fix asyncio stream warning (GH-17474)

Miss Islington (bot) webhook-mailer at python.org
Sat Dec 7 06:40:01 EST 2019


https://github.com/python/cpython/commit/7fde4f446a3dcfed780a38fbfcd7c0b4d9d73b93
commit: 7fde4f446a3dcfed780a38fbfcd7c0b4d9d73b93
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-12-07T03:39:57-08:00
summary:

bpo-38529: Fix asyncio stream warning (GH-17474)

(cherry picked from commit 7ddcd0caa4c2e6b43265df144f59c5aa508a94f2)

Co-authored-by: Andrew Svetlov <andrew.svetlov at gmail.com>

files:
A Misc/NEWS.d/next/Library/2019-12-05-16-13-25.bpo-38529.yvQgx3.rst
M Lib/asyncio/streams.py
M Lib/test/test_asyncio/test_streams.py

diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py
index 795530e6f69ef..3c80bb8892590 100644
--- a/Lib/asyncio/streams.py
+++ b/Lib/asyncio/streams.py
@@ -214,8 +214,7 @@ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
     def __init__(self, stream_reader, client_connected_cb=None, loop=None):
         super().__init__(loop=loop)
         if stream_reader is not None:
-            self._stream_reader_wr = weakref.ref(stream_reader,
-                                                 self._on_reader_gc)
+            self._stream_reader_wr = weakref.ref(stream_reader)
             self._source_traceback = stream_reader._source_traceback
         else:
             self._stream_reader_wr = None
@@ -231,22 +230,6 @@ def __init__(self, stream_reader, client_connected_cb=None, loop=None):
         self._over_ssl = False
         self._closed = self._loop.create_future()
 
-    def _on_reader_gc(self, wr):
-        transport = self._transport
-        if transport is not None:
-            # connection_made was called
-            context = {
-                'message': ('An open stream object is being garbage '
-                            'collected; call "stream.close()" explicitly.')
-            }
-            if self._source_traceback:
-                context['source_traceback'] = self._source_traceback
-            self._loop.call_exception_handler(context)
-            transport.abort()
-        else:
-            self._reject_connection = True
-        self._stream_reader_wr = None
-
     @property
     def _stream_reader(self):
         if self._stream_reader_wr is None:
diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py
index b9413ab35fc50..12bd536911dbb 100644
--- a/Lib/test/test_asyncio/test_streams.py
+++ b/Lib/test/test_asyncio/test_streams.py
@@ -924,59 +924,6 @@ def test_wait_closed_on_close_with_unread_data(self):
             wr.close()
             self.loop.run_until_complete(wr.wait_closed())
 
-    def test_del_stream_before_sock_closing(self):
-        messages = []
-        self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
-
-        with test_utils.run_test_server() as httpd:
-            with self.assertWarns(DeprecationWarning):
-                rd, wr = self.loop.run_until_complete(
-                    asyncio.open_connection(*httpd.address, loop=self.loop))
-            sock = wr.get_extra_info('socket')
-            self.assertNotEqual(sock.fileno(), -1)
-
-            wr.write(b'GET / HTTP/1.0\r\n\r\n')
-            f = rd.readline()
-            data = self.loop.run_until_complete(f)
-            self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
-
-            # drop refs to reader/writer
-            del rd
-            del wr
-            gc.collect()
-            # make a chance to close the socket
-            test_utils.run_briefly(self.loop)
-
-            self.assertEqual(1, len(messages))
-            self.assertEqual(sock.fileno(), -1)
-
-        self.assertEqual(1, len(messages))
-        self.assertEqual('An open stream object is being garbage '
-                         'collected; call "stream.close()" explicitly.',
-                         messages[0]['message'])
-
-    def test_del_stream_before_connection_made(self):
-        messages = []
-        self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
-
-        with test_utils.run_test_server() as httpd:
-            rd = asyncio.StreamReader(loop=self.loop)
-            pr = asyncio.StreamReaderProtocol(rd, loop=self.loop)
-            del rd
-            gc.collect()
-            tr, _ = self.loop.run_until_complete(
-                self.loop.create_connection(
-                    lambda: pr, *httpd.address))
-
-            sock = tr.get_extra_info('socket')
-            self.assertEqual(sock.fileno(), -1)
-
-        self.assertEqual(1, len(messages))
-        self.assertEqual('An open stream was garbage collected prior to '
-                         'establishing network connection; '
-                         'call "stream.close()" explicitly.',
-                         messages[0]['message'])
-
     def test_async_writer_api(self):
         async def inner(httpd):
             rd, wr = await asyncio.open_connection(*httpd.address)
diff --git a/Misc/NEWS.d/next/Library/2019-12-05-16-13-25.bpo-38529.yvQgx3.rst b/Misc/NEWS.d/next/Library/2019-12-05-16-13-25.bpo-38529.yvQgx3.rst
new file mode 100644
index 0000000000000..c688926b4a49b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-05-16-13-25.bpo-38529.yvQgx3.rst
@@ -0,0 +1,2 @@
+Drop too noisy asyncio warning about deletion of a stream without explicit
+``.close()`` call.



More information about the Python-checkins mailing list