Fixed a race condition in FastAPIWebsocketClient that occurred when attempting to send a message while the client was disconnecting.
This commit is contained in:
@@ -97,6 +97,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
- Fixed a race condition in `FastAPIWebsocketClient` that occurred when attempting to
|
||||||
|
send a message while the client was disconnecting.
|
||||||
|
|
||||||
- Fixed an issue in `GoogleLLMService` where interruptions did not work when an
|
- Fixed an issue in `GoogleLLMService` where interruptions did not work when an
|
||||||
interruption strategy was used.
|
interruption strategy was used.
|
||||||
|
|
||||||
|
|||||||
@@ -132,8 +132,11 @@ class FastAPIWebsocketClient:
|
|||||||
f"{self} exception sending data: {e.__class__.__name__} ({e}), application_state: {self._websocket.application_state}"
|
f"{self} exception sending data: {e.__class__.__name__} ({e}), application_state: {self._websocket.application_state}"
|
||||||
)
|
)
|
||||||
# For some reason the websocket is disconnected, and we are not able to send data
|
# For some reason the websocket is disconnected, and we are not able to send data
|
||||||
# So let's properly handle it and disconnect the transport
|
# So let's properly handle it and disconnect the transport if it is not already disconnecting
|
||||||
if self._websocket.application_state == WebSocketState.DISCONNECTED:
|
if (
|
||||||
|
self._websocket.application_state == WebSocketState.DISCONNECTED
|
||||||
|
and not self.is_closing
|
||||||
|
):
|
||||||
logger.warning("Closing already disconnected websocket!")
|
logger.warning("Closing already disconnected websocket!")
|
||||||
self._closing = True
|
self._closing = True
|
||||||
await self.trigger_client_disconnected()
|
await self.trigger_client_disconnected()
|
||||||
@@ -146,8 +149,12 @@ class FastAPIWebsocketClient:
|
|||||||
|
|
||||||
if self.is_connected and not self.is_closing:
|
if self.is_connected and not self.is_closing:
|
||||||
self._closing = True
|
self._closing = True
|
||||||
await self._websocket.close()
|
try:
|
||||||
await self.trigger_client_disconnected()
|
await self._websocket.close()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"{self} exception while closing the websocket: {e}")
|
||||||
|
finally:
|
||||||
|
await self.trigger_client_disconnected()
|
||||||
|
|
||||||
async def trigger_client_disconnected(self):
|
async def trigger_client_disconnected(self):
|
||||||
"""Trigger the client disconnected callback."""
|
"""Trigger the client disconnected callback."""
|
||||||
|
|||||||
Reference in New Issue
Block a user