Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.7k
Closed
Description
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 toPydantic.
- I already checked if it is not related to FastAPI but toSwagger UI.
- I already checked if it is not related to FastAPI but toReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
importtimefromtypingimportCallable,AwaitableimportuvicornfromfastapiimportFastAPIfromsqlalchemyimportcreate_enginefromsqlalchemy.ormimportsessionmaker,declarative_basefromstarlette.requestsimportRequestfromstarlette.responsesimportResponseSQLALCHEMY_DATABASE_URL="postgresql://user:password@postgresserver/db"engine=create_engine(SQLALCHEMY_DATABASE_URL)SessionLocal=sessionmaker(autocommit=False,autoflush=False,bind=engine)Base=declarative_base(bind=engine)app=FastAPI()classExceptionHandlerMiddleware:asyncdef__call__(self,request:Request,call_next:Callable[[Request],Awaitable[Response]], )->Response:try:returnawaitcall_next(request)exceptExceptionasexc:print(exc)app.middleware('http')(ExceptionHandlerMiddleware())@app.middleware("http")asyncdefdb_session_middleware(request:Request,call_next):response=Response("Internal server error",status_code=500)try:request.state.db=SessionLocal()response=awaitcall_next(request)finally:request.state.db.close()returnresponse@app.get('/')deftest():time.sleep(2)return {'message':'hello'}if__name__=='__main__':uvicorn.run('example:app',reload=True, )
Description
We are using the db session middleware exactly as describedhere in combination with a custom http exception middleware. When a client is disconnecting from us early we get spammed with a bunch ofNo response returned errors from starlette.
How to reproduce:
Open browser and call the endpoint/
Close the window before the request completes.
You will see that AnyIO raises aEndOfStream error which ends up being propogated as aRuntime Error with the message asNo response returned
Operating System
Linux, macOS
Operating System Details
No response
FastAPI Version
0.73.0
Python Version
Python 3.9.6
Additional Context
SQLAlchemy==1.4.31