holoscan::sensor_io::StageGuards
holoscan::sensor_io::StageGuards
Withhold a sensor’s teardown bodies from resources its forward bodies never took.
Core decides whether to invoke an operator’s teardown stages from a completion ledger, and that ledger does not distinguish a stage that did work from one that had none. callback_succeeded admits both kOk and kSkipped, run_forward_stage records a completion on that basis, and run_planned_teardown then gates kRelease on a recorded kAllocate and kStop on a recorded kArm or kStart (public/src/core/lifecycle_kernel.cpp). An operator whose kAllocate reserved nothing and correctly said so is therefore still asked to release. For most operators that is harmless. For one holding mapped device buffers, the release body runs against handles its allocate body never filled in, or ones a previous epoch already returned.
The operator cannot consult that ledger to defend itself. LifecycleContext offers cancellation, parameters, the parameter generation, the transition phase, clock resolution, and restart-target binding, and nothing that reports what an earlier stage of the same operator returned.
This object keeps that one missing fact where the operator can reach it: which of its own forward bodies actually took something. Only LifecycleStatus::kOk claims ownership, because a body that reports kSkipped states that it created no work. It states nothing about what an earlier epoch took, so a forward body that reports anything other than kOk leaves the record as it found it: acquiring and relinquishing are separate events, and only the stages listed below relinquish. kStop is then withheld from a device that was never armed, and kRelease from buffers that were never reserved. A withheld body is not invoked and reports LifecycleStatus::kSkipped, which Core treats as a distinct success rather than a failure, and which guard_skipped distinguishes afterwards from a body that ran and chose to skip its own work.
Forward bodies are never withheld. Core’s forward loop returns on the first stage that fails and rolls back without entering any later stage, so an operator’s kArm body is never invoked after its own kAllocate body failed, and a gate protecting against that ordering would guard a sequence the runtime does not produce. It would, however, withhold real work: the lifecycle guide’s recommended shape is to register a stage only where the operator genuinely has work, commonly just a matched kAllocate and kRelease pair, and gating each forward stage on a record written by an earlier one silently withholds every body of an operator that registered sparsely. Core would read the resulting run as a success in which the sensor never started.
Ownership is cleared by the stage that hands the resource back, not by the stage that stops using it:
- A successful
kStopclears only the start record. AnkArmbody’s acquisitions outlive a stop, so the shutdownkStopthat follows a restart’skStopmust still reach its body. The same contract binds the bodies this object guards: ankArmbody must not establish anything itskStopbody tears down, or Core’s retainedkArmcheckpoint outlives the state it stands for and the minimal restart suffix restores nothing. - A successful
kReleaseclears the arm and start records as well as its own. Released resources cannot still be armed or running, whatever an earlierkStopreported, so a release that follows a failed stop cannot leave a freed device describing itself as armed.
kRelease is gated on ownership alone and not on the device having stopped cleanly. Teardown must remain reachable after teardown fails, or a failed kStop would strand the buffers it was trying to quiesce: the one stage that can hand them back would be permanently unreachable, and the failure that most needs reclamation would be the failure that prevents it. A release body that needs to behave differently when the device never stopped can inspect armed and started, which still hold their pre-release values while it runs.
A kConfigure body resets the record first. Core reaches kConfigure only through a suffix that already ran kRelease, so this is a backstop for an operator that registers no release body rather than the ordinary path for clearing ownership.
The object is not thread-safe and does not need to be: Core quiesces an operator’s callbacks against each other. It allocates nothing and performs no I/O.
Methods
run
Run one stage body and record what it took, withholding teardown that owns nothing.
Only kStop and kRelease are ever withheld. A forward body always runs, because Core does not enter a forward stage whose predecessor failed.
A withheld body is recorded and can be inspected afterwards with guard_skipped, which distinguishes it from a body that ran and chose to skip its own work.
Returns: The body’s own status, or LifecycleStatus::kSkipped when kStage is a teardown stage with nothing recorded to tear down, in which case body was never invoked.
Template parameters
Lifecycle stage owning body.
Non-throwing body returning LifecycleStatus.
Parameters
Stage work to attempt.
allocated
Return whether a kAllocate body reported kOk and therefore owns resources.
This is what gates kRelease. A kAllocate body that returned kSkipped reserved nothing, so this stays false even though Core records the checkpoint as complete.
armed
Return whether an kArm body reported kOk and therefore owns device state.
This is what gates kStop, together with started. An kArm body that returned kSkipped armed nothing, so this stays false.
A successful kStop does not clear this, because an arm body’s acquisitions outlive a stop. Only kRelease or a kConfigure reset invalidates it. A re-entered kArm body does not: the restart suffix that re-enters kArm runs no kRelease, so a re-arm that reports kSkipped has performed no relinquishment and the previous epoch’s acquisition is still outstanding. Clearing the record there would withhold the shutdown kStop that owes that device its quiesce.
started
Return whether a kStart body reported kOk and therefore owns running state.
This is what gates kStop, together with armed. A kStart body that returned kSkipped started nothing, so this stays false.
Cleared by a successful kStop, by a successful kRelease, or by a kConfigure reset, and by nothing else. A re-entered kStart body does not clear it for the same reason a re-entered kArm body does not: reporting kSkipped is not a relinquishment. Core happens to run the kStop that does relinquish this before it re-enters kStart, so the record is already clear by then and the two readings agree on every sequence the runtime produces; the guard does not depend on that ordering to stay correct.
guard_skipped
Return whether a stage body was withheld because its precondition was unmet.
This reports a guard decision, not the body’s own kSkipped return.
A value no stage names cannot have been withheld, so it answers false rather than shifting by it. Nothing in the vocabulary is ever withheld silently as a result: the widest valid stage still fits the record.
Returns: true when run declined to invoke that stage’s body since the last reset, and false for any value outside the closed stage vocabulary.
Parameters
Stage to query.
any_guard_skipped
Return whether any stage body was withheld since the last reset.