Skip to content
Merged
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
14 changes: 12 additions & 2 deletions docs/diagrams/foreground-loss-recovery.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ stateDiagram-v2
Preparing --> Requested : Request
Requested --> GraphReady : GraphReady [mapped]
GraphReady --> Serving : Serve / retire intent, lock
Serving --> Stopping : Ctrl-C
Preparing --> Dead : Die
Requested --> Dead : Die
GraphReady --> Dead : Die
Serving --> Dead : Die
Stopping --> Dead : Die
Dead --> Lost : Watch / release lock
Dead --> Lost : StartFails / same message
state advice <<choice>>
Expand All @@ -34,6 +36,12 @@ stateDiagram-v2
still holds its cleaned
intent and its lock.
end note
note right of Stopping
The owner's enrolled stop:
a pending relay cleanup
marker and its bridge
selection (cross_boot_stop).
end note
note right of Dead
The watch and the start's
own error race; both print
Expand All @@ -50,8 +58,9 @@ stateDiagram-v2
note right of Down
Starts the pool; retires a
lost start (failed-retained
or restoring) or a ready
run, port or none, data kept.
or restoring), a ready run,
port or none, or a stop lost
at cleanup-intent, data kept.
Removes containers a lost
VM brought back.
end note
Expand All @@ -68,6 +77,7 @@ stateDiagram-v2
classDef idle stroke-width:1.6px
class Preparing,Requested,GraphReady wait
class Serving live
class Stopping wait
class Dead,Lost fail
class Down,Up wait
class Recovered ok
Expand Down
Binary file modified docs/diagrams/foreground-loss-recovery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions docs/diagrams/foreground-loss-recovery.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions docs/guides/native-candidate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2935,6 +2935,16 @@ code 1, and what to run next, which depends on how far the start got:
(`relay_owner_refused`). The start asks the runtime once the VM is gone, and
for such a run it names no command and says to keep the native home for
inspection (HACK-2731); `hack down --recover` does not start the pool for it.
- During its own stop (you pressed Ctrl-C and the VM was lost before the stop
was confirmed, with the receipt at `cleanup-intent`): `hack down --recover`,
then `hack up` (HACK-2746). The stop runs inside the owner's enrolled relay
cleanup, and the runtime finishes it across the boot with data kept, once the
down has started the pool. `graph foreground-owner` reports it as
`cross_boot_stop`; a `hack up` run first names the same down
(`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.
- 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
16 changes: 16 additions & 0 deletions packages/runtime-core/src/provider/graph/bridges/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,22 @@ 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()
})
}

fn run_holds_slot(candidate: &Candidate, owner: &str, run: &str) -> bool {
state::read_bounded::<Store>(&root(candidate).join("state.json"), 65536).is_ok_and(|store| {
store.version == 1
Expand Down
108 changes: 108 additions & 0 deletions packages/runtime-core/src/provider/graph/dead_owner_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,45 @@ 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.
pub(super) fn interrupted_stop(
candidate: &Candidate,
root: &std::path::Path,
receipt: &Receipt,
owner: &Owner,
) -> bool {
let Some(marker) = receipt
.relay_cleanup
.as_ref()
.filter(|marker| marker.valid() && marker.phase == cleanup_enrollment::Phase::Pending)
else {
return false;
};
if receipt.phase != "cleanup-intent" || receipt.relay_startup.is_none() {
return false;
}
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))
}

fn validate(
intent: &Intent,
receipt: &Receipt,
Expand Down Expand Up @@ -3185,4 +3224,73 @@ mod tests {
.unwrap();
assert_eq!(pending(&fixture), true);
}

#[test]
fn a_stop_interrupted_inside_its_enrolled_relay_cleanup_is_a_cross_boot_stop() {
// HACK-2746 (real VM, 3 of 3): Ctrl-C, then the provider killed while the
// 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`.
let fixture = super::super::retained_startup::tests::Fixture::new();
let 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();
let stop = |receipt: &Receipt| interrupted_stop(&fixture.candidate, &root, receipt, &owner);
let marker = |boot: &str, phase: &str| {
let context = host_relay::context(&receipt.owner, boot).unwrap();
serde_json::from_value::<cleanup_enrollment::RelayCleanup>(json!({
"version":1,"runtime":context.runtime,"boot":context.boot,"operation":([3;16]),
"effect":([4;32]),"control_root":"/private/owned","phase":phase
}))
.unwrap()
};
// Before HACK-2746 nothing reported it: no relay startup or marker.
assert!(!stop(&receipt));
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());
receipt.relay_cleanup = Some(marker(&boot, "pending"));
// The persisted selection is required.
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})
};
state::write(
&root.join("relay-cleanup-bridges.json"),
&selection(json!({})),
)
.unwrap();
assert!(stop(&receipt));
state::write(&root.join("state.json"), &receipt).unwrap();
assert_eq!(
foreground_owner(&fixture.candidate, &fixture.run).unwrap()["cross_boot_stop"],
true
);
// A confirmed marker, one of another boot, or another phase is not one.
let mut confirmed = receipt.clone();
confirmed.relay_cleanup = Some(marker(&boot, "confirmed"));
assert!(!stop(&confirmed));
let mut foreign = receipt.clone();
foreign.relay_cleanup = Some(marker("33333333-3333-3333-3333-333333333333", "pending"));
assert!(!stop(&foreign));
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.
let mut unsettled = receipt.clone();
unsettled.relay_startup.as_mut().unwrap().control_only = false;
assert!(!stop(&unsettled));
state::write(
&root.join("relay-cleanup-bridges.json"),
&selection(json!({"1":{"assignment":{
"reservation":"r","run":receipt.run,"service":"web","generation":"g",
"container_id":"c","network_id":"n","boot_id":boot,"phase":"reserved"}}})),
)
.unwrap();
assert!(stop(&unsettled));
}
}
5 changes: 5 additions & 0 deletions packages/runtime-core/src/provider/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,11 @@ pub fn foreground_owner(candidate: &Candidate, run: &str) -> Result<Value, Candi
// (`relay_owner_refused`), so a caller names `hack down --recover` for
// 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
// `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),
// read from its host marker: only `hack down --recover` on a running pool
// finishes it, so a down finding the pool stopped starts it (HACK-2731).
Expand Down
60 changes: 60 additions & 0 deletions packages/runtime-core/src/provider/graph/retained_startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ fn published_owner_refused(candidate: &Candidate, run: &str) -> CandidateError {
"graph_foreground_owner_exited",
"The previous foreground owner of this run exited while its services were running and did not retire its publication. Run hack down --recover to stop the orphaned services with data retained, then hack up.",
),
// HACK-2746: the owner died during its own stop with its VM, inside its
// enrolled relay cleanup. Its services may still be listed; the same
// cross-boot recovery as for a ready run finishes that stop.
Ok(PublishedOwner::Exited) if exited_interrupted_stop(candidate, run) => error(
"graph_foreground_owner_exited",
"The previous foreground owner of this run exited during its own stop, before that stop was confirmed, and did not retire its publication. Run hack down --recover to finish that stop with data retained, then hack up.",
),
Ok(PublishedOwner::Exited) if exited_unadmitted_retained(candidate, run) => error(
"graph_foreground_owner_exited_unadmitted",
"The previous foreground owner of this run exited before its restore was admitted and did not retire its publication. Data is retained. Run hack down --recover to retire that attempt, then hack up.",
Expand Down Expand Up @@ -72,6 +79,20 @@ fn exited_unadmitted_retained(candidate: &Candidate, run: &str) -> bool {
.is_some_and(|(receipt, _)| receipt.phase == "stopped-data-retained")
}

/// A stop interrupted inside its enrolled relay cleanup that the cross-boot
/// dead-owner recovery finishes (`dead_owner_cleanup::interrupted_stop`).
/// Presentation only, as above.
fn exited_interrupted_stop(candidate: &Candidate, run: &str) -> bool {
Owner::load(candidate).ok().is_some_and(|owner| {
directory(candidate, run)
.ok()
.and_then(|root| load_at(root, run, &owner.token).ok())
.is_some_and(|(receipt, root)| {
super::dead_owner_cleanup::interrupted_stop(candidate, &root, &receipt, &owner)
})
})
}

/// A start the cross-boot dead-owner recovery admits (`failed-retained`,
/// `preparing` or `restoring`, with a settled relay startup). Presentation only,
/// as above.
Expand Down Expand Up @@ -935,6 +956,45 @@ pub(in crate::provider::graph) mod tests {
assert_eq!(error.code, "graph_foreground_owner_exited");
assert!(error.message.contains("hack down --recover"));
assert_eq!(state(&fixture), before);
// HACK-2746 (real VM): an owner whose VM was lost during its own stop, inside
// its enrolled relay cleanup, has the code of an exited ready owner, so
// `hack up` and its message name `hack down --recover`, then `hack up`.
// Before, it named no command ("no supported command can prove ...").
let fixture = Fixture::new();
let owner = Owner::load(&fixture.candidate).unwrap();
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","artifact":"e".repeat(64),"services":{}}))
.unwrap(),
);
let context = super::super::host_relay::context(
&receipt.owner,
owner.guest_boot_id.as_deref().unwrap(),
)
.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(&fixture.graph().join("state.json"), &receipt).unwrap();
state::write(
&fixture.graph().join("relay-cleanup-bridges.json"),
&json!({"version":1}),
)
.unwrap();
foreground::transport::fixture_exited_publication(&fixture.candidate, &fixture.run);
let before = state(&fixture);
let error = preflight(&fixture.candidate, &fixture.run).unwrap_err();
assert_eq!(error.code, "graph_foreground_owner_exited");
assert!(error.message.contains("during its own stop"));
assert!(error.message.contains("hack down --recover"));
assert!(error.message.contains("then hack up"));
assert_eq!(state(&fixture), before);
}

#[test]
Expand Down
18 changes: 17 additions & 1 deletion scripts/lib/tla-runtime-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3986,10 +3986,12 @@ const contracts: readonly ModelContract[] = [
// (#1253's message on the fixed runtime) restore one rule of the runtime or
// message before the VM-loss recovery fix. live-relay (HACK-2731) is
// #1273's message once ready, which ignored an unsettled relay startup.
// no-cross-boot-stop (HACK-2746) is the runtime and message before a stop
// interrupted at `cleanup-intent` was finished across the boot.
// restart-recovers is the reachability witness of a recovered restart.
name: "foreground-loss-recovery",
module: "ForegroundLossRecovery",
states: 596,
states: 752,
invariant: "NoRefusedAdvice",
fields: ['outcome = "refused"', 'lostAt = "Preparing"'],
alternativeWitnesses: [['outcome = "refused"', 'lostAt = "Requested"']],
Expand Down Expand Up @@ -4110,6 +4112,20 @@ const contracts: readonly ModelContract[] = [
'lostAt = "GraphReady"',
],
},
{
// HACK-2746 (real VM, 3 of 3): Ctrl-C, then the VM lost while the
// receipt read `cleanup-intent`. The message named
// `hack down --recover`, which kept `runtime_not_running`, and
// `hack up` refused: no command got the run back.
name: "no-cross-boot-stop",
negative: true,
invariant: "NoRefusedAdvice",
fields: [
'outcome = "refused"',
'msg = "downUp"',
'lostAt = "Stopping"',
],
},
{
// HACK-2714 (real VM, row 2): no cross-boot retirement of a start lost
// before ready, so the down keeps `runtime_not_running`.
Expand Down
Loading
Loading