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
7 changes: 6 additions & 1 deletion docs/guides/native-candidate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2944,7 +2944,12 @@ code 1, and what to run next, which depends on how far the start got:
(`graph_foreground_owner_exited`) and runs it for you as for a ready run.
Earlier versions named the down, which answered `runtime_not_running`, and
`hack up` refused the run. A stop the runtime cannot finish this way names no
command.
command and says why: the relay cleanup marker names no ended boot of the
pool, the bridge selection the stop saved is missing or does not match, or
the runtime is too old to report it (HACK-2757). An owner that dies during
its stop while its VM keeps running (the provider still alive) is not this
case: that boot has not ended, and no command retires it yet (HACK-2763). A
provider killed with the VM counts as ended even before `runtime recover`.
- Before any service was requested (while it resolved, reviewed, built or
pulled images, or selected the restore): for a run with a record (a resumed
stopped run, or a restart's old run), the same `hack down --recover`, then
Expand Down
129 changes: 115 additions & 14 deletions packages/runtime-core/src/provider/graph/bridges/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,20 +419,75 @@ pub(crate) fn relay_owner_provable(candidate: &Candidate, owner: &str, receipt:
pinned_predecessor_eligible(receipt) || run_holds_slot(candidate, owner, &receipt.run)
}

/// Whether the bridge selection an interrupted enrolled cleanup persisted
/// (`relay-cleanup-bridges.json`, for this run and pool owner) names any route
/// of the run, so a cross-boot recovery proves the dead relay owner by them
/// although the cleanup already released the slots (HACK-2746). Presentation
/// only; unreadable or foreign counts as none.
pub(crate) fn interrupted_selection_routes(root: &std::path::Path, owner: &str, run: &str) -> bool {
selection_path(root)
.and_then(|path| state::read_bounded::<Selection>(&path, 65536))
.is_ok_and(|selection| {
selection.version == 1
&& selection.owner == owner
&& selection.run == run
&& !selection.selected.is_empty()
})
/// Whether the cross-boot recovery of a stop interrupted inside its enrolled relay
/// cleanup, in `old_boot`, can prove the dead relay owner, as far as host files show
/// (HACK-2746, HACK-2757). While the bridge registry still records the run's slots,
/// `capture_previous_boot` selects them. Otherwise it reads the selection the
/// cleanup persisted (`relay-cleanup-bridges.json`) and accepts it only as
/// [`interrupted_selection_admits`] does here; an empty one then needs a settled
/// relay startup. Presentation only (`graph foreground-owner`): read without a lease
/// or the guest; an unreadable or foreign file counts as no proof, and the recovery
/// revalidates everything itself.
pub(crate) fn interrupted_stop_provable(
candidate: &Candidate,
graph_root: &std::path::Path,
owner: &crate::provider::state::Owner,
receipt: &Receipt,
old_boot: &str,
) -> bool {
if run_holds_slot(candidate, &owner.token, &receipt.run) {
return true;
}
let Ok(prior) =
selection_path(graph_root).and_then(|path| state::read_bounded::<Selection>(&path, 65536))
else {
return false;
};
let path = root(candidate).join("state.json");
let serial = match state::read_bounded::<Store>(&path, 65536) {
Ok(store) if store.version == 1 && store.owner == owner.token => store.next_launch_serial,
// A pool without the bridge registry reads it as empty (`load_store`).
Err(_) if owner.application_bridge.is_none() && !path.exists() => 0,
_ => return false,
};
interrupted_selection_admits(
&prior,
receipt,
&owner.token,
old_boot,
owner.application_bridge.map_or(0, |intent| intent.slots),
serial,
)
}

/// The checks `capture_previous_boot` applies to the persisted selection of an
/// interrupted enrolled cleanup (`pending_interrupted`): this pool owner, captured
/// in `old_boot` with no earlier boot or predecessor proof of its own, this run and
/// plan, this bridge capacity, a serial the registry has reached, and bindings that
/// [`validate_bindings`] accepts (route assignments running on that boot). An
/// empty one also needs a settled relay startup, which its predecessor proof
/// requires ([`select_predecessor`]).
fn interrupted_selection_admits(
prior: &Selection,
receipt: &Receipt,
owner: &str,
old_boot: &str,
capacity: u8,
serial: u64,
) -> bool {
prior.version == 1
&& prior.owner == owner
&& prior.boot == old_boot
&& prior.previous_boot.is_none()
&& prior.predecessor_owner.is_none()
&& prior.predecessor_absence.is_none()
&& prior.predecessor_exit.is_none()
&& prior.run == receipt.run
&& prior.plan == receipt.plan_id
&& prior.capacity == capacity
&& prior.serial <= serial
&& validate_bindings(receipt, prior, None).is_ok()
&& (!prior.selected.is_empty() || pinned_predecessor_eligible(receipt))
}

fn run_holds_slot(candidate: &Candidate, owner: &str, run: &str) -> bool {
Expand Down Expand Up @@ -1354,6 +1409,52 @@ mod tests {
.unwrap()
}

#[test]
fn an_interrupted_selection_is_admitted_only_as_capture_previous_boot_admits_it() {
// HACK-2757 (review of #1290): the advice for a Ctrl-C stop lost with its
// VM names `hack down --recover` only for a persisted selection the
// recovery's own capture accepts.
let (selected, store) = live_selection();
let assignment = store.slots[&0].clone();
let receipt = receipt_for_assignment(&selected, &assignment);
let boot = selected.boot.clone();
let admits = |prior: &Selection| {
interrupted_selection_admits(prior, &receipt, &selected.owner, &boot, 3, 1)
};
assert!(admits(&selected));
let changed = |change: &dyn Fn(&mut Selection)| {
let mut prior = selected.clone();
change(&mut prior);
admits(&prior)
};
assert!(!changed(&|prior| prior.owner = "9".repeat(32)));
assert!(!changed(
&|prior| prior.boot = "22222222-2222-2222-2222-222222222222".into()
));
assert!(!changed(&|prior| prior.previous_boot = Some("0".repeat(8))));
assert!(!changed(
&|prior| prior.predecessor_owner = Some("4".repeat(64))
));
assert!(!changed(&|prior| prior.run = "9".repeat(32)));
assert!(!changed(&|prior| prior.plan = "9".repeat(64)));
assert!(!changed(&|prior| prior.capacity = 4));
assert!(!changed(&|prior| prior.serial = 2));
// Route assignments must be running on that boot, with their helpers.
assert!(!changed(&|prior| {
prior.selected.get_mut(&0).unwrap().assignment.phase = "stopped".into()
}));
assert!(!changed(&|prior| {
prior.selected.get_mut(&0).unwrap().assignment.boot_id = "other".into()
}));
assert!(!changed(&|prior| prior
.selected
.get_mut(&0)
.unwrap()
.helper = None));
// An empty one needs a settled relay startup: this receipt has none.
assert!(!changed(&|prior| prior.selected.clear()));
}

#[test]
fn exited_current_run_relay_is_selected_before_live_bridge_capture() {
let (selected, mut store) = live_selection();
Expand Down
163 changes: 142 additions & 21 deletions packages/runtime-core/src/provider/graph/dead_owner_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,25 @@ pub(super) fn unconfirmed_start(receipt: &Receipt) -> bool {
/// A stop its owner left inside its enrolled relay cleanup, at `cleanup-intent`,
/// when its VM was lost (HACK-2746, real VM: Ctrl-C, then the provider killed while
/// the receipt read `cleanup-intent`). The relay cleanup marker is still `pending`
/// for one of the pool's recorded boots and the bridge selection it persisted is
/// beside it. The cross-boot recovery admits that receipt once the pool boots again
/// (`interrupted_enrollment` in `prepare`), provided it can prove the dead relay
/// owner: by the routes that selection names, or by a settled relay startup. So a
/// caller starts a stopped pool, or names `hack down --recover`, only for one this
/// returns. Selection only, read from host files; the recovery revalidates the dead
/// owner, the ended boot and every inventory.
/// for a boot that has ended, and the bridge selection it persisted is beside it.
/// The cross-boot recovery admits that receipt once the pool boots again
/// (`interrupted_enrollment` in `execute`, and in `archive_completed_prior`; the
/// explicit cleanup's `prepare` refuses it), provided it can prove the dead relay
/// owner: by the run's slots, or by a persisted selection its capture accepts
/// (`bridges::cleanup::interrupted_stop_provable`). So a caller starts a stopped
/// pool, or names `hack down --recover`, only for one this returns.
///
/// The ended boot is one of the pool's earlier boots, or its last recorded boot
/// unless that boot is still live: the record says `running` and its provider is
/// alive (the check `runtime status` makes, [`identity::provider_gone`]). A provider
/// killed with its VM leaves the record `running` with the dead boot until
/// `runtime recover` writes `recovered-unclean`; that boot has ended (`status` reads
/// it `process-exited`), so it counts (#1291 review). A live boot never counts
/// (HACK-2757): the cross-boot recovery never selects the current boot, so an owner
/// that died while its VM kept running is not one. A provider whose state cannot
/// be read counts as live, so nothing is promised. Selection only, read from host
/// files; the recovery revalidates the dead owner, the ended boot and every
/// inventory.
pub(super) fn interrupted_stop(
candidate: &Candidate,
root: &std::path::Path,
Expand All @@ -119,17 +131,30 @@ pub(super) fn interrupted_stop(
if receipt.phase != "cleanup-intent" || receipt.relay_startup.is_none() {
return false;
}
let current = owner.guest_boot_id.as_deref().filter(|_| live_boot(owner));
let mut boots: Vec<String> = owner.guest_boot_id.iter().cloned().collect();
boots
.extend(crate::provider::boot_history::earlier_boots(candidate, owner).unwrap_or_default());
let enrolled_boot = boots.iter().any(|boot| {
host_relay::context(&receipt.owner, boot)
.is_ok_and(|context| marker.runtime == context.runtime && marker.boot == context.boot)
});
enrolled_boot
&& exists(&root.join("relay-cleanup-bridges.json")).unwrap_or(false)
&& (bridges::cleanup::relay_owner_provable(candidate, &owner.token, receipt)
|| bridges::cleanup::interrupted_selection_routes(root, &owner.token, &receipt.run))
let Some(old_boot) = boots.iter().find(|boot| {
current != Some(boot.as_str())
&& host_relay::context(&receipt.owner, boot).is_ok_and(|context| {
marker.runtime == context.runtime && marker.boot == context.boot
})
}) else {
return false;
};
exists(&root.join("relay-cleanup-bridges.json")).unwrap_or(false)
&& bridges::cleanup::interrupted_stop_provable(candidate, root, owner, receipt, old_boot)
}

/// Whether the pool record's boot is still live: `running`, with a provider that
/// is not proven gone. An unreadable provider state counts as live.
fn live_boot(owner: &Owner) -> bool {
owner.phase == "running"
&& owner
.process
.as_ref()
.is_none_or(|process| !identity::provider_gone(process).unwrap_or(false))
}

fn validate(
Expand Down Expand Up @@ -3231,8 +3256,8 @@ mod tests {
// receipt read `cleanup-intent`. The enrolled cleanup's marker is pending for
// the run's boot, its bridge selection is persisted beside it, and its slots
// were already released. The cross-boot recovery admits that receipt
// (`interrupted_enrollment`), so `graph foreground-owner` reports
// `cross_boot_stop` and `hack up` names `hack down --recover`.
// (`interrupted_enrollment` in `execute`), so `graph foreground-owner`
// reports `cross_boot_stop` and `hack up` names `hack down --recover`.
let fixture = super::super::retained_startup::tests::Fixture::new();
let owner = Owner::load(&fixture.candidate).unwrap();
let boot = owner.guest_boot_id.clone().unwrap();
Expand All @@ -3256,7 +3281,9 @@ mod tests {
assert!(!stop(&receipt));
let selection = |selected: serde_json::Value| {
json!({"version":1,"owner":owner.token,"boot":boot,"run":receipt.run,
"plan":receipt.plan_id,"capacity":2,"serial":1,"selected":selected})
"plan":receipt.plan_id,
"capacity":owner.application_bridge.map_or(0, |intent| intent.slots),
"serial":0,"selected":selected})
};
state::write(
&root.join("relay-cleanup-bridges.json"),
Expand All @@ -3279,8 +3306,10 @@ mod tests {
let mut ready = receipt.clone();
ready.phase = "ready-observed".into();
assert!(!stop(&ready));
// Without a settled relay startup, only the persisted routes prove the dead
// relay owner: none, then one.
// Without a settled relay startup, only persisted routes the recovery's
// capture accepts prove the dead relay owner (HACK-2757): none, nor a
// reserved route without its helper evidence, which that capture refuses
// (`bridges::cleanup` tests cover the accepted shape).
let mut unsettled = receipt.clone();
unsettled.relay_startup.as_mut().unwrap().control_only = false;
assert!(!stop(&unsettled));
Expand All @@ -3291,6 +3320,98 @@ mod tests {
"container_id":"c","network_id":"n","boot_id":boot,"phase":"reserved"}}})),
)
.unwrap();
assert!(stop(&unsettled));
assert!(!stop(&unsettled));
// A selection of another plan, capacity or boot is refused by that capture.
for changed in [
json!({"plan":"9".repeat(64)}),
json!({"capacity":9}),
json!({"boot":"33333333-3333-3333-3333-333333333333"}),
json!({"previous_boot":"33333333-3333-3333-3333-333333333333"}),
] {
let mut value = selection(json!({}));
for (key, field) in changed.as_object().unwrap() {
value[key] = field.clone();
}
state::write(&root.join("relay-cleanup-bridges.json"), &value).unwrap();
assert!(!stop(&receipt), "{changed}");
}
state::write(
&root.join("relay-cleanup-bridges.json"),
&selection(json!({})),
)
.unwrap();
assert!(stop(&receipt));
}

#[test]
fn an_owner_that_died_while_its_vm_kept_running_is_not_a_cross_boot_stop_but_a_killed_provider_is()
{
// HACK-2757 (review of #1290): the cross-boot recovery never selects the
// pool's current boot, so while the pool runs a marker of that boot is not a
// cross-boot stop; `hack up` and the advice do not send it to
// `hack down --recover`. Once the pool has booted again, the same marker is
// one of an earlier boot.
let fixture = super::super::retained_startup::tests::Fixture::new();
let mut owner = Owner::load(&fixture.candidate).unwrap();
let boot = owner.guest_boot_id.clone().unwrap();
let root = fixture.graph();
let mut receipt = fixture.receipt();
receipt.phase = "cleanup-intent".into();
receipt.relay_startup = Some(serde_json::from_value(json!({"control_only":true,"guest_root":null,"control_root":"/private/owned","control_generation":"a".repeat(32),"artifact":"e".repeat(64),"services":{}})).unwrap());
let context = host_relay::context(&receipt.owner, &boot).unwrap();
receipt.relay_cleanup = Some(
serde_json::from_value(json!({
"version":1,"runtime":context.runtime,"boot":context.boot,"operation":([3;16]),
"effect":([4;32]),"control_root":"/private/owned","phase":"pending"
}))
.unwrap(),
);
state::write(
&root.join("relay-cleanup-bridges.json"),
&json!({"version":1,"owner":owner.token,"boot":boot,"run":receipt.run,
"plan":receipt.plan_id,
"capacity":owner.application_bridge.map_or(0, |intent| intent.slots),
"serial":0,"selected":{}}),
)
.unwrap();
// The VM was lost and `runtime recover` ran: the pool is stopped with that
// boot recorded.
assert_ne!(owner.phase, "running");
assert!(interrupted_stop(
&fixture.candidate,
&root,
&receipt,
&owner
));
// #1291 review: the provider was killed and nothing rewrote the record, so
// it still says `running` with the dead boot (`runtime status` reads it
// `process-exited`). That boot has ended: still a cross-boot stop, so the
// lost-VM advice and `hack up` name `hack down --recover` (HACK-2746 rows).
owner.phase = "running".into();
assert!(identity::provider_gone(owner.process.as_ref().unwrap()).unwrap());
assert!(interrupted_stop(
&fixture.candidate,
&root,
&receipt,
&owner
));
// The owner died while its VM kept running: a live provider, so the boot is
// still current and nothing is promised.
owner.process = Some(identity::observe(std::process::id() as i32).unwrap());
assert!(!interrupted_stop(
&fixture.candidate,
&root,
&receipt,
&owner
));
// The pool booted again: the run's boot is now an earlier one.
owner.previous_guest_boot_id = Some(boot.clone());
owner.guest_boot_id = Some("44444444-4444-4444-4444-444444444444".into());
assert!(interrupted_stop(
&fixture.candidate,
&root,
&receipt,
&owner
));
}
}
5 changes: 3 additions & 2 deletions packages/runtime-core/src/provider/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2128,8 +2128,9 @@ pub fn foreground_owner(candidate: &Candidate, run: &str) -> Result<Value, Candi
// a live phase only with this. Selection only, like the fields around it.
"cross_boot_relay": bridges::cleanup::relay_owner_provable(candidate, &owner.token, &receipt),
// A stop its owner left at `cleanup-intent` inside its enrolled relay
// cleanup, which the cross-boot recovery retires once the pool boots again
// (HACK-2746), so a caller starts a stopped pool or names
// cleanup in a boot that has ended, which the cross-boot recovery retires
// once the pool boots again (HACK-2746). Never a boot whose provider is
// still alive (HACK-2757). A caller starts a stopped pool or names
// `hack down --recover` for it. Selection only, like the fields around it.
"cross_boot_stop": dead_owner_cleanup::interrupted_stop(candidate, &root, &receipt, &owner),
// A lost-removal repair whose storage barrier has not confirmed (HACK-2719),
Expand Down
Loading
Loading