Skip to content

Commit

Permalink
Handle Thread.join correctly when it is not joinable
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kirienko committed Jan 18, 2024
1 parent c0e9c20 commit 7b38e49
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
5 changes: 4 additions & 1 deletion pycyphal/transport/can/media/candump/_candump.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ def close(self) -> None:
self._f.close()
self._thread, thd = None, self._thread
assert thd is not None
thd.join(timeout=1)
try:
thd.join(timeout=1)
except RuntimeError:
pass

@property
def _is_closed(self) -> bool:
Expand Down
10 changes: 8 additions & 2 deletions pycyphal/transport/can/media/pythoncan/_pythoncan.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,15 @@ def close(self) -> None:
self._closed = True
try:
self._tx_queue.put(None)
self._tx_thread.join(timeout=self._MAXIMAL_TIMEOUT_SEC * 10)
try:
self._tx_thread.join(timeout=self._MAXIMAL_TIMEOUT_SEC * 10)
except RuntimeError:
pass
if self._maybe_thread is not None:
self._maybe_thread.join(timeout=self._MAXIMAL_TIMEOUT_SEC * 10)
try:
self._maybe_thread.join(timeout=self._MAXIMAL_TIMEOUT_SEC * 10)
except RuntimeError:
pass
self._maybe_thread = None
finally:
try:
Expand Down
5 changes: 4 additions & 1 deletion pycyphal/transport/can/media/socketcan/_socketcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ def close(self) -> None:
if self._ctl_main.fileno() >= 0: # Ignore if already closed.
self._ctl_main.send(b"stop") # The actual data is irrelevant, we just need it to unblock the select().
if self._maybe_thread:
self._maybe_thread.join(timeout=_SELECT_TIMEOUT)
try:
self._maybe_thread.join(timeout=_SELECT_TIMEOUT)
except RuntimeError:
pass
self._maybe_thread = None
finally:
self._sock.close() # These are expected to be idempotent.
Expand Down
10 changes: 8 additions & 2 deletions pycyphal/transport/can/media/socketcand/_socketcand.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,15 @@ def close(self) -> None:
self._closed = True
try:
self._tx_queue.put(None)
self._tx_thread.join(timeout=self._MAXIMAL_TIMEOUT_SEC * 10)
try:
self._tx_thread.join(timeout=self._MAXIMAL_TIMEOUT_SEC * 10)
except RuntimeError:
pass
if self._maybe_thread is not None:
self._maybe_thread.join(timeout=self._MAXIMAL_TIMEOUT_SEC * 10)
try:
self._maybe_thread.join(timeout=self._MAXIMAL_TIMEOUT_SEC * 10)
except RuntimeError:
pass
self._maybe_thread = None
finally:
try:
Expand Down

0 comments on commit 7b38e49

Please sign in to comment.