Skip to content
Open
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
8 changes: 4 additions & 4 deletions can/thread_safe_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ def shutdown(self) -> None:

@property
def state(self) -> BusState:
with self._self_lock_send, self._self_lock_recv:
return self._self_wrapped.state
# Reading the state must not wait for the locks: a thread blocking in
# recv() holds the receive lock for its whole timeout.
return self._self_wrapped.state

@state.setter
def state(self, new_state: BusState) -> None:
Expand All @@ -117,5 +118,4 @@ def state(self, new_state: BusState) -> None:

@property
def protocol(self) -> CanProtocol:
with self._self_lock_send, self._self_lock_recv:
return self._self_wrapped.protocol
return self._self_wrapped.protocol
1 change: 1 addition & 0 deletions doc/changelog.d/1891.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reading ``ThreadSafeBus.state`` and ``ThreadSafeBus.protocol`` no longer waits for a thread that is blocked in ``recv()``.
14 changes: 14 additions & 0 deletions test/back2back_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,20 @@ def receiver(_):
receiver_pool.close()
receiver_pool.join()

def test_state_while_recv_blocks(self):
"""Reading state and protocol does not wait for a blocked recv() (#1891)."""
receiver = ThreadPool(1)
pending = receiver.apply_async(self.bus2.recv, (1.0,))
sleep(0.1) # let the receiver take the receive lock

start = time()
self.assertIsInstance(self.bus2.state, can.BusState)
self.assertIsInstance(self.bus2.protocol, can.CanProtocol)
self.assertLess(time() - start, 0.5)

self.assertIsNone(pending.get())
receiver.close()

@pytest.mark.timeout(180.0 if IS_PYPY else 5.0)
def test_filtered_bus(self):
sender_pool = ThreadPool(100)
Expand Down
Loading