You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you have a websocket endpoint which uses a dependency that holds some resource for the time of execution, this can lead to problems. Maybe this dependency itself does not acquire a resource, but it depends on another dependency which acquires a resource - so it's easy not to notice.
I propose to add a clarification to this page that you must not generally use dependencies with yield in a websocket endpoint.
I faced this problem myself:
Added websocket endpoint to my REST application. In this websocket endpoint I reused a dependency to authenticate users. This dependency requires DB session dependency. So, DB connection is acquired when client connects to ws and it's held until client disconnects from ws. But it's actually only needed in the beginning. I did not notice it, and because of this my app started having huge delays for both HTTP and websocket endpoints when a certain number of connected clients is reached. It was hard to debug.
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.110.0
Pydantic Version
2.6.2
Python Version
3.11.7
Additional Context
I can make a PR, but first want to ensure this is a good idea.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
When you have a websocket endpoint which uses a dependency that holds some resource for the time of execution, this can lead to problems. Maybe this dependency itself does not acquire a resource, but it depends on another dependency which acquires a resource - so it's easy not to notice.
I propose to add a clarification to this page that you must not generally use dependencies with yield in a websocket endpoint.
I faced this problem myself:
Added websocket endpoint to my REST application. In this websocket endpoint I reused a dependency to authenticate users. This dependency requires DB session dependency. So, DB connection is acquired when client connects to ws and it's held until client disconnects from ws. But it's actually only needed in the beginning. I did not notice it, and because of this my app started having huge delays for both HTTP and websocket endpoints when a certain number of connected clients is reached. It was hard to debug.
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.110.0
Pydantic Version
2.6.2
Python Version
3.11.7
Additional Context
I can make a PR, but first want to ensure this is a good idea.
Beta Was this translation helpful? Give feedback.
All reactions