Skip to content

Commit

Permalink
fix(websockets): improve error logging for edge-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
janaab11 committed Jan 3, 2025
1 parent dd1ff48 commit e1db50d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/diart/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,21 @@ def _on_message_received(
WebSocket server instance
message : AnyStr
Received message data
Raises
------
Warning
If client not found in self._clients. Common cases:
- Message received before client initialization complete
- Message received after client cleanup started
- Race condition in client connection/disconnection
Error
If message processing fails. Error will be logged.
"""
client_id = client["id"]

if client_id not in self._clients:
logger.warning(f"Received message from unregistered client {client_id}")
return

try:
Expand All @@ -240,12 +251,23 @@ def send(self, client_id: Text, message: AnyStr) -> None:
Target client identifier
message : AnyStr
Message to send
Raises
------
Warning
If client is not found in server.clients. Common cases:
- Client disconnected but cleanup is not complete
- Network issues caused client to drop
- Race condition between disconnect and message send
Error
If message sending fails. Error will be logged and re-raised.
"""
if not message:
return

client = next((c for c in self.server.clients if c["id"] == client_id), None)
if client is None:
logger.warning(f"Failed to send message to client {client_id}: client not found")
return

try:
Expand All @@ -261,8 +283,19 @@ def close(self, client_id: Text) -> None:
----------
client_id : Text
Client identifier to close
Raises
------
Warning
If client not found in self._clients. Common cases:
- Already cleaned up by another error handler
- Multiple error handlers trying to cleanup same client
- Cleanup triggered for client that never fully connected
Error
If cleanup fails. Error will be logged and client will be force-removed.
"""
if client_id not in self._clients:
logger.warning(f"Attempted to close non-existent client {client_id}")
return

try:
Expand Down

0 comments on commit e1db50d

Please sign in to comment.