BaseHTTPMiddleware + unhandled exceptions cause ResourceWarning about unclosed anyio.streams.memory.MemoryObjectReceiveStream #2772
Unanswered
nikitagashkov
asked this question in
Potential Issue
Replies: 1 comment 4 replies
-
Would you try to open a PR to fix it? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello and thank you for your wonderful framework!
I'd like to start a discussion about something I've stumbled upon during testing a
starlette
-based application. We have a FastAPI application with a custom unhandled exception handler that we test in ourpytest
-based suiteAfter upgrading
anyio
to 4.4.0+ we started recieving failed suites due toResourceWarning
being raised fromanyio
(this PR introduced the warning — agronholm/anyio#715 — that've landed in 4.4.0). It seems like that there's nothing wrong in thatanyio
patchset in particular and it just highlighted the issue withinstarlette
I've sprinked
print
s andtracepack.print_stack()
s within our code to find the origin of the unclosed stream and traces lead to theBaseHTTPMiddleware
, specifically this line, allocating therecv_stream
: https://github.com/encode/starlette/blob/0.41.3/starlette/middleware/base.py#L112A quick research lead me to this block of code and it looks like it does not cleanup the
recv_stream
upon unhandled exceptions.I've come up with the script to reproduce the issue:
Running the following script raises the following issue:
Probably, this issue might cause warnings for some folks from this discussion as well: #2603
Thank you for reviewing this report!
Beta Was this translation helpful? Give feedback.
All reactions