Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/action/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import DistroUtils from '../distro.js';
import Entity from '../entity.js';
import loader from '../interface_loader.js';
import QoS from '../qos.js';
import { assertEntityCreationAllowed } from '../lifecycle_state.js';
import { TypeValidationError, ActionError, OperationError } from '../errors.js';
import { assertValidMessage } from '../message_validation.js';

Expand Down Expand Up @@ -55,6 +56,7 @@ class ActionClient extends Entity {
constructor(node, typeClass, actionName, options) {
super(null, null, options);

assertEntityCreationAllowed(node);
this._node = node;
this._typeClass = loader.loadInterface(typeClass);
this._actionName = actionName;
Expand Down Expand Up @@ -99,6 +101,7 @@ class ActionClient extends Entity {

let type = this.typeClass.type();

assertEntityCreationAllowed(node);
this._handle = rclnodejs.actionCreateClient(
node.handle,
actionName,
Expand Down
3 changes: 3 additions & 0 deletions lib/action/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Entity from '../entity.js';
import { CancelResponse, GoalEvent, GoalResponse } from './response.js';
import loader from '../interface_loader.js';
import QoS from '../qos.js';
import { assertEntityCreationAllowed } from '../lifecycle_state.js';
import ServerGoalHandle from './server_goal_handle.js';
import { TypeValidationError } from '../errors.js';

Expand Down Expand Up @@ -87,6 +88,7 @@ class ActionServer extends Entity {
) {
super(null, null, options);

assertEntityCreationAllowed(node);
this._node = node;
this._typeClass = loader.loadInterface(typeClass);
this._actionName = actionName;
Expand Down Expand Up @@ -117,6 +119,7 @@ class ActionServer extends Entity {

let type = this.typeClass.type();

assertEntityCreationAllowed(node);
this._handle = rclnodejs.actionCreateServer(
node.handle,
node.getClock().handle,
Expand Down
1 change: 0 additions & 1 deletion lib/action/server_goal_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class ServerGoalHandle {
this._actionServer = actionServer;
this._goalInfo = goalInfo;
this._goalRequest = goalRequest;
this._cancelRequested = false;
this._deferred = new Deferred();

this._handle = rclnodejs.actionAcceptNewGoal(
Expand Down
96 changes: 70 additions & 26 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import rclnodejs from './native_loader.js';
import { OperationError } from './errors.js';
import { LifecycleState } from './lifecycle_state.js';

let defaultContext = null;

Expand Down Expand Up @@ -66,7 +67,7 @@ class Context {
*/
constructor(domainId) {
this._handle = rclnodejs.createContext();
this._isShutdown = false;
this._lifecycleState = LifecycleState.OPEN;
this._nodes = [];
this._domainId = domainId;
Context._instances.push(this);
Expand Down Expand Up @@ -110,7 +111,7 @@ class Context {
* @returns {boolean} True if context has been shutdown; otherwise false
*/
isShutdown() {
return this._isShutdown;
return this._lifecycleState === LifecycleState.CLOSED;
}

/**
Expand Down Expand Up @@ -142,31 +143,50 @@ class Context {

/**
* Shut down the context including destroying all nodes.
* Reentrant shutdown is ignored, and node creation is rejected while
* shutdown is in progress.
* If a node is already being destroyed, native shutdown is deferred until
* its teardown finishes. Cleanup failures leave the context open for retry.
* @returns {undefined}
* @throws {Error} If there is a problem shutting down the context.
*/
shutdown() {
if (this.isShutdown()) return;
if (!this._isOpen()) return;

// shutdown and remove all nodes
for (const node of this.nodes) {
node.destroy();
}
this._lifecycleState = LifecycleState.CLOSING;
try {
// shutdown and remove all nodes
for (const node of this.nodes) {
node.destroy();
}

if (this.isInitialized()) {
rclnodejs.shutdown(this.handle);
}
// A completion callback can call shutdown() during node.destroy().
// Reentrant destroy() returns early, leaving that node registered.
// Defer native shutdown until the outer teardown finishes.
if (this._nodes.length > 0) {
this._lifecycleState = LifecycleState.WAITING_FOR_NODE_TEARDOWN;
return;
}

this._isShutdown = true;
if (this.isInitialized()) {
rclnodejs.shutdown(this.handle);
}

// remove context from _instances[]
const index = Context._instances.indexOf(this);
if (index > -1) {
Context._instances.splice(index, 1);
}
this._lifecycleState = LifecycleState.CLOSED;

if (this.isDefaultContext()) {
defaultContext = null;
// remove context from _instances[]
const index = Context._instances.indexOf(this);
if (index > -1) {
Context._instances.splice(index, 1);
}

if (this.isDefaultContext()) {
defaultContext = null;
}
} finally {
if (this._lifecycleState === LifecycleState.CLOSING) {
this._lifecycleState = LifecycleState.OPEN;
}
}
}

Expand All @@ -181,23 +201,47 @@ class Context {
}
}

onNodeCreated(node) {
if (!node) {
throw new OperationError('Node must be defined to add to Context', {
code: 'NODE_UNDEFINED',
entityType: 'context',
});
_onNodeDestructionFinished(node, completed) {
let resumeShutdown;
try {
if (!completed) return;
this.onNodeDestroyed(node);
resumeShutdown =
this._lifecycleState === LifecycleState.WAITING_FOR_NODE_TEARDOWN;
} finally {
if (this._lifecycleState === LifecycleState.WAITING_FOR_NODE_TEARDOWN) {
this._lifecycleState = LifecycleState.OPEN;
}
}
if (resumeShutdown) this.shutdown();
}

if (this.isShutdown()) {
_isOpen() {
return this._lifecycleState === LifecycleState.OPEN;
}

_assertNodeCreationAllowed() {
if (!this._isOpen()) {
const state = this.isShutdown() ? 'shutdown' : 'shutting down';
throw new OperationError(
'Can not add a Node to a Context that is shutdown',
`Can not add a Node to a Context that is ${state}`,
{
code: 'CONTEXT_SHUTDOWN',
entityType: 'context',
}
);
}
}

onNodeCreated(node) {
if (!node) {
throw new OperationError('Node must be defined to add to Context', {
code: 'NODE_UNDEFINED',
entityType: 'context',
});
}

this._assertNodeCreationAllowed();

if (this._nodes.includes(node)) {
// do nothing
Expand Down
35 changes: 35 additions & 0 deletions lib/lifecycle_state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2026, The Robot Web Tools Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export const LifecycleState = Object.freeze({
OPEN: 'open',
CLOSING: 'closing',
FINALIZING: 'finalizing',
WAITING_FOR_NODE_TEARDOWN: 'waiting_for_node_teardown',
CLOSED: 'closed',
});

/** @ignore */
export function assertEntityCreationAllowed(node) {
if (node._lifecycleState !== LifecycleState.OPEN) {
throw new Error(
'Cannot create entities while the node is being destroyed or has been destroyed.'
);
}
if (node.context._lifecycleState !== LifecycleState.OPEN) {
throw new Error(
'Cannot create entities while the context is shutting down or shut down.'
);
}
}
Loading
Loading