Skip to content

prune expired callback groups - #3437

Merged
christophebedard merged 1 commit into
ros2:rollingfrom
jwallace42:fix/callback-group-weak-ptr-prune
Dec 1, 2026
Merged

christophebedard merged 1 commit into
ros2:rollingfrom
jwallace42:fix/callback-group-weak-ptr-prune

Conversation

@jwallace42

@jwallace42 jwallace42 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

Weak pointers to the destroyed callback groups were never removed from NodeBase::callback_groups_. Each expired weak pointer keeps the group's allocation alive. This causes the memory to grow for every callback group added or destroyed.

This fix follows the same pattern as CallbackGroup::add_subscription and other add methods.

Using the script provided in the ticket I verified that the memory leak no longer exists.

Fixes #3428

Is this user-facing behavior change?

Did you use Generative AI?

I used generative AI to test the fix and find find out how this particular issue was handled for callbacks.

Additional Information

@SteveMacenski

SteveMacenski commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

This resolved a current bug in Nav2 we would like backported - we end up running into this because we construct potentially hundreds of behavior trees in a single session due to multiple runs with iteratively changing behaviors. So we end up creating a bunch of new callback groups for particular BT node ROS interfaces, which triggers this issue. Over the course of day(s) of operations it can end up hogging memory.

PS to maintainers - the test failures seem unrelated to this change

@github-actions

Copy link
Copy Markdown

ABI Compliance Check

✅ Verdict: compatible

Library Verdict Summary
libcomponent_manager.so ✅ compatible No ABI changes detected.
librclcpp.so ✅ compatible No ABI changes detected.
librclcpp_action.so ✅ compatible No ABI changes detected.
librclcpp_lifecycle.so ✅ compatible No ABI changes detected.
✅ libcomponent_manager.so — full abidiff report

Compared:

  • Base: lib-base/libcomponent_manager.so
  • Head: lib-pr/libcomponent_manager.so @ 4666471
(empty report — no differences printed by abidiff)
✅ librclcpp.so — full abidiff report

Compared:

  • Base: lib-base/librclcpp.so
  • Head: lib-pr/librclcpp.so @ 4666471
Functions changes summary: 0 Removed (29 filtered out), 0 Changed (86 filtered out), 0 Added (42 filtered out) functions
Variables changes summary: 0 Removed, 0 Changed, 0 Added variable


✅ librclcpp_action.so — full abidiff report

Compared:

  • Base: lib-base/librclcpp_action.so
  • Head: lib-pr/librclcpp_action.so @ 4666471
(empty report — no differences printed by abidiff)
✅ librclcpp_lifecycle.so — full abidiff report

Compared:

  • Base: lib-base/librclcpp_lifecycle.so
  • Head: lib-pr/librclcpp_lifecycle.so @ 4666471
(empty report — no differences printed by abidiff)

Updated for commit 4666471 · check run · suppressions: /home/runner/work/_temp/ros2-abi-suppressions.txt

@christophebedard

christophebedard commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Pulls: #3437
Gist: https://gist.githubusercontent.com/christophebedard/d82e5664bf81cf7a19011b5f709387e3/raw/4be470d381f24ca5bd5f125653d4adf4e94ca98d/ros2.repos
BUILD args: --packages-above-and-dependencies rclcpp
TEST args: --packages-above rclcpp
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/21294

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@christophebedard

Copy link
Copy Markdown
Member

I think the Rpr build failure is due to ros2/rcl#1358 not being released/bloomed and synced to the testing repo; it's trying to build against an older version of rcl.

@christophebedard christophebedard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks for the fix.

Could you just amend & sign-off your commit?

@christophebedard

Copy link
Copy Markdown
Member

I slightly changed the PR description so that the referenced issue gets closed when this gets merged.

@jmachowinski

Copy link
Copy Markdown
Collaborator

We had complains with similar patches in the past.
If you now add 2000+ callbackgroups (I know it is a bogus case), adding gets slower over time.

What about adding an argument defaulting to true for pruning ?

@christophebedard

christophebedard commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Good point. What if we only prune when the vector is full, i.e., callback_groups_.size() == callback_groups_.capacity(), before pushing back the new group?

@SteveMacenski

Copy link
Copy Markdown
Collaborator

What about adding an argument defaulting to true for pruning ?

The pattern in this PR is the same as how subscriptions, timers, etc are handled. I think for consistency this is the best solution. I also (personally) don't see a motivating non-theoretical reason for disabling that pruning.

What if we only prune when the vector is full, i.e., callback_groups_.size() == callback_groups_.capacity()

How do we decide how much we set the default capacity to? I imagine that would need to be a parameter to reserve the size on setup. I think my comment above is similar that if we did that for the callback groups, we'd want to do that for the interfaces too for consistency (probably a parameter for each one for the advanced user that wants to specifically state the number to avoid allocations).

I think that's more of 'future work' / new ticket ground, since that is a not-super-related feature addition to what we're doing here, which has current codebase prescience. Admittedly my bias is for consistency over most over considerations in how I maintain Nav2 so the same patterns repeat for familiarity.

@christophebedard

Copy link
Copy Markdown
Member

I'm personally fine with taking this as-is and considering an improvement later. This is an initialization-time thing anyway.

How do we decide how much we set the default capacity to? I imagine that would need to be a parameter to reserve the size on setup

I would've just not set a default capacity and let it double (or whatever) its capacity when it's full

@christophebedard

christophebedard commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

I'll proceed with this as soon as the commit is signed off; we can follow-up with an improvement later.

Signed-off-by: Joshua Wallace <josho.wallace@gmail.com>
@jwallace42
jwallace42 force-pushed the fix/callback-group-weak-ptr-prune branch from 4666471 to 741dddb Compare November 30, 2026 19:10
@christophebedard

Copy link
Copy Markdown
Member

the commit was amended without making any code changes, so I'll just merge this.

@christophebedard
christophebedard merged commit f09d1c3 into ros2:rolling Dec 1, 2026
5 of 6 checks passed
@christophebedard

Copy link
Copy Markdown
Member

@jmachowinski @SteveMacenski FYI I opened a follow-up issue based on the discussion above: #3463

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory grows per callback group created and destroyed on long lived nodes

5 participants