-
-
Notifications
You must be signed in to change notification settings - Fork 917
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
anyio.BrokenResourceError when using BaseHTTPMiddleware #1284
Comments
It is not a Starlette issue — client abort connection immediatly after sending request, and Starlette has to write to closed socket. This should work: from http.client import HTTPConnection
def ping(x):
con = HTTPConnection('0.0.0.0', 8000)
con.request('GET', f'/ping?id={x}')
con.getresponse()
con.close()
z = [
ping(y)
for y in range(10)
]
|
If you remove the Middleware, or add other Middleware such as |
ASGI specifies that send is a no-op when the connection is closed. HTTPMiddleware uses anyio streams which will raise an exception when the connection is closed. So we need to ignore the exception in our send function. Fixes encode#1284
ASGI specifies that send is a no-op when the connection is closed. HTTPMiddleware uses anyio streams which will raise an exception when the connection is closed prematurely. So we need to ignore the exception in our send function. Fixes encode#1284
ASGI specifies that send is a no-op when the connection is closed. HTTPMiddleware uses anyio streams which will raise an exception when the connection is closed prematurely. So we need to ignore the exception in our send function. Fixes encode#1284
ASGI specifies that send is a no-op when the connection is closed. HTTPMiddleware uses anyio streams which will raise an exception when the connection is closed prematurely. So we need to ignore the exception in our send function. Fixes encode#1284
ASGI specifies that send is a no-op when the connection is closed. HTTPMiddleware uses anyio streams which will raise an exception when the connection is closed prematurely. So we need to ignore the exception in our send function. Fixes encode#1284
ASGI specifies that send is a no-op when the connection is closed. HTTPMiddleware uses anyio streams which will raise an exception when the connection is closed prematurely. So we need to ignore the exception in our send function. Fixes encode#1284
I can't reproduce it on master after #1262 was merged. Can someone confirm the issue was solved? |
I was not able to reproduce on master branch either. |
Since I can't reproduce this anymore, I'm going to close it. |
I'm still seeing this in my sentry logs. It seems to occur when the server is overloaded, and the client times out / dies. I managed to reproduce it with a slightly modified server app:
and this stressor script:
Running Traceback for local example
but in my production application I'm still seeing the
Traceback I see in production
According to Sentry, the state of
(I'm guessing this is just after Maybe this is AnyIO's problem at this point? |
@xkortex I am seeing same error in production environment, have you solved the issue? I tried to reproduce the issue on local machine with your bash script, but unfortunately that didn't work |
Checklist
master
.Describe the bug
An anyio.BrokenResourceError is observed when using a very simple custom Middleware base on BaseHTTPMiddleware.
To reproduce
Server
Client
Expected behavior
No exceptions, responses returned
Actual behavior
An anyio.BrokenResourceError is raised
Debugging material
Traceback
``` ERROR: Exception in ASGI application Traceback (most recent call last): File "/home/plaid/py39/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 371, in run_asgi result = await app(self.scope, self.receive, self.send) File "/home/plaid/py39/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 59, in __call__ return await self.app(scope, receive, send) File "/home/plaid/py39/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__ await self.middleware_stack(scope, receive, send) File "/home/plaid/py39/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__ raise exc File "/home/plaid/py39/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__ await self.app(scope, receive, _send) File "/home/plaid/py39/lib/python3.9/site-packages/starlette/middleware/base.py", line 57, in __call__ task_group.cancel_scope.cancel() File "/home/plaid/py39/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 564, in __aexit__ raise exceptions[0] File "/home/plaid/py39/lib/python3.9/site-packages/starlette/middleware/base.py", line 30, in coro await self.app(scope, request.receive, send_stream.send) File "/home/plaid/py39/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__ raise exc File "/home/plaid/py39/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__ await self.app(scope, receive, sender) File "/home/plaid/py39/lib/python3.9/site-packages/starlette/routing.py", line 656, in __call__ await route.handle(scope, receive, send) File "/home/plaid/py39/lib/python3.9/site-packages/starlette/routing.py", line 259, in handle await self.app(scope, receive, send) File "/home/plaid/py39/lib/python3.9/site-packages/starlette/routing.py", line 64, in app await response(scope, receive, send) File "/home/plaid/py39/lib/python3.9/site-packages/starlette/responses.py", line 139, in __call__ await send({"type": "http.response.body", "body": self.body}) File "/home/plaid/py39/lib/python3.9/site-packages/starlette/exceptions.py", line 68, in sender await send(message) File "/home/plaid/py39/lib/python3.9/site-packages/anyio/streams/memory.py", line 205, in send raise BrokenResourceError anyio.BrokenResourceError ```Environment
18.04
3.9.6
0.16.0
The text was updated successfully, but these errors were encountered: