Skip to content

Commit ea1759f

Browse files
committed
Fix python client exceptions
1 parent cf53a4d commit ea1759f

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

compost_rpc/compost_rpc.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ class UnexpectedTxnError(RpcError):
166166
pass
167167

168168

169+
class MalformedMessageError(RpcError):
170+
"""Received malformed message."""
171+
172+
pass
173+
174+
169175
class MemUnit:
170176
"""Represents a memory size that can be specified in bits or bytes. Now immutable."""
171177

@@ -1101,7 +1107,7 @@ def __init__(self, transport: Transport, rpcs: dict[int, Rpc], timeout:float=1):
11011107
self.rpcs = rpcs
11021108
self.timeout = timeout
11031109
self.last_txn = 1
1104-
self.q: Queue[Msg] = Queue(0) # Maybe define some size?
1110+
self.q: Queue[Msg | Exception] = Queue(0) # Maybe define some size?
11051111
self.listener = threading.Thread(target=self.receiver, name="Listener_thread")
11061112
self.listener.daemon = True
11071113
self.listener.start()
@@ -1115,13 +1121,20 @@ def next_txn(self) -> int:
11151121

11161122
def receiver(self):
11171123
while True:
1118-
msg_bytes = self.transport.receive()
1124+
try:
1125+
msg_bytes = self.transport.receive()
1126+
except Exception as e:
1127+
logger.error(f"Transport receive error: {e}")
1128+
self.q.put(e)
1129+
continue
11191130
if len(msg_bytes) < 4:
11201131
logger.error("Malformed message: Length < 4")
1132+
self.q.put(MalformedMessageError("Received message length < 4 bytes"))
11211133
continue
11221134
msg = Msg.unpack(msg_bytes)
11231135
logger.debug(f"<- {msg}")
11241136
if msg.header.rpc_id not in self.rpcs:
1137+
self.q.put(UnknownRpcIdError())
11251138
logger.error(f"Received unknown rpc_id {hex(msg.header.rpc_id)}")
11261139
continue
11271140
if msg.header.txn == 0:
@@ -1137,10 +1150,16 @@ def sender(self, msg: Msg):
11371150
self.transport.send(msg.pack())
11381151

11391152
def recv_response(self, txn: int) -> Msg:
1153+
if not self.listener.is_alive():
1154+
raise RuntimeError("Transport error. Listener thread not running.")
11401155
try:
1141-
msg = self.q.get(timeout=self.timeout)
1156+
item = self.q.get(timeout=self.timeout)
11421157
except(Empty):
11431158
raise RequestTimeoutError("Response not received in time.")
1159+
if isinstance(item, Exception):
1160+
raise item
1161+
else:
1162+
msg = item
11441163
if msg.header.rpc_id not in self.rpcs:
11451164
logger.error(f"Received unknown rpc_id {hex(msg.header.rpc_id)}")
11461165
raise UnknownRpcIdError()

0 commit comments

Comments
 (0)