Skip to content

Commit

Permalink
Handle condition where socket returns None
Browse files Browse the repository at this point in the history
This happens when we try to reset the connection, but can't establish anything
due to the device not accepting connections.
  • Loading branch information
BrianJKoopman committed Oct 4, 2024
1 parent 6fc9d14 commit 81929b9
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions socs/agents/cryomech_cpa/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def reset(self):
self.comm = self._connect((self.ip_address, self.port))

def _write(self, msg):
if self.comm is None:
print("Connection not established. Unable to send command.")
self.reset()
return

try:
self.comm.sendall(msg)
return
Expand All @@ -73,13 +78,18 @@ def _write(self, msg):
except TimeoutError as e:
print(f"Timeout error while writing: {e}")
raise ConnectionError
except AttributeError:
raise ConnectionError("Unable to reset connection.")
except Exception as e:
print(f"Caught unexpected {type(e).__name__} during write:")
print(f" {e}")
raise ConnectionError

def _check_ready(self):
"""Check socket is ready to read from."""
if self.comm is None:
raise ConnectionError("Connection not established, not ready to read.")

sel = selectors.DefaultSelector()
sel.register(self.comm, selectors.EVENT_READ)
if not sel.select(self.timeout):
Expand Down

0 comments on commit 81929b9

Please sign in to comment.