How can I cancel the request handler gracefully when a client disconnects? #8805
Replies: 6 comments 1 reply
-
I am stuck on this at the moment. I tested the above code with sleep time higher than 1 and found out it is the same issue, cancellation was not sent to the sleep function. |
Beta Was this translation helpful? Give feedback.
-
Hello @bolajiwahab, I have created a small repo with a possible workaround: https://github.com/RedRoserade/fastapi-disconnect-example In short, what it does is poll for |
Beta Was this translation helpful? Give feedback.
-
Thank you @RedRoserade I will check it out and revert. |
Beta Was this translation helpful? Give feedback.
-
+1 for this being a FastAPI feature that needs to be implemented. CPU polling isn't the right way to do this. |
Beta Was this translation helpful? Give feedback.
-
Came looking for answers to this too. I tried to figure out a more elegant way using the ASGI These methods work and don't require polling but the big issue is that they will consume the entire request body trying to detect the client disconnection. So they definitely won't work if you are trying to combine them with any kind of streaming handler. They possibly could work if you go ahead and read the request body yourself ahead of time before calling any of the Trying to get answers here encode/starlette#2215 and here encode/uvicorn#2046 Hope this helps someone |
Beta Was this translation helpful? Give feedback.
-
I wrote middleware for exact same purpose May this be of help? #11360 |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
Reproduction steps:
I would expect the
except
handler to run, therefore printingcancelled
. However, this is not the case. The handler will only be called when I useCtrl+C
to stop the app.Operating System
Linux
Operating System Details
No response
FastAPI Version
0.68.1
Python Version
Python 3.9.6
Additional Context
This question arises because we have some APIs, written with aiohttp, which can do some long-running compute. When the client times out, the coroutine of the handler gets cancelled. The following snippet, for aiohttp, which should be equivalent, does what I expect, and prints "cancelled" when the tab on the browser is closed.
Furthermore, I see that the Starlette
Request
class has a method,async def is_disconnected() -> bool
, which can be used for this.Indeed, if I modify the minimal example to do something like this:
Then indeed, I can catch the disconnect, and stop any potentially long-running work. It is a workaround, though.
So, the question is: Is there a configuration switch, or something equivalent, which would enable this behaviour? Are there drawbacks I am missing regarding such an approach?
Beta Was this translation helpful? Give feedback.
All reactions