Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions can/thread_safe_bus.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from contextlib import nullcontext
from threading import RLock
from typing import TYPE_CHECKING, Any, cast

Expand Down Expand Up @@ -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`.

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions doc/changelog.d/1620.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Synchronize default thread-based periodic sends with explicit sends on
``ThreadSafeBus`` to prevent concurrent calls into the underlying backend.
37 changes: 35 additions & 2 deletions test/simplecyclic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading