Fix: Reset _closing flag on WebSocket reconnection to ensure on_client_disconnected fires

The on_client_disconnected event handler was not firing after the first
disconnection in long-running containers. The _closing flag was set to True
on first disconnect and never reset, preventing subsequent disconnect events
from being triggered.

This fix resets the _closing flag to False in the setup() method, ensuring
each new connection starts with a clean state.
This commit is contained in:
James Hush
2025-10-02 14:11:14 +08:00
parent c567fd71b1
commit 45ebd042ac

View File

@@ -105,6 +105,8 @@ class FastAPIWebsocketClient:
_: The start frame (unused).
"""
self._leave_counter += 1
# Reset closing flag when setting up a new connection
self._closing = False
def receive(self) -> typing.AsyncIterator[bytes | str]:
"""Get an async iterator for receiving WebSocket messages.