-
-
Notifications
You must be signed in to change notification settings - Fork 962
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
Fix 'ResourceWarning's about unclosed 'anyio.streams.memory.MemoryObjectReceiveStream' #2777
Fix 'ResourceWarning's about unclosed 'anyio.streams.memory.MemoryObjectReceiveStream' #2777
Conversation
0203498
to
241c4bc
Compare
tests/middleware/test_base.py
Outdated
# BaseHTTPMiddleware creates a TaskGroup which copies the context | ||
# and erases any changes to it made within the TaskGroup | ||
assert ctxvar.get() == "set by middleware" |
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.
Well, yeah, that's why the pytest.mark.xfail
is there... 🤔
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.
xfail
complicates testing for a warning, which is why I had to make adjustments.
An AssertionError
within the middleware triggers the same ResourceWarning
(refer to issue #2778). However, since the warning is emitted only after the stream is deleted, it appears some time later, specifically after an explicit gc.collect()
in test_anyio_streams_cleanup_exc_in_route
. This causes the test to fail due to a leftover stream
from another test.
Perhaps there's no need for this test at all, as it is currently quite brittle. 🤷♂️ It might be better to stop ignoring the warning from anyio once all streams are properly closed.
I'm very confused about the comments in this PR. I'll review the other PR. Adding tons of comments in unrelated code is not helpful. |
I believe comments are useful for highlighting certain parts of the code, but I have removed them as requested. Additionally, I made the patchset smaller by removing fixes for |
Let's continue on the other PR. |
Fixes #2772.
Summary
This PR addresses the cleanup of memory object streams, particularly focusing on ensuring that streams are properly closed to prevent resource warnings:
recv_stream
inBaseHTTPMiddleware
upon exception within the route to prevent dangling streams.Checklist
↑ Looks like no changes in docs needed, this is just a bugfix.