From 385390a35e44f4c30f5a9147e30f0a2b1d7f5668 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 5 Aug 2023 17:48:15 +0530 Subject: [PATCH 1/3] GH-106684: raise `ResourceWarning` when `asyncio.StreamWriter` is not closed (GH-107650) (cherry picked from commit 41178e41995992bbe417f94bce158de93f9e3188) Co-authored-by: Kumar Aditya --- Lib/asyncio/streams.py | 6 +++++ Lib/test/test_asyncio/test_streams.py | 23 +++++++++++++++++++ ...-08-05-05-10-41.gh-issue-106684.P9zRXb.rst | 1 + 3 files changed, 30 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-08-05-05-10-41.gh-issue-106684.P9zRXb.rst diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py index bf15f517e50dce..b7ad365709b19e 100644 --- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -5,6 +5,7 @@ import collections import socket import sys +import warnings import weakref if hasattr(socket, 'AF_UNIX'): @@ -392,6 +393,11 @@ async def start_tls(self, sslcontext, *, self._transport = new_transport protocol._replace_writer(self) + def __del__(self, warnings=warnings): + if not self._transport.is_closing(): + self.close() + warnings.warn(f"unclosed {self!r}", ResourceWarning) + class StreamReader: diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 7f9dc621808358..9c92e75886c593 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -1074,6 +1074,29 @@ def test_eof_feed_when_closing_writer(self): 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): + del wr + gc.collect() + + + 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, []) + + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2023-08-05-05-10-41.gh-issue-106684.P9zRXb.rst b/Misc/NEWS.d/next/Library/2023-08-05-05-10-41.gh-issue-106684.P9zRXb.rst new file mode 100644 index 00000000000000..02c52d714e9df7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-08-05-05-10-41.gh-issue-106684.P9zRXb.rst @@ -0,0 +1 @@ +Raise :exc:`ResourceWarning` when :class:`asyncio.StreamWriter` is not closed leading to memory leaks. Patch by Kumar Aditya. From 02429ce78c75d97d80a426bbead406e28525f48e Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Thu, 10 Aug 2023 06:01:20 +0000 Subject: [PATCH 2/3] remove warning --- Lib/asyncio/streams.py | 4 +--- .../Library/2023-08-05-05-10-41.gh-issue-106684.P9zRXb.rst | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py index b7ad365709b19e..14861dffce3a84 100644 --- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -5,7 +5,6 @@ import collections import socket import sys -import warnings import weakref if hasattr(socket, 'AF_UNIX'): @@ -393,10 +392,9 @@ async def start_tls(self, sslcontext, *, self._transport = new_transport protocol._replace_writer(self) - def __del__(self, warnings=warnings): + def __del__(self): if not self._transport.is_closing(): self.close() - warnings.warn(f"unclosed {self!r}", ResourceWarning) class StreamReader: diff --git a/Misc/NEWS.d/next/Library/2023-08-05-05-10-41.gh-issue-106684.P9zRXb.rst b/Misc/NEWS.d/next/Library/2023-08-05-05-10-41.gh-issue-106684.P9zRXb.rst index 02c52d714e9df7..85bce76229853f 100644 --- a/Misc/NEWS.d/next/Library/2023-08-05-05-10-41.gh-issue-106684.P9zRXb.rst +++ b/Misc/NEWS.d/next/Library/2023-08-05-05-10-41.gh-issue-106684.P9zRXb.rst @@ -1 +1 @@ -Raise :exc:`ResourceWarning` when :class:`asyncio.StreamWriter` is not closed leading to memory leaks. Patch by Kumar Aditya. +Close :class:`asyncio.StreamWriter` when it is not closed by application leading to memory leaks. Patch by Kumar Aditya. From f299eac2f98045abc03d71d68b9cca14b485a961 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Thu, 10 Aug 2023 11:37:09 +0530 Subject: [PATCH 3/3] remove test --- Lib/test/test_asyncio/test_streams.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 9c92e75886c593..7f9dc621808358 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -1074,29 +1074,6 @@ def test_eof_feed_when_closing_writer(self): 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): - del wr - gc.collect() - - - 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, []) - - if __name__ == '__main__': unittest.main() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy