Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

db session middleware with custom exception middleware raises AnyIO EndOfSteam when client closes early #4544

Closed
@s-quinn

Description

@s-quinn

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp