Skip to content

Connection reset after exception raised & handled in FastAPI #2285

Discussion options

You must be logged in to vote

After taking another look today, I actually think I figured this out. It turns out, Starlette treats exception handlers that handle Exception differently than handlers that handle more specific exceptions:

https://github.com/encode/starlette/blob/89fae174a1ea10f59ae248fe030d9b7e83d0b8a0/starlette/applications.py#L92-L92

For exception handlers that handle Exception, it uses a ServerErrorMiddleware class, which at the end of handling the exception, re-raises it to the Uvicorn layer:

https://github.com/encode/starlette/blob/89fae174a1ea10f59ae248fe030d9b7e83d0b8a0/starlette/middleware/errors.py#L186-L186

Then, Uvicorn closes the client connection:

Replies: 1 comment 17 replies

Comment options

Kludex
Mar 24, 2024
Maintainer Sponsor

You must be logged in to vote
17 replies
@Kludex
Comment options

Kludex Mar 24, 2024
Maintainer Sponsor

@mcgrawia
Comment options

@mcgrawia
Comment options

@mcgrawia
Comment options

Answer selected by tomchristie
@tomchristie
Comment options

@mcgrawia
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants