Skip to content

Destroy the old timer when the updater period changes - #673

Open
Randil-Fernando wants to merge 1 commit into
ros:ros2from
Randil-Fernando:fix-updater-period-timer
Open

Randil-Fernando wants to merge 1 commit into
ros:ros2from
Randil-Fernando:fix-updater-period-timer

Conversation

@Randil-Fernando

Copy link
Copy Markdown

Fixes ros2/ros2#1883.

Updater.period's setter resets the old timer instead of destroying it:

self.__period = period
self.timer.reset()
self.timer = self.node.create_timer(self.__period, self.update)

reset() only restarts a timer. rclpy's Node appends every timer it creates to
self._timers and holds a strong reference, so rebinding self.timer doesn't free
the old one — it stays registered and keeps firing. Every period assignment leaks
a timer and adds another caller of update().

The fix destroys it through the node, which removes it from _timers before the
replacement is created:

self.__period = period
self.node.destroy_timer(self.timer)
self.timer = self.node.create_timer(self.__period, self.update)

Added test/test_updater_period.py, registered in CMakeLists.txt next to the
other pytest tests. It asserts one timer survives a period change, that it's a new
object, and that its period actually took effect. Against the old code it fails with
AssertionError: 2 != 1; with the fix it passes.

The C++ Updater is not affected — reset_timer() reassigns a
rclcpp::TimerBase::SharedPtr and the callback group only holds a weak pointer, so
the old timer is cleaned up by RAII. This is rclpy-specific.

Reported against Jazzy, so a backport label would be worth adding.

The period setter called self.timer.reset() and then rebound self.timer
to a new timer. reset() only restarts a timer, so the old one stayed
registered with the node and kept firing: rclpy's Node holds a strong
reference to every timer it creates, so rebinding the attribute does not
free it. Each period change therefore leaked a timer and added another
caller of update()

Destroy the old timer through the node instead, which removes it from
the node's timer list before the replacement is created

Fixes ros2/ros2#1883

Signed-off-by: Randil Fernando <randilfernando829@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 06:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting diagnostics updater's period results in double update calls.

2 participants