fastapi: BrokenResourceError

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

app.py

import time
import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware


app = FastAPI()


@app.middleware("http")
async def middleware(request: Request, call_next):
    return await call_next(request)


@app.get("/")
def read_root():
    time.sleep(4)
    return {"Hello": "World"}


if __name__ == "__main__":
    uvicorn.run(app="app:app", port=8000)

script.py

import requests as requests

r = requests.get(f"http://127.0.0.1:8000", timeout=2)

Description

If you trigger script.py, error will appear. After upgrade fastapi to 0.70.0 In some requests in our application its apearing this error. So what we can do? What is problem?

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "venv/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 398, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "venv/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "venv/lib/python3.8/site-packages/fastapi/applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "venv/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "venv/lib/python3.8/site-packages/starlette/middleware/base.py", line 57, in __call__
    task_group.cancel_scope.cancel()
  File "venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 567, in __aexit__
    raise exceptions[0]
  File "venv/lib/python3.8/site-packages/starlette/middleware/base.py", line 30, in coro
    await self.app(scope, request.receive, send_stream.send)
  File "venv/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "venv/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "venv/lib/python3.8/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "venv/lib/python3.8/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "venv/lib/python3.8/site-packages/starlette/routing.py", line 64, in app
    await response(scope, receive, send)
  File "venv/lib/python3.8/site-packages/starlette/responses.py", line 139, in __call__
    await send({"type": "http.response.body", "body": self.body})
  File "venv/lib/python3.8/site-packages/starlette/exceptions.py", line 68, in sender
    await send(message)
  File "venv/lib/python3.8/site-packages/anyio/streams/memory.py", line 205, in send
    raise BrokenResourceError
anyio.BrokenResourceError

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.70.0

Python Version

3.8

Additional Context

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 15
  • Comments: 43 (3 by maintainers)

Commits related to this issue

Most upvoted comments

This is fixed in Starlette v0.17.0, hopefully FastAPI bumps starlette again on next release

Still experiencing this issue:

  • fastapi == 0.72.0
  • starlette == 0.17.1

Do you use BaseHTTPMiddleware somewhere in your code? Either directly like:

class CustomMiddleware(BaseHTTPMiddleware:
    pass

or indirectly like:

@app.middleware('http')
def middlewarefunc():
    pass

I found that most people above are experiencing this with that particular middleware, which actually uses MemoryStream of anyio.

I have been debugging and trying to replicate this, I used the original example:

import time
from fastapi import FastAPI, Request


app = FastAPI()


@app.middleware("http")
async def middleware(request: Request, call_next):
    return await call_next(request)


@app.get("/")
def read_root():
    time.sleep(4)
    return {"Hello": "World"}

With the script:

import requests

r = requests.get(f"http://127.0.0.1:8000", timeout=2)

This means that the server starts handling the request, and before returning the response, the client disconnects. I was only able to replicate the error in an old environment with starlette-0.15.0, but the error disappeared after upgrading FastAPI to the latest (currently 0.75.0) that includes Starlette version 0.17.1.


I also tried making the server start the response in a streaming response, and making the client close the connection before the server ends sending the response, e.g.:

import time

from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse

app = FastAPI()


@app.middleware("http")
async def middleware(request: Request, call_next):
    return await call_next(request)


@app.get("/stream")
def main():
    def stream():
        for i in range(10000):
            yield str(i)
            time.sleep(1)
            if i == 10:
                break

    return StreamingResponse(stream(), media_type="mp4/video")

…but still, that didn’t trigger the exception either.


If anyone is still experiencing this, please help me find a way to replicate your error. The versions of the packages alone don’t seem to be enough to replicate it with the examples above. If you have some particular example that breaks with the current versions and that can be replicated, that would be the best way to figure out what’s happening (if there’s anything still happening) and then solve it, thanks! 🙇

The issue is still relevant. We are running FastApi using uvicorn deployed on a k8s cluster in Digitalocean running 1.22.7-do.0. More information:

Operating System

Debian GNU/Linux 11 (bullseye) - container python:3.10.2-slim

FastAPI Version

0.75.0

Other libraries

anyio==3.5.0 ddtrace==0.59.1 starlette==0.17.1 uvicorn==0.17.6

Python Version

3.10.2

Traceback

Traceback (most recent call last):
  File "starlette/middleware/cors.py", line 84, in __call__
              await self.app(scope, receive, send)
  File "ddtrace/contrib/asgi/middleware.py", line 178, in __call__
              reraise(exc_type, exc_val, exc_tb)
  File "six.py", line 719, in reraise
              raise value
  File "ddtrace/contrib/asgi/middleware.py", line 173, in __call__
              return await self.app(scope, receive, wrapped_send)
  File "starlette/exceptions.py", line 82, in __call__
                  raise exc
  File "starlette/exceptions.py", line 71, in __call__
              await self.app(scope, receive, sender)
  File "fastapi/middleware/asyncexitstack.py", line 21, in __call__
                      raise e
  File "fastapi/middleware/asyncexitstack.py", line 18, in __call__
                      await self.app(scope, receive, send)
  File "starlette/routing.py", line 656, in __call__
                  await route.handle(scope, receive, send)
  File "starlette/routing.py", line 259, in handle
              await self.app(scope, receive, send)
  File "starlette/routing.py", line 64, in app
          await response(scope, receive, send)
  File "starlette/responses.py", line 156, in __call__
          await send({"type": "http.response.body", "body": self.body})
  File "starlette/exceptions.py", line 68, in sender
              await send(message)
  File "ddtrace/contrib/asgi/middleware.py", line 170, in wrapped_send
              return await send(message)
  File "anyio/streams/memory.py", line 205, in send
                  raise BrokenResourceError
anyio.BrokenResourceError

Traceback (most recent call last):
  File "anyio/streams/memory.py", line 193, in send
              self.send_nowait(item)
  File "anyio/streams/memory.py", line 186, in send_nowait
              raise WouldBlock
anyio.WouldBlock

Running the app outside of a docker container on a stand-alone Ubuntu 20.04 LTS server we never experienced this issue.

Seeing this issue on EKS with plenty of resources. We are using ddtrace though I doubt this is causing the issue. We are running using uvicorn as our ASGI server.

deps

ddtrace==0.57.3
fastapi==0.73.0
starlette==0.17.1
uvicorn==0.17.4

full stack trace

anyio.BrokenResourceError

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 193, in send
    self.send_nowait(item)
  File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 186, in send_nowait
    raise WouldBlock
anyio.WouldBlock

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/ddtrace/contrib/asgi/middleware.py", line 173, in __call__
    return await self.app(scope, receive, wrapped_send)
  File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 64, in app
    await response(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/responses.py", line 156, in __call__
    await send({"type": "http.response.body", "body": self.body})
  File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 68, in sender
    await send(message)
  File "/usr/local/lib/python3.9/site-packages/ddtrace/contrib/asgi/middleware.py", line 170, in wrapped_send
    return await send(message)
  File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 205, in send
    raise BrokenResourceError

anyio.BrokenResourceError

App invocation

CMD ["ddtrace-run", "uvicorn", "app:app"]

In case it helps others, We ran into this issue when running in k8s with low memory/cpu limits.

i catch same error with fastapi==0.70. is there any progress?

@tiangolo I have a set of files that will reliably, and quickly reproduce an AnyIO stream error when using ContextMiddleware, but don’t when ContextMiddleware is removed.

The stack trace is not the same as the ones above – this test setup raises anyio.EndOfStream, but it involves many of the same same modules as the anyio.BrokenResourceError staclktraces we’ve been seeing, and seems plausibly related. I put this together because we’ve been having the same anyio.BrokenResourceError errors in production, using fastapi 0.75.2.

Could this issue be reopened, as there seems to be a number of other people who are also running into this?

I’ve catched the same error with fastapi==0.70 When fastapi configured through gunicorn with uvicorn worker and behind nginx with proxy_pass upstream over unix socket. In such configuration when xhr request becomes canceled (in browser) throws BrokenResourceError Canceled requests

We switched our CorrelationIdPlugin to use RawContextMiddleware (which doesn’t inherit from the starlette baseMiddleware) and deployed that to prod yesterday at 4pm. We haven’t had any BrokenResourceErrors since:

app.add_middleware(RawContextMiddleware, plugins=[plugins.CorrelationIdPlugin()])

We have a different service which has never run into this issue, and it sets up middleware like so:


middleware = [
    Middleware(RawContextMiddleware),
    Middleware(ContextMiddleware, plugins=(plugins.CorrelationIdPlugin(),)),
    Middleware(CORSMiddleware...)
]

More of a workaround than a fix (what the heck is RawContextMiddleware doing? How is it different from starlette baseMiddleware? Is this bad? Who knows! I looked at the source for both very briefly and decided that things were not dire enough that I needed to go down those rabbit holes right now.] However, it has silenced the BrokenResourceErrors coming out of our /health endpoint since 4pm yesterday, and all our tests still pass and no one has yelled at us yet so… cool?

I just got this issue as well for the first time, with fastapi 0.75.0. I’m running on a kubernetes cluster from the most recent python 3.9 image.

Maybe it’s related : this issue occured only a few days after I set up sentry, with its ASGI middleware : https://docs.sentry.io/platforms/python/guides/asgi/

Here is how I integrated it in FastAPI :

from sentry_sdk.integrations.asgi import SentryAsgiMiddleware


alembic.check_revisions()
app = FastAPI(version="1.1.0")

sentry_url = os.environ.get("SENTRY_URL")
if sentry_url:

    def strip_paths(sampling_context):
        path = sampling_context["asgi_scope"]["path"]
        if path in ["/healthz"]:
            return 0.0
        else:
            return 1.0

    sentry_sdk.init(
        sentry_url,
        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        # traces_sample_rate=0.,
        traces_sampler=strip_paths,
        # before_send=strip_paths,
    )
    app.add_middleware(SentryAsgiMiddleware)

By the way, the exception occured in the /healthz endpoint, that I’m filtering out of sentry. It’s quite rare, as Kubernetes makes tens of thousands of calls to this endpoint per day. The exception occured only once so far.

I have the same problem, this is my code:

main.py

# -*- coding: utf-8 -*-


from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from pony.orm import *

from models_pony.name_basics import NameBasics as pn_name_basics

api = FastAPI() 


@api.middleware('http')
async def process_request(request: Request, call_next):
    return await call_next(request)


@api.get('/namebasics/id/{id}')
async def get_namebasic_by_id(id:int):
    with db_session:
         result = (pn_name_basics[id]).to_dict()
    return JSONResponse(result)



@api.get('/namebasics/ncost/{ncost}')
async def get_namebasic_by_ncost(ncost:str):
    with db_session:
        result = (pn_name_basics.get(ncost=ncost)).to_dict()
    return JSONResponse(result)



@api.get('/namebasics/year/{year}')
async def get_namebasic_by_year(year:str):
    with db_session:
        results = select(p for p in pn_name_basics if p.birth_year == year)[:]
        _results = [i.to_dict() for i in results]
        _results = jsonable_encoder(_results)
    return JSONResponse(_results)



@api.get('/namebasics/yearalive/{yearalive}')
async def get_namebasic_by_yearalive(yearalive:str):
    with db_session:
        results = select(p for p in pn_name_basics if p.birth_year == yearalive and p.death_year is None)[:]
        _results = [i.to_dict() for i in results]
        _results = jsonable_encoder(_results)
    return JSONResponse(_results)

Command terminal test

wrk --duration 20s --threads 2 --connections 5 http://0.0.0.0:8000/namebasics/id/1024
Running 20s test @ http://0.0.0.0:8000/namebasics/id/1024
  2 threads and 5 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     8.54ms    9.84ms 112.06ms   91.46%
    Req/Sec   289.56    154.88   565.00     59.00%
  11538 requests in 20.05s, 3.66MB read
Requests/sec:    575.56
Transfer/sec:    187.17KB

Error

[2021-10-20 01:50:45 +0000] [5117] [ERROR] Exception in ASGI application
Traceback (most recent call last):
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/uvicorn/protocols/http/httptools_impl.py", line 375, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/fastapi/applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/base.py", line 57, in __call__
    task_group.cancel_scope.cancel()
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/_backends/_asyncio.py", line 567, in __aexit__
    raise exceptions[0]
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/_backends/_asyncio.py", line 604, in _run_wrapped_task
    await coro
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/base.py", line 30, in coro
    await self.app(scope, request.receive, send_stream.send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 64, in app
    await response(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/responses.py", line 139, in __call__
    await send({"type": "http.response.body", "body": self.body})
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 68, in sender
    await send(message)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/streams/memory.py", line 205, in send
    raise BrokenResourceError
anyio.BrokenResourceError
[2021-10-20 01:50:45 +0000] [5117] [ERROR] Exception in ASGI application
Traceback (most recent call last):
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/uvicorn/protocols/http/httptools_impl.py", line 375, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/fastapi/applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/base.py", line 57, in __call__
    task_group.cancel_scope.cancel()
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/_backends/_asyncio.py", line 567, in __aexit__
    raise exceptions[0]
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/_backends/_asyncio.py", line 604, in _run_wrapped_task
    await coro
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/base.py", line 30, in coro
    await self.app(scope, request.receive, send_stream.send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 64, in app
    await response(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/responses.py", line 139, in __call__
    await send({"type": "http.response.body", "body": self.body})
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 68, in sender
    await send(message)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/streams/memory.py", line 205, in send
    raise BrokenResourceError
anyio.BrokenResourceError
[2021-10-20 01:50:45 +0000] [5116] [ERROR] Exception in ASGI application
Traceback (most recent call last):
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/uvicorn/protocols/http/httptools_impl.py", line 375, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/fastapi/applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/base.py", line 57, in __call__
    task_group.cancel_scope.cancel()
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/_backends/_asyncio.py", line 567, in __aexit__
    raise exceptions[0]
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/_backends/_asyncio.py", line 604, in _run_wrapped_task
    await coro
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/base.py", line 30, in coro
    await self.app(scope, request.receive, send_stream.send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 64, in app
    await response(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/responses.py", line 139, in __call__
    await send({"type": "http.response.body", "body": self.body})
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 68, in sender
    await send(message)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/streams/memory.py", line 205, in send
    raise BrokenResourceError
anyio.BrokenResourceError
[2021-10-20 01:50:45 +0000] [5116] [ERROR] Exception in ASGI application
Traceback (most recent call last):
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/uvicorn/protocols/http/httptools_impl.py", line 375, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/fastapi/applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/base.py", line 57, in __call__
    task_group.cancel_scope.cancel()
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/_backends/_asyncio.py", line 567, in __aexit__
    raise exceptions[0]
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/_backends/_asyncio.py", line 604, in _run_wrapped_task
    await coro
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/middleware/base.py", line 30, in coro
    await self.app(scope, request.receive, send_stream.send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/routing.py", line 64, in app
    await response(scope, receive, send)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/responses.py", line 139, in __call__
    await send({"type": "http.response.body", "body": self.body})
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/starlette/exceptions.py", line 68, in sender
    await send(message)
  File "/lxcshared/benchmark.git/Python/src/.venv/site-packages/anyio/streams/memory.py", line 205, in send
    raise BrokenResourceError
anyio.BrokenResourceError

Something that strikes me is that the application does not crash. The error is not displayed if I remove the middleware like issues encode/starlette/issues/1284 and the performance of the requests is better.

Test 1

wrk --duration 20s --threads 2 --connections 5 http://0.0.0.0:8000/namebasics/id/1024
Running 20s test @ http://0.0.0.0:8000/namebasics/id/1024
  2 threads and 5 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.42ms    5.01ms  90.24ms   92.50%
    Req/Sec   807.40    461.95     1.66k    54.25%
  32149 requests in 20.02s, 10.21MB read
Requests/sec:   1606.09
Transfer/sec:    522.29KB

Test 2

wrk --duration 20s --threads 2 --connections 5 http://0.0.0.0:8000/namebasics/id/1024
Running 20s test @ http://0.0.0.0:8000/namebasics/id/1024
  2 threads and 5 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.33ms    4.77ms  60.69ms   95.11%
    Req/Sec     1.34k   326.30     1.85k    87.75%
  53350 requests in 20.00s, 16.94MB read
Requests/sec:   2666.88
Transfer/sec:    867.27KB

Operating System

Linux

Operating System Details

lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

FastAPI Version

0.70.0

Python Version

3.6.12 pypy 7.3.3 with GCC 10.2.1

Additional Context

Web server: gunicorn 20.1.0 with worker class uvicorn.workers.UvicornWorker

We just discovered that replacing ContextMiddleware with RawContextMiddleware in my test fast_app.py makes the anyio.EndOfStream errors go away.

I have tested FastAPI v0.71.0 in my project and the BrokenResourceError is fixed.

This is fixed in Starlette v0.17.0, hopefully FastAPI bumps starlette again on next release

I think this can be closed now that fastapi 0.71.0 bumped Starlette to 0.17.1 .

@ixuuux I think hide the error message it isn’t recommended, definitely something wrong happening and this could produce unexpected behaviors, so I do think it’s dangerous IMHO.