Skip to content

Reference to task is lost when starting a second Sync periodic #582

@yves-chevallier

Description

@yves-chevallier

Consider this snippet:

import canopen
network = canopen.Network()
network.connect(channel='vcan0', bustype="socketcan")
network.sync.start(1.0)
network.sync.start(0.5)
# Using `candump -L vcan0` I have the two tasks running in parallel
network.sync.stop() # Only the latter task is stopped
network.sync.stop() # CanOperationError, the latter task is already stopped

In sync.py, we notice that attribute self._task is erased with the new task, which became inaccessible. I don't know if this hack will work, but we could only allow one sync event at the time:

diff --git a/canopen/sync.py b/canopen/sync.py
index d373451..6965b94 100644
--- a/canopen/sync.py
+++ b/canopen/sync.py
@@ -33,9 +33,14 @@ class SyncProducer:
         if not self.period:
             raise ValueError("A valid transmission period has not been given")
 
+        if self._task is not None:
+            self._task.stop()
+            self._task = None
+
         self._task = self.network.send_periodic(self.cob_id, [], self.period)
 
     def stop(self):
         """Stop periodic transmission of SYNC message."""
         if self._task is not None:
             self._task.stop()
+            self._task = None

Does it make sens?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions