Skip to content

Commit 8a6ee66

Browse files
committed
fix: gracefully handle end of connection
1 parent 2e15072 commit 8a6ee66

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

wherobots/db/connection.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
import cbor2
99
import pyarrow
10-
from websockets.protocol import State
11-
from websockets.sync.client import ClientConnection
10+
import websockets.exceptions
11+
import websockets.protocol
12+
import websockets.sync.client
1213

1314
from wherobots.db.constants import (
1415
RequestKind,
@@ -47,7 +48,7 @@ class Connection:
4748
corresponding query state. Queries are tracked by their unique execution ID.
4849
"""
4950

50-
def __init__(self, ws: ClientConnection):
51+
def __init__(self, ws: websockets.sync.client.ClientConnection):
5152
self.__ws = ws
5253
self.__queries: dict[str, Query] = {}
5354
self.__thread = threading.Thread(
@@ -75,11 +76,15 @@ def cursor(self) -> Cursor:
7576

7677
def __main_loop(self):
7778
"""Main background loop listening for messages from the SQL session."""
78-
while self.__ws.protocol.state < State.CLOSING:
79+
logging.debug("Starting background connection handling loop...")
80+
while self.__ws.protocol.state < websockets.protocol.State.CLOSING:
7981
try:
8082
self.__listen()
83+
except websockets.exceptions.ConnectionClosedOK:
84+
logging.debug("Connection closed; stopping main loop.")
85+
return
8186
except Exception as e:
82-
logging.exception("Error handling message from SQL session", e)
87+
logging.exception("Error handling message from SQL session", exc_info=e)
8388

8489
def __listen(self):
8590
"""Waits for the next message from the SQL session and processes it.

0 commit comments

Comments
 (0)