Migrating to stop execution (SDK 2.43.0)¶
This page is for node authors whose node was written before SDK 2.43.0. It explains what changed in how a node is stopped, what is deprecated, how to move each kind of on_stop body to the right hook, and how to verify the result. After reading it you can migrate a Python or C++ node in one sitting and know that it keeps working on every engine version in the field.
What changed, in one paragraph¶
Before 2.43.0 the engine had one way to stop a graph, and it ended the run and released every node in one go. Running the graph again meant Initialize and Configure again. The engine now has two verbs. Stop execution (StopExecutionNodeGraph) ends the run and leaves every node configured, registered and wired, so the next Run needs no Initialize and no Configure. Stop and cleanup (StopAndCleanupNodeGraph) is the full teardown. The old Stop (StopNodeGraph) still exists with unchanged behaviour and is deprecated. On the node side that gives one new hook and one deprecated hook:
| Hook | Status | Called by | Purpose |
|---|---|---|---|
on_stop_execution(trigger_context) / onStopExecution() |
new, optional | Stop execution, and as the first step of every hard stop | End the current run: make on_execute return, stop per-run work. Keep configured resources. |
on_cleanup() / onCleanup() |
unchanged name, now the final step | Hard stop only | Release everything on_configure acquired. The counterpart of Initialize/Configure. |
on_stop(trigger_context) / onStop() |
deprecated, no longer required | Hard stop only, after the SDK teardown and before on_cleanup |
Still called so unmigrated nodes keep releasing their resources. Removed in a later major. |
onStopRequested() |
removed (C++ only) | — | Was the pre-join unblock hook. Rename it to onStopExecution(); the compiler flags every leftover. |
The full lifecycle of a node is now: Initialize → on_configure → on_execute → on_stop_execution → on_cleanup.
Deprecation notice¶
StopNodeGraphandStopNodeare marked deprecated in the entities contract since 2.58.0. Their behaviour is unchanged. New callers useStopExecutionNodeGraphto end a run andStopAndCleanupNodeGraphto release a graph.on_stop/onStopare deprecated since SDK 2.43.0. A node service logs one warning per node class that still overrideson_stopwithouton_stop_execution. Both hook and RPC are removed together in a later major release; until then every engine version keeps working with every SDK version, see Compatibility.onStopRequested(C++) is removed in 2.43.0.
The one rule¶
Put code where its lifetime is:
| The code ... | belongs in |
|---|---|
| acquires something the node needs while it is configured (open a device, connect a client, start a thread, load a model) | on_configure |
makes the current run end (set a flag the on_execute loop reads, notify a condition variable, cancel a timer, tell the robot to stop the motion in progress) |
on_stop_execution |
| suspends work without ending the run | on_pause / on_resume |
releases what on_configure acquired |
on_cleanup |
Two consequences that trip people up:
- Reset per-run state at the top of
on_execute, not inon_stop_execution. The engine ends the run on every armed node, whether or not it was executing, and the next run starts with a freshon_execute. on_cleanupruns after the SDK transports are gone. Publishing or firing a trigger from it fails. Before 2.43.0 it ran before the dataflow was destroyed; a node that relied on that publishes at the end ofon_executeinstead.
Migrating a Python node¶
Look at your on_stop body. It is one of five shapes.
Shape A: it sets a flag so the on_execute loop returns. Rename the method to on_stop_execution. Done.
# before
def on_stop(self, trigger_context=None):
self._should_stop = True
# after
def on_stop_execution(self, trigger_context=None):
self._should_stop = True
def on_execute(self, trigger_context=None):
self._should_stop = False # per-run reset at the top
while not self._should_stop:
...
Shape B: it only logs, or is pass. Delete it. on_stop is no longer abstract. If the node has a Stop input port and you want a Stop trigger to end the run softly, add an empty on_stop_execution override; without one the SDK treats the node as unmigrated and a Stop trigger keeps tearing it down.
Shape C: it tears things down (destroys a ROS node, shuts an executor, joins a thread, closes a connection). Move the body to on_cleanup. If a thread must stop before the node can be released, split it: signal in on_stop_execution, join in on_cleanup. Never call rclpy.shutdown() from either hook if the process hosts more than one node.
Shape D: it delegates to on_cleanup() (or on_cleanup delegates to on_stop). Delete the delegation and keep one body in on_cleanup. The SDK guarantees on_cleanup runs exactly once per lifecycle, so the extra call was only ever a double release.
Shape E: it commands hardware. Decide per statement: "stop this motion" is on_stop_execution; "release the hold on the shared robot connection" is on_cleanup.
A node that used on_cleanup as its loop breaker (because it used to run first) moves that line to on_stop_execution, and a node that published a final message from on_cleanup publishes it at the end of on_execute instead.
Then check three things: on_execute resets its per-run state at the top; on_stop_execution tolerates being called when nothing runs; on_cleanup tolerates resources that were never opened (the first Initialize already runs it).
Migrating a C++ node¶
| Your node has | Do |
|---|---|
void onStopRequested() override |
Rename to void onStopExecution() override. The body (set flag, notify) is exactly right. |
void onStop() override that tears down (stops a loop thread, disconnects the robot) |
Rename to void onCleanup() override, or merge into an existing onCleanup. |
void onStop() override {} or log-only |
Delete it. onStop() is no longer pure virtual. |
a blocking onExecute() waiting on a condition variable |
Clear the latched stop flag at the top of onExecute(); keep the wait; make callbacks (onMessage etc.) check a running_ flag so they stop acting between runs. |
| a worker thread | Signal it in onStopExecution() (non-blocking), join it in onCleanup() or at the end of onExecute(), never on the stop thread. |
The context overload onStopExecution(const std::optional<TriggerContext>&) exists as for onExecute.
What the SDK now guarantees¶
on_stop_executionruns at most once per run, andon_cleanup(and the deprecatedon_stop) exactly once per lifecycle, whatever combination of soft and hard stops the engine sends. You do not need your own once-guards.- Order is always
on_stop_execution→ SDK teardown →on_stop→on_cleanup. A hard stop never overtakes anon_stop_executionstill running on another thread. - Every hard stop (stop and cleanup, the deprecated Stop, a re-Initialize or Configure of a running node, service shutdown) waits up to
stop_execution_join_timeout_s(default 5 s, aservice_config.yamlkey) foron_executeto return before it tears down. Before SDK 2.46.2 only stop and cleanup waited. If your loop ignoreson_stop_execution, the SDK logs a warning and tears down after the timeout. A stop called from insideon_executedoes not wait on itself. - After a stop execution, a
Starttoken that was already on the wire for the ended run is dropped, and anon_executethat returns late does not fire itsStartoutputs. The next Run re-arms the node.
Compatibility¶
Nothing rejects anything. Every mismatch is reported as a named resultCode 1 on the graph verb.
| Engine | Node SDK | Stop (deprecated) | Stop execution | Stop and cleanup |
|---|---|---|---|---|
| older than 2.32 | older than 2.43 | works as today | not available | not available |
| older than 2.32 | 2.43 or newer | works: the SDK runs on_stop_execution, the teardown, on_stop (no-op if migrated) and on_cleanup |
not available | not available |
| 2.32 or newer | older than 2.43 | works | refused; the message names the nodes that need SDK 2.43 | works; the engine sends the old Stop to those nodes |
| 2.32 or newer | 2.43 or newer | works | works | works |
So a migrated node can be deployed before the engine it runs under is upgraded, and an unmigrated node keeps working after it.
Verifying¶
- Unit-test the hooks directly: call
stop_execution()while a thread is insideon_executeand asserton_executereturns; callstop_and_cleanup()and assert the resource is closed once; callstop_and_cleanup()twice and assert nothing runs twice. The smart home tutorial's test does exactly that, see Testing. - The Mock Runner has no stop action, so it cannot exercise these hooks.
- On an engine: Run, Stop execution, Run again without Configure, then Cleanup. The node's log should show
on_configureonce,on_stop_executiontwice,on_cleanuponce.