Compare commits

...

1 Commits

Author SHA1 Message Date
Alexander Whitestone
0694fa6167 fix: closes #730
Some checks failed
CI / test (pull_request) Failing after 9s
CI / validate (pull_request) Failing after 13s
Review Approval Gate / verify-review (pull_request) Failing after 4s
2026-04-12 12:38:57 -04:00

View File

@@ -48,24 +48,21 @@ async def broadcast_handler(websocket: websockets.WebSocketServerProtocol):
pass pass
# Broadcast to all OTHER clients # Broadcast to all OTHER clients
if not clients:
continue
disconnected = set() disconnected = set()
# Create broadcast tasks for efficiency # Create broadcast tasks paired with their target client
tasks = [] task_client_pairs = []
for client in clients: for client in clients:
if client != websocket and client.open: if client != websocket and client.open:
tasks.append(asyncio.create_task(client.send(message))) task = asyncio.create_task(client.send(message))
task_client_pairs.append((task, client))
if tasks:
if task_client_pairs:
tasks = [t for t, _ in task_client_pairs]
results = await asyncio.gather(*tasks, return_exceptions=True) results = await asyncio.gather(*tasks, return_exceptions=True)
for i, result in enumerate(results): for (task, client), result in zip(task_client_pairs, results):
if isinstance(result, Exception): if isinstance(result, Exception):
# Find the client that failed logger.error(f"Failed to send to a client {client.remote_address}: {result}")
target_client = [c for c in clients if c != websocket][i] disconnected.add(client)
logger.error(f"Failed to send to a client {target_client.remote_address}: {result}")
disconnected.add(target_client)
if disconnected: if disconnected:
clients.difference_update(disconnected) clients.difference_update(disconnected)