Release canceled timers dropped by their owner in EventsCBGExecutor #3441
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.43) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 358 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.352
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #3441"
general
weight: 0.35
mjcarroll
Description
EventsCBGExecutorleaks a timer that was canceled and then dropped by its owner.A canceled timer leaves the running list of the
TimerQueuebut stays inall_timers. Once the owner drops the timer, the weak pointer in the entity cache expires, so the cache never callsremove_timerfor it.remove_if_droppedonly looks at the head of the running list and is called from the reset callback, so it never sees the canceled timer either. Thercl_timer_tand itsTimerDatastay alive until the executor is destroyed.This PR removes such timers when a new timer is added. A timer is removed when it is not in the running list and the
TimerQueueholds the only reference to its rcl handle. The removal itself goes throughremove_if_dropped. Timers in the running list are left to the existing path, becauserunning_timersstill points to them.The cleanup runs only in the
TimerQueuethat the new timer goes to, and each clock type has its own queue. For example, ROS time timers that were canceled and dropped stay until the next ROS time timer is added. What can remain is bounded by the timers that were in that queue at its lastadd_timer, so it no longer grows with the number of timers created.How to reproduce
Repeat N times on an
EventsCBGExecutor: create a 2 ms wall timer that cancels itself in its first callback, wait for the callback, then drop the timer. Then count the dropped timers whosercl_timer_thandle (held asweak_ptr) is still alive, and check what heaptrack reports as leaked at_exit.Before this change, on rolling HEAD (with rcl at rolling HEAD):
| N | dropped handles still alive | leaked from
rcl_timer_init2| leaked fromTimerQueue::add_timer||---|---|---|---|
| 200 | 200 | 35.20K | 22.40K + 6.40K |
| 1000 | 1000 | 176.00K | 112.00K + 32.00K |
| 2000 | 2000 | 352.00K | 224.00K + 64.00K |
After this change, no dropped handle stays alive, and the leaked size does not grow with N: 176 B to 352 B from
rcl_timer_init2and about 112 B + 32 B fromadd_timer, for every N.The jazzy branch has the same leak (about 160 B per timer from
rcl_timer_init2), and the equivalent change fixes it there too.Tests
Adds
test_events_cbg_executor_timer_leak:timer_canceled_in_callback_is_released_after_dropandtimer_canceled_before_due_is_released_after_dropfail without this change and pass with it.canceled_timer_still_owned_fires_after_resetchecks that a canceled timer that is still owned is kept and fires again afterreset().Is this user-facing behavior change?
No. Only memory that was never released is released now.
Did you use Generative AI?
Yes. Claude Code (Claude Opus 5.5) helped find the cause and write the change, the tests and this description. I reviewed the change and ran the tests and the heaptrack measurements above.