diff --git a/can/thread_safe_bus.py b/can/thread_safe_bus.py index 518604364..8b3db4d8f 100644 --- a/can/thread_safe_bus.py +++ b/can/thread_safe_bus.py @@ -1,4 +1,3 @@ -from contextlib import nullcontext from threading import RLock from typing import TYPE_CHECKING, Any, cast @@ -29,6 +28,7 @@ class ThreadSafeBus( wraps around an existing interface instance. All public methods of that base class are now safe to be called from multiple threads. The send and receive methods are synchronized separately. + Default thread-based periodic sends share the send lock. Use this as a drop-in replacement for :class:`~can.BusABC`. @@ -67,14 +67,14 @@ def __init__( "BusABC", object.__getattribute__(self, "__wrapped__") ) - # now, BusABC.send_periodic() does not need a lock anymore, but the - # implementation still requires a context manager - self._self_wrapped._lock_send_periodic = nullcontext() # type: ignore[assignment] - # init locks for sending and receiving separately self._self_lock_send = RLock() self._self_lock_recv = RLock() + # Default periodic tasks call the wrapped backend directly, so their + # worker threads must share the lock used by explicit sends. + self._self_wrapped._lock_send_periodic = self._self_lock_send # type: ignore[assignment] + def recv(self, timeout: float | None = None) -> Message | None: with self._self_lock_recv: return self._self_wrapped.recv(timeout=timeout) diff --git a/doc/changelog.d/1620.fixed.rst b/doc/changelog.d/1620.fixed.rst new file mode 100644 index 000000000..1bf5c8dbe --- /dev/null +++ b/doc/changelog.d/1620.fixed.rst @@ -0,0 +1,2 @@ +Synchronize default thread-based periodic sends with explicit sends on +``ThreadSafeBus`` to prevent concurrent calls into the underlying backend. diff --git a/test/simplecyclic_test.py b/test/simplecyclic_test.py index 22a11e643..d28895b11 100644 --- a/test/simplecyclic_test.py +++ b/test/simplecyclic_test.py @@ -10,9 +10,9 @@ import time import traceback import unittest -from threading import Thread +from threading import Event, Thread from time import sleep -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch import can @@ -151,6 +151,39 @@ def test_stopping_perodic_tasks(self): # still be associated with the bus (e.g. for restarting) assert len(bus._periodic_tasks) == 5 + def test_manual_and_periodic_send_do_not_overlap(self): + first_entered = Event() + release_first = Event() + second_entered = Event() + first = can.Message(arbitration_id=0x100, data=[1], is_extended_id=False) + second = can.Message(arbitration_id=0x200, data=[2], is_extended_id=False) + + def observed_send(msg, timeout=None): + if msg.arbitration_id == first.arbitration_id: + first_entered.set() + release_first.wait() + else: + second_entered.set() + + bus = can.ThreadSafeBus(interface="virtual", channel=self.id()) + with bus, patch.object(bus.__wrapped__, "send", side_effect=observed_send): + manual_thread = Thread(target=bus.send, args=(first,)) + task = None + manual_thread.start() + try: + self.assertTrue(first_entered.wait(5)) + task = bus.send_periodic(second, 0.01) + self.assertFalse(second_entered.wait(0.1)) + release_first.set() + self.assertTrue(second_entered.wait(5)) + finally: + release_first.set() + if task is not None: + task.stop() + self.join_threads([manual_thread, task.thread], 5.0) + else: + self.join_threads([manual_thread], 5.0) + def test_restart_perodic_tasks(self): period = 0.01 safe_timeout = period * 5 if not IS_PYPY else 1.0