]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-109538: Catch closed loop runtime error and issue warning (GH-111983) ...
authorDPR <dpr-0xff@pm.me>
Thu, 16 Nov 2023 17:40:55 +0000 (01:40 +0800)
committerGitHub <noreply@github.com>
Thu, 16 Nov 2023 17:40:55 +0000 (09:40 -0800)
* [3.12] gh-109538: Avoid RuntimeError when StreamWriter is deleted with closed loop (GH-111983)

Issue a ResourceWarning instead.

(cherry picked from commit e0f512797596282bff63260f8102592aad37cdf1)
gh-109538: Avoid RuntimeError when StreamWriter is deleted with closed loop (#111983)

Issue a ResourceWarning instead.

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
(cherry picked from commit e0f512797596282bff63260f8102592aad37cdf1)

* Fix missing warnings import

Lib/asyncio/streams.py
Lib/test/test_asyncio/test_streams.py
Misc/NEWS.d/next/Library/2023-11-11-16-42-48.gh-issue-109538.cMG5ux.rst [new file with mode: 0644]

index f63eeca2a7719a592dae9a0700093ee8f0cb9a26..f5695266e1a8c635d270304b67d614bbe72fa6f3 100644 (file)
@@ -5,6 +5,7 @@ __all__ = (
 import collections
 import socket
 import sys
+import warnings
 import weakref
 
 if hasattr(socket, 'AF_UNIX'):
@@ -405,8 +406,11 @@ class StreamWriter:
 
     def __del__(self):
         if not self._transport.is_closing():
-            self.close()
-
+            if self._loop.is_closed():
+                warnings.warn("loop is closed", ResourceWarning)
+            else:
+                self.close()
+                warnings.warn(f"unclosed {self!r}", ResourceWarning)
 
 class StreamReader:
 
index 5a22232c00a36a0777de21142108470a66cf4016..ccb7dbf667c3202f56983edcfc594d950b2b3560 100644 (file)
@@ -1073,6 +1073,65 @@ os.close(fd)
 
         self.assertEqual(messages, [])
 
+    def test_unclosed_resource_warnings(self):
+        async def inner(httpd):
+            rd, wr = await asyncio.open_connection(*httpd.address)
+
+            wr.write(b'GET / HTTP/1.0\r\n\r\n')
+            data = await rd.readline()
+            self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
+            data = await rd.read()
+            self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
+            with self.assertWarns(ResourceWarning) as cm:
+                del wr
+                gc.collect()
+                self.assertEqual(len(cm.warnings), 1)
+                self.assertTrue(str(cm.warnings[0].message).startswith("unclosed <StreamWriter"))
+
+        messages = []
+        self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
+
+        with test_utils.run_test_server() as httpd:
+            self.loop.run_until_complete(inner(httpd))
+
+        self.assertEqual(messages, [])
+
+    def test_loop_is_closed_resource_warnings(self):
+        async def inner(httpd):
+            rd, wr = await asyncio.open_connection(*httpd.address)
+
+            wr.write(b'GET / HTTP/1.0\r\n\r\n')
+            data = await rd.readline()
+            self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
+            data = await rd.read()
+            self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
+
+            # Make "loop is closed" occur first before "del wr" for this test.
+            self.loop.stop()
+            wr.close()
+            while not self.loop.is_closed():
+                await asyncio.sleep(0.0)
+
+            with self.assertWarns(ResourceWarning) as cm:
+                del wr
+                gc.collect()
+                self.assertEqual(len(cm.warnings), 1)
+                self.assertEqual("loop is closed", str(cm.warnings[0].message))
+
+        messages = []
+        self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
+
+        with test_utils.run_test_server() as httpd:
+            try:
+                self.loop.run_until_complete(inner(httpd))
+            # This exception is caused by `self.loop.stop()` as expected.
+            except RuntimeError:
+                pass
+            finally:
+                gc.collect()
+
+        self.assertEqual(messages, [])
+
     def test_unhandled_exceptions(self) -> None:
         port = socket_helper.find_unused_port()
 
diff --git a/Misc/NEWS.d/next/Library/2023-11-11-16-42-48.gh-issue-109538.cMG5ux.rst b/Misc/NEWS.d/next/Library/2023-11-11-16-42-48.gh-issue-109538.cMG5ux.rst
new file mode 100644 (file)
index 0000000..d1ee4c0
--- /dev/null
@@ -0,0 +1 @@
+Issue warning message instead of having :class:`RuntimeError` be displayed when event loop has already been closed at :meth:`StreamWriter.__del__`.