-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-109538: Catch closed loop runtime error and issue warning #111983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
354780a
gh-109538: Catch closed loop runtime error and issue warning
dpr-0 0a1c4ef
📜🤖 Added by blurb_it.
blurb-it[bot] 7461e4e
Update Misc/NEWS.d/next/Library/2023-11-11-16-42-48.gh-issue-109538.c…
dpr-0 54106eb
use checking condition instead of try catch
dpr-0 b088f82
Add test
dpr-0 5eb3fa1
Make test more clear
dpr-0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -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)) | ||
|
@@ -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: | ||
pass | ||
Comment on lines
+1128
to
+1129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment at least showing why we expect |
||
finally: | ||
gc.collect() | ||
|
||
self.assertEqual(messages, []) | ||
|
||
def test_unhandled_exceptions(self) -> None: | ||
port = socket_helper.find_unused_port() | ||
|
||
|
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2023-11-11-16-42-48.gh-issue-109538.cMG5ux.rst
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__`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this exception is expected then why not use
self.assertRaises
etc?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
except
allows/tolerates an exception,assert
requires one and will fail if it does not happen because of other improvements.