Skip to content

Commit

Permalink
Suppress websocket going away error (#24)
Browse files Browse the repository at this point in the history
Supress uncaught exception whenever we attempt to write to a websocket
the client has disconnected on their end. Occurs whenever we are
streaming data and the user navigates away, implicitly closing the socket.
  • Loading branch information
Mejiro-McQueen authored Aug 1, 2023
1 parent 88e4190 commit c5a8cff
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions bifrost/services/core/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from starlette.responses import JSONResponse
from starlette.routing import Route, WebSocketRoute
from starlette.websockets import WebSocket, WebSocketDisconnect
from websockets.exceptions import ConnectionClosed
from starlette.routing import Mount
from starlette.staticfiles import StaticFiles
from starlette.middleware import Middleware
Expand Down Expand Up @@ -76,7 +77,10 @@ async def ws_subscribe(self, websocket):
pass
except TypeError:
print(f'{m} is not JSONable')

except ConnectionClosed:
log.info("Websocket Connection closed.")
except Exception as e:
log.error(e)

@with_loud_coroutine_exception
async def ws_telemetry(self, websocket):
Expand All @@ -93,6 +97,10 @@ async def ws_telemetry(self, websocket):
await websocket.send_json(d)
except WebSocketDisconnect:
pass
except ConnectionClosed:
log.info("Websocket Connection closed.")
except Exception as e:
log.error(e)

@with_loud_coroutine_exception
async def tlm_dict(self, request):
Expand Down Expand Up @@ -128,7 +136,11 @@ async def ws_command_loader(self, websocket):
await websocket.send_json(m)
except WebSocketDisconnect:
pass

except ConnectionClosed:
log.info("Websocket Connection closed.")
except Exception as e:
log.error(e)

@with_loud_coroutine_exception
async def config_request(self, request):
k = request.query_params.get('config_key', None)
Expand All @@ -150,6 +162,10 @@ async def ws_variable_messages(self, websocket):
pass
except TypeError:
print(f'{m} is not JSONable')
except ConnectionClosed:
log.info("Websocket Connection closed.")
except Exception as e:
log.error(e)

@with_loud_coroutine_exception
async def ws_monitors(self, websocket):
Expand All @@ -166,6 +182,10 @@ async def ws_monitors(self, websocket):
pass
except TypeError:
print(f'{m} is not JSONable')
except ConnectionClosed:
log.info("Websocket Connection closed.")
except Exception as e:
log.error(e)

@with_loud_coroutine_exception
async def sle_raf_directive(self, request):
Expand Down Expand Up @@ -210,3 +230,6 @@ async def ws_downlink_updates(self, websocket):
pass
except TypeError:
print(f'{m} is not JSONable')


# TODO: This is all junk, rewrite in golang.

0 comments on commit c5a8cff

Please sign in to comment.