Skip to content

Commit 8112d13

Browse files
authored
Merge pull request hardbyte#637 from hardbyte/felixdivo-patch-stop_all_periodic_tasks
Fix iteration in Bus.stop_all_periodic_tasks
2 parents c7c1640 + 0120e29 commit 8112d13

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

can/bus.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,21 @@ def _send_periodic_internal(self, msgs, period, duration=None):
259259
return task
260260

261261
def stop_all_periodic_tasks(self, remove_tasks=True):
262-
"""Stop sending any messages that were started using bus.send_periodic
262+
"""Stop sending any messages that were started using **bus.send_periodic**.
263+
264+
.. note::
265+
The result is undefined if a single task throws an exception while being stopped.
263266
264267
:param bool remove_tasks:
265268
Stop tracking the stopped tasks.
266269
"""
267270
for task in self._periodic_tasks:
268-
task.stop(remove_task=remove_tasks)
271+
# we cannot let `task.stop()` modify `self._periodic_tasks` while we are
272+
# iterating over it (#634)
273+
task.stop(remove_task=False)
274+
275+
if remove_tasks:
276+
self._periodic_tasks = []
269277

270278
def __iter__(self):
271279
"""Allow iteration on messages as they are received.

0 commit comments

Comments
 (0)