Skip to content

Commit 0a9fad2

Browse files
author
Alan Christie
committed
fix: Handle RuntimeError closing socket
1 parent c298640 commit 0a9fad2

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

app/app.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,19 @@ async def event_stream(
345345
offset_specification=offset_specification,
346346
)
347347

348-
# One our way out...
349-
await websocket.close(
350-
code=status.WS_1000_NORMAL_CLOSURE, reason="The stream has been deleted"
351-
)
348+
# On our way out...
349+
try:
350+
await websocket.close(
351+
code=status.WS_1000_NORMAL_CLOSURE, reason="The stream has been deleted"
352+
)
353+
except RuntimeError:
354+
# There's a chance we encounter a RuntimeError
355+
# with the message 'RuntimeError: Cannot call "send" once a close message has been sent.'
356+
# We don't care - we have to just get out of here
357+
# so errors in tear-down have to be ignored,
358+
# and there's no apparent way to know whether calling 'close()' is safe.
359+
_LOGGER.debug("Ignoring RuntimeError from close() for %s", es_id)
360+
352361
_LOGGER.info("Closed WebSocket for %s (uuid=%s)", es_id, uuid)
353362

354363

0 commit comments

Comments
 (0)