FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-109538: Catch closed loop runtime error and issue warning by dpr-0 · Pull Request #111983 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 5 additions & 3 deletions Lib/asyncio/streams.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,11 @@ async def start_tls(self, sslcontext, *,

def __del__(self, warnings=warnings):
if not self._transport.is_closing():
self.close()
warnings.warn(f"unclosed {self!r}", ResourceWarning)

if self._loop.is_closed():
warnings.warn("loop is closed", ResourceWarning)
else:
self.close()
warnings.warn(f"unclosed {self!r}", ResourceWarning)

class StreamReader:

Expand Down
41 changes: 39 additions & 2 deletions Lib/test/test_asyncio/test_streams.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,11 @@ async def inner(httpd):
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):
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))
Expand All @@ -1095,6 +1096,42 @@ async def 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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If this exception is expected then why not use self.assertRaises etc?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

except allows/tolerates an exception, assert requires one and will fail if it does not happen because of other improvements.

pass
Comment on lines +1128 to +1129

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Add a comment at least showing why we expectRuntimeError here.

finally:
gc.collect()

self.assertEqual(messages, [])

def test_unhandled_exceptions(self) -> None:
port = socket_helper.find_unused_port()

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Issue warning message instead of having :class:`RuntimeError` be displayed when event loop has already been closed at :meth:`StreamWriter.__del__`.

Back | FazBrowse Home | New Git URL