[Python-checkins] gh-90467: StreamReaderProtocol - add strong reference to created task (#96323)

gvanrossum webhook-mailer at python.org
Sat Aug 27 15:32:21 EDT 2022


https://github.com/python/cpython/commit/e860e521ec0d84e175269aeb15cf24bd6053ad17
commit: e860e521ec0d84e175269aeb15cf24bd6053ad17
branch: main
author: Kirill <iam at python273.pw>
committer: gvanrossum <gvanrossum at gmail.com>
date: 2022-08-27T12:32:01-07:00
summary:

gh-90467: StreamReaderProtocol - add strong reference to created task (#96323)

files:
A Misc/NEWS.d/next/Library/2022-08-27-14-38-49.gh-issue-90467.VOOB0p.rst
M Lib/asyncio/streams.py
M Misc/ACKS

diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py
index 9a169035de88..614b2cda6068 100644
--- a/Lib/asyncio/streams.py
+++ b/Lib/asyncio/streams.py
@@ -205,6 +205,7 @@ def __init__(self, stream_reader, client_connected_cb=None, loop=None):
             self._strong_reader = stream_reader
         self._reject_connection = False
         self._stream_writer = None
+        self._task = None
         self._transport = None
         self._client_connected_cb = client_connected_cb
         self._over_ssl = False
@@ -247,7 +248,7 @@ def connection_made(self, transport):
             res = self._client_connected_cb(reader,
                                             self._stream_writer)
             if coroutines.iscoroutine(res):
-                self._loop.create_task(res)
+                self._task = self._loop.create_task(res)
             self._strong_reader = None
 
     def connection_lost(self, exc):
@@ -265,6 +266,7 @@ def connection_lost(self, exc):
         super().connection_lost(exc)
         self._stream_reader_wr = None
         self._stream_writer = None
+        self._task = None
         self._transport = None
 
     def data_received(self, data):
diff --git a/Misc/ACKS b/Misc/ACKS
index daa01c1a0f1d..0edea9219d05 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1442,6 +1442,7 @@ Ram Rachum
 Jeffrey Rackauckas
 Jérôme Radix
 Burton Radons
+Kirill (python273) R.
 Abhilash Raj
 Shorya Raj
 Ajith Ramachandran
@@ -1987,6 +1988,7 @@ Gordon Worley
 Darren Worrall
 Thomas Wouters
 Daniel Wozniak
+Simon Wrede
 Marcin Niemira
 Wei Wu
 Heiko Wundram
diff --git a/Misc/NEWS.d/next/Library/2022-08-27-14-38-49.gh-issue-90467.VOOB0p.rst b/Misc/NEWS.d/next/Library/2022-08-27-14-38-49.gh-issue-90467.VOOB0p.rst
new file mode 100644
index 000000000000..282c0e76a8c8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-08-27-14-38-49.gh-issue-90467.VOOB0p.rst
@@ -0,0 +1,2 @@
+Fix :class:`asyncio.streams.StreamReaderProtocol` to keep a strong reference
+to the created task, so that it's not garbage collected



More information about the Python-checkins mailing list