Skip to content

Commit 192d730

Browse files
umlaeutehardbyte
authored andcommitted
capture OSErrors while receiving data (hardbyte#87)
Handle OSErrors while receiving data off socketcan native. Both sock.recv() and select.select(sock) throw errors when the device to which the socket is bound goes down. This makes the notifier thread survive such a condition (and bail out on the log) Closes: hardbyte#86
1 parent 695fef7 commit 192d730

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

can/interfaces/socketcan_native.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ def capturePacket(sock):
278278
except socket.timeout:
279279
log.debug('Captured no data, socket read timed out.')
280280
return None
281+
except OSError:
282+
# something bad happened (e.g. the interface went down)
283+
log.exception("Captured no data.")
284+
return None
281285

282286
can_id, can_dlc, data = dissect_can_frame(cf)
283287
log.debug('Received: can_id=%x, can_dlc=%x, data=%s', can_id, can_dlc, data)
@@ -335,11 +339,17 @@ def shutdown(self):
335339
self.socket.close()
336340

337341
def recv(self, timeout=None):
342+
data_ready = True
343+
try:
344+
if timeout is not None:
345+
data_ready = len(select.select([self.socket], [], [], timeout)[0]) > 0
346+
except OSError:
347+
# something bad happened (e.g. the interface went down)
348+
log.exception("Error while waiting for timeout")
349+
return None
338350

339-
if timeout is None or len(select.select([self.socket],
340-
[], [], timeout)[0]) > 0:
351+
if data_ready:
341352
packet = capturePacket(self.socket)
342-
343353
# The capturePacket function can return None if
344354
# self.socket.settimeout has been called.
345355
if packet is None:

0 commit comments

Comments
 (0)