Skip to content

[refactor] Align observable ownership and guard teardown reentrancy - #1707

Open
minggangw wants to merge 15 commits into
RobotWebTools:developfrom
minggangw:fix-1706
Open

minggangw wants to merge 15 commits into
RobotWebTools:developfrom
minggangw:fix-1706

Conversation

@minggangw

@minggangw minggangw commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Align observable subscriptions with node-owned cleanup and coordinate reentrant Node/Context teardown using distinct teardown states.

Changes

  • Tie observable completion and destroyed state to the underlying subscription, including node-owned cleanup and explicit destruction retries.
  • Define separate NodeTeardownState and ContextTeardownState enums in lib/teardown_states.js, with independent _teardownState fields and owner-specific state semantics.
  • Ignore reentrant teardown calls and reject entity creation and spinning during or after teardown.
  • Defer native Context shutdown until registered nodes finish cleanup, retaining failed node teardown for retry.
  • Recheck entity creation immediately before native allocation after QoS validation, type metadata, and content-filter conversion callbacks.
  • Apply allocation guards to direct action constructors and lifecycle publishers.
  • Use non-private names for cross-class coordination methods and document shutdown guards with compact call stacks.
  • Add regression coverage for ownership, completion ordering, reentrancy, deferred shutdown, allocation rejection, and cleanup retries.
  • Use same-sandbox Sinon spies for call-order assertions and preserve native startup-failure coverage.
  • Update lifecycle documentation without changing public TypeScript signatures, and remove the unused action-goal cancellation flag.

Fixes #1706

Copilot AI balanced review requested due to automatic review settings November 28, 2026 15:15

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.

🔵 Needs a closer look

Completion callbacks now run synchronously during native-backed teardown, while tests cover only wrapper/subscription reentrancy, leaving node/context shutdown reentrancy needing human review and ROS runtime validation.

No open issues 🎉

What changed in this PR

Aligns RxJS observable subscriptions with rclnodejs’s node-owned entity lifecycle, so observable completion and destroyed state follow the underlying ROS 2 subscription.

Changes:

  • Adds a subscription subclass that completes its observable during entity finalization.
  • Shares subscription creation logic while preserving node registration and delegating wrapper destruction to the node.
  • Adds lifecycle regression tests and documents teardown, retry, and unsubscribe behavior.
File Description
types/​observable_subscription.d.ts Documents ownership, destroyed state, and completion semantics.
types/​node.d.ts Documents observable completion on subscription or node destruction.
test/​test-observable-subscription.js Adds registration, teardown, finalization, and destruction-retry coverage.
lib/​subscription.js Allows the subscription factory to instantiate subclasses.
lib/​observable_subscription.js Moves observable ownership into the subscription entity and delegates wrapper lifecycle operations.
lib/​node.js Extracts shared creation logic and registers observable subscription entities.

Review effort: Balanced

@coveralls

coveralls commented Nov 28, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 91.518% (+0.2%) from 91.357% — minggangw:fix-1706 into RobotWebTools:develop

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.

🟡 Changes recommended

A critical teardown race allows context shutdown to finalize native resources before in-progress node destruction finishes; the teardown paths need coordination and a regression test.

1 open issue

Review effort: Balanced

Comment thread lib/node.js Outdated
if (this.spinning) {
this.stop();
}
if (this._destroying) return;

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.

🔵 Needs a closer look

Lifecycle checks occur too late to prevent native allocation with released handles, and failed subscription destruction loses cleanup ownership, leaving finalizers skipped during bulk teardown.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

High severity Lifecycle state is not rechecked before native allocation

lib/​node.js:649

The registration guard runs after native allocation: a QoS validation callback that destroys the node or shuts down its context lets publisher/subscription creation continue with a released handle. Direct ActionClient/ActionServer construction also allocates before this guard. Recheck lifecycle state after QoS callbacks and before allocation (lib/node.js:909,1042), and add pre-allocation checks to the action constructors (lib/action/client.js:102, lib/action/server.js:120).

Medium severity Failed subscription destruction is lost from bulk teardown

lib/​node.js:1286

If obsSub.destroy() throws from syncHandles(), the registry has already removed the subscription. A later node.destroy() or context.shutdown() skips its finalizer, leaving obsSub.isDestroyed false and its observable uncompleted even after native teardown. Retain failed destructions and their attached events in a pending-cleanup collection, or restore ownership on synchronization failure, so bulk teardown still finalizes them.

Review effort: Balanced

@minggangw minggangw changed the title [refactor] Align observable subscriptions with node entity lifecycle [refactor] Unify subscription lifecycle and guard node/context teardown Nov 30, 2026
minggangw and others added 8 commits November 30, 2026 18:38
Reject creation and dispatch during teardown, preserve cleanup retries, and cover generic finalizer reentrancy independently of observable subscriptions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep the node's owning context live while forcing real native startup failure through an uninitialized context. Verify failed starts preserve stopped state and can recover without weakening teardown guards.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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.

🟡 Changes recommended

Entity creation can still pass a released node handle to native allocation, and failed subscription cleanup removes registrations needed for teardown retries.

3 open issues

Review effort: Balanced

Comment thread lib/node.js
Comment on lines +1023 to +1024
assertEntityCreationAllowed(this);
let subscription = SubscriptionClass.createSubscription(
this.#subject.complete();
this.#node.destroySubscription(this.#subscription);
}
this.#node.destroySubscription(this.#subscription);
@minggangw minggangw changed the title [refactor] Unify subscription lifecycle and guard node/context teardown [refactor] Align observable ownership and guard teardown reentrancy Dec 1, 2026

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.

🔵 Needs a closer look

Human assessment is needed because critical lifecycle edge cases can leave failed cleanup incomplete across retries or allow native entity allocation after node teardown.

3 open issues

Review effort: Balanced

This branch has not been deployed

No deployments
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.

Refactor observable subscriptions

3 participants