Fixed error on trying to send ws message to disconnected client
This could result in a major filament usage track failure since the Use http request would fail and Moonraker would keep retrying to save the used filament. However it was actually saved since the db.commit() happens before the websocket message fails.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import logging
|
||||
|
||||
from fastapi import WebSocket
|
||||
from starlette.websockets import WebSocketState
|
||||
|
||||
from spoolman.api.v1.models import Event
|
||||
|
||||
@@ -46,7 +47,22 @@ class SubscriptionTree:
|
||||
"""Send a message to all websockets in this branch of the tree."""
|
||||
# Broadcast to all subscribers on this level
|
||||
for websocket in self.subscribers:
|
||||
await websocket.send_text(evt.json())
|
||||
if (
|
||||
websocket.client_state == WebSocketState.DISCONNECTED # noqa: PLR1714
|
||||
or websocket.application_state == WebSocketState.DISCONNECTED
|
||||
):
|
||||
# A bad disconnection may have occurred
|
||||
self.remove(path, websocket)
|
||||
logger.info(
|
||||
"Forcing disconnection of client %s on pool %s",
|
||||
websocket.client.host if websocket.client else "?",
|
||||
",".join(path),
|
||||
)
|
||||
elif (
|
||||
websocket.client_state == WebSocketState.CONNECTED
|
||||
and websocket.application_state == WebSocketState.CONNECTED
|
||||
):
|
||||
await websocket.send_text(evt.json())
|
||||
|
||||
# Send the message further down the tree
|
||||
if len(path) > 0 and path[0] in self.children:
|
||||
|
||||
Reference in New Issue
Block a user