Random behaviour of `Depends` functionality.
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the FastAPI documentation, with the integrated search.
- [X] I already searched in Google "How to X in FastAPI" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to FastAPI but to Pydantic.
- [X] I already checked if it is not related to FastAPI but to Swagger UI.
- [X] I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
'''Main Websocket Route'''
@app.websocket("/{obj_id}")
async def websocket_endpoint(websocket: WebSocket, obj_id: str, active_clients = Depends(get_active_clients)):
await websocket.accept()
try:
await ws_manager.connect(websocket, obj_id, active_clients)
except WebSocketDisconnect:
await websocket.close()
'''Connection Manager'''
class ConnectionManager:
async def connect(self, websocket: WebSocket, obj_id: str, active_connections):
client = ServerPoint(obj_id, websocket)
try:
active_connections[cp_id] = client
await client.start()
except WebSocketDisconnect:
active_connections.pop(cp_id)
ws_manager = ConnectionManager()
active_clients: Dict[str, ServerPoint] = {}
'''Dependency function'''
async def get_active_clients() -> Dict[str, ServerPoint]:
return active_clients
'''Sample Test'''
@app.get("/test-active)
def test_dict_keys(active_clients: Dict[str, ServerPoint] = Depends(get_active_clients)):
clients = list(active_clients.keys())
return clients
Description
Hi, I have been working with FastAPI for managing websocket connections, and I am saving the websocket connections in a dictionary. However, whenever I try to access this active connection array, there are seemingly random instances wherein this array returns empty while at other times, it outputs the correct connection instances.
My workflow is derived from the websocket tutorial on the FastAPI website. I am using an external library that manages this websocket connection to perform various functionalities (defined as ServerPoint in the ConnectionManager class).
The major issue arises wherein I have defined an additional HTTP route which looks to perform certain operations defined in the ServerPoint class on an individual connection stored in the dictionary, but the behaviour of the Depends(get_active_clients) is pretty erratic with it returning an empty dictionary on some occasions and returning full instances on another.
I created a minimal testing route to check the contents of the dictionary but somehow, it gives mixed results at each call. It is difficult to reproduce the error as I am not able to understand any real pattern to the behaviour.
It would be greatly helpful if someone could guide me to solve this issue and be able to generate a robust workflow.
Thanks
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.85.0
Python Version
3.9.13
Additional Context
No response
Can you provide an example of the issue that you're facing?
Are you running with multiple workers? - just trying to figure out.