← Back to Pull requests

Release canceled timers dropped by their owner in EventsCBGExecutor #3441

Open
unknown opened this pull request with 358 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 26, 2026 04:50 UTC
Author

Description

EventsCBGExecutor leaks a timer that was canceled and then dropped by its owner.

A canceled timer leaves the running list of the TimerQueue but stays in all_timers. Once the owner drops the timer, the weak pointer in the entity cache expires, so the cache never calls remove_timer for it. remove_if_dropped only looks at the head of the running list and is called from the reset callback, so it never sees the canceled timer either. The rcl_timer_t and its TimerData stay 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 TimerQueue holds the only reference to its rcl handle. The removal itself goes through remove_if_dropped. Timers in the running list are left to the existing path, because running_timers still points to them.

The cleanup runs only in the TimerQueue that 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 last add_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 whose rcl_timer_t handle (held as weak_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 from TimerQueue::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_init2 and about 112 B + 32 B from add_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_drop and timer_canceled_before_due_is_released_after_drop fail without this change and pass with it.

  • canceled_timer_still_owned_fires_after_reset checks that a canceled timer that is still owned is kept and fires again after reset().
  • 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.

    Diff: 358 lines (+268 / -90) across 4 files Reviewer: mjcarroll Review duration: 6459m
    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
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.6% (MEDIUM) · Higher = larger blast radius
    42.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.3% · Safe to merge
    24.3%
    Repository