Repository navigation
fix(observable_subscription): destroy native ROS subscription on destroy() - #1704
Conversation
…roy() ObservableSubscription.destroy()/complete() only completed the RxJS Subject wrapper; the underlying native ROS 2 subscription created by createObservableSubscription() was never destroyed. Any code that reconnects an observable subscription (e.g. an rxjs timeout()+repeat() pattern resubscribing after a publisher gap) leaks one native subscription per cycle, each still receiving and deserializing every message forever, causing CPU usage to climb on every reconnect. Pass the owning node into ObservableSubscription and call node.destroySubscription() when the wrapper is destroyed.
There was a problem hiding this comment.
🟡 Changes recommended
The reproduction only resubscribes to the same Subject rather than exercising cleanup, so it needs repeated wrapper creation, explicit disposal, and clarification that RxJS unsubscription alone does not release the native subscription.
1 open issue
What changed in this PR
This PR makes explicit disposal of an ObservableSubscription release its underlying native ROS 2 subscription, rather than only completing the RxJS Subject.
Changes:
- Pass the owning node into each observable subscription wrapper.
- Destroy the native subscription when
complete()ordestroy()is called. - Add regression tests for native cleanup and repeated creation/disposal cycles.
| File | Description |
|---|---|
| test/test-observable-subscription.js | Adds native cleanup and repeated-disposal regression tests. |
| lib/observable_subscription.js | Stores the owning node and releases the native subscription on completion. |
| lib/node.js | Passes the owning node to the observable subscription constructor. |
Review effort: Balanced
| if (!this.#destroyed) { | ||
| this.#destroyed = true; | ||
| this.#subject.complete(); | ||
| this.#node.destroySubscription(this.#subscription); |
There was a problem hiding this comment.
You're right — the original repro only resubscribed to the same wrapper's .observable via repeat(), so it never called destroy()/complete() and never actually created a second native subscription. Updated both:
- PR description: the reproduction now wraps
createObservableSubscriptionin aswitchMap+share()+finalize(() => obsSub.destroy())(the same shape the affected app code uses), so eachtimeout()/repeat()cycle creates a fresh wrapper and disposes of the previous one. - Test comment (now around test-observable-subscription.js:224): clarified that plain RxJS unsubscription never releases the native subscription either way, and that the leak only happens when app code creates a new
ObservableSubscriptionper reconnect and relies ondestroy()to free the old one — which is exactly what the two tests below exercise explicitly.
minggangw
left a comment
There was a problem hiding this comment.
@PierreSachot Thanks for submitting the PR, LGTM!

Problem
ObservableSubscription.destroy()/.complete()only callsSubject.complete(). It never destroys the underlying native ROS 2 subscription created inNode.createObservableSubscription().Any consumer that creates a new
ObservableSubscriptionper reconnect and disposes of the previous one — for example aswitchMap+share()wrapper whosefinalize()callsobsSub.destroy(), driven by an RxJStimeout()+catchError()+repeat()pattern that detects a stalled topic — leaks one native subscription per reconnect cycle. Each leaked subscription keeps receiving and deserializing every message on the topic forever, with nothing listening to it. CPU usage climbs a little more on every reconnect and never comes back down.Note: merely unsubscribing from RxJS (e.g.
repeat()resubscribing to the same wrapper's.observable) never touches the native subscription either way — only an explicitdestroy()/complete()call does. The leak only shows up once app code creates a freshObservableSubscriptionper cycle and relies ondestroy()to free the old one, which is the pattern below.Fix
Pass the owning
NodeintoObservableSubscriptionand callnode.destroySubscription(subscription)fromcomplete()/destroy(), in addition to completing theSubject.destroySubscriptionis already idempotent (guarded by the entity'sisDestroyed()), so this is safe to call even if the node itself destroys the subscription later (e.g. vianode.destroy()).Reproduction
Minimal repro extracted from a real app that hit this (unrelated app internals mocked out). The
switchMap/share/finalizewrapper is the part that actually creates a newObservableSubscriptionon every reconnect and disposes of the previous one — this is what was missing from an earlier version of this repro:Before this fix, process CPU usage (e.g. via
top/htopon the node process) increases by a roughly constant amount after every publish-gap/resume cycle, with no corresponding increase in subscriber count or log output — the extra work is happening in orphaned native subscriptions nobody is listening to.Tests
Added two tests to
test/test-observable-subscription.js:destroy()also destroys the underlying native ROS subscription.Both explicitly create a new
ObservableSubscriptionand call.destroy()each cycle, matching thefinalize()-driven disposal shown in the reproduction above (plain RxJS unsubscription alone wouldn't exercise this code path).I don't have a ROS 2 environment set up in the sandbox I used to write this patch, so I wasn't able to run
npm testlocally — please let me know if CI surfaces anything and I'll fix it up.