diff --git a/packages/runtime-core/src/provider/engine/command_lease_tests.rs b/packages/runtime-core/src/provider/engine/command_lease_tests.rs index 91a0b3e66..d8ca31b55 100644 --- a/packages/runtime-core/src/provider/engine/command_lease_tests.rs +++ b/packages/runtime-core/src/provider/engine/command_lease_tests.rs @@ -10,6 +10,7 @@ use super::*; use crate::provider::lifecycle::COMMAND_LEASE_WAIT; use crate::provider::{Profile, state}; +use std::cell::Cell; use std::fs; use std::os::unix::fs::DirBuilderExt; use std::path::PathBuf; @@ -348,36 +349,42 @@ fn the_requesting_side_of_exec_waits_as_long_as_a_start_and_the_owner_side_keeps assert_eq!(command_lease_deadline(now, near, COMMAND_LEASE_WAIT), near); } -/// HACK-2729, scaled (the owner's 15 s is 300 ms): a project stop holds the lease -/// for 600 ms. At the owner-side bound the wait refuses with the exact -/// project-stop refusal the CLI reads; the requesting side's longer bound -/// outlasts the stop and acquires right after it, before any check of its own. +/// HACK-2729: the owner deadline expires while the project stop still holds +/// the lease. The requester retries and acquires after the stop releases it. #[test] fn a_requested_command_waits_out_a_project_stop_that_the_owner_bound_refuses() { let (_home, candidate) = running_pool(); - let hold = Duration::from_millis(600); - let (_release, holder) = hold_lease_as_project_stop(&candidate, hold); + let root = candidate.state_root.join("run/smolvm"); + let held_stop = Cell::new(Some({ + let _stop = state::RecordProjectStop::begin(); + state::Lock::acquire(&root).unwrap() + })); - let started = Instant::now(); - let refused = - Engine::connect_command(&candidate, started + Duration::from_millis(300), &|| Ok(())) - .err() - .unwrap(); + let owner_deadline = Instant::now() + Duration::from_millis(300); + let refused = Engine::connect_command(&candidate, owner_deadline, &|| Ok(())) + .err() + .unwrap(); + assert!(Instant::now() >= owner_deadline); assert_eq!(refused.code, "provider_busy"); assert_eq!(refused.message, PROJECT_STOP_REFUSAL); - assert!(started.elapsed() < hold); - // The backoff caps at 50 ms and is not scaled with the hold. Allow ten poll - // intervals for the holder to run, unlock and be observed on CI (HACK-2741). + // The guard runs before each attempt. Keep the stop through the first + // attempt and release it on retry, regardless of how long CI was paused. + let first_attempt = Cell::new(true); let after = Engine::connect_command_request( &candidate, - started + hold + Duration::from_millis(500), - &|| Ok(()), + Instant::now() + Duration::from_secs(5), + &|| { + if !first_attempt.replace(false) { + drop(held_stop.take()); + } + Ok(()) + }, ) .err() .unwrap(); - assert_waited_then_acquired(started, hold, &after); - holder.join().unwrap(); + assert_eq!(after.code, PAST_ACQUISITION, "{}", after.message); + assert!(held_stop.into_inner().is_none()); } /// HACK-2729: a stop that outlasts the requesting side's bound is still refused diff --git a/packages/runtime-core/src/provider/graph/foreground/dependency_refresh.rs b/packages/runtime-core/src/provider/graph/foreground/dependency_refresh.rs index 9f67fdece..fd0d28498 100644 --- a/packages/runtime-core/src/provider/graph/foreground/dependency_refresh.rs +++ b/packages/runtime-core/src/provider/graph/foreground/dependency_refresh.rs @@ -410,7 +410,7 @@ mod automatic_tests { mod owner_lease_tests { use super::*; use crate::provider::engine::command_lease_tests::{ - PAST_ACQUISITION, PROJECT_STOP_REFUSAL, hold_lease, hold_lease_as_project_stop, + PROJECT_STOP_REFUSAL, assert_waited_then_acquired, hold_lease, hold_lease_as_project_stop, running_pool, }; use std::cell::Cell; @@ -448,9 +448,9 @@ mod owner_lease_tests { } /// HACK-2729: the requesting side's selection, before anything is sent to the - /// owner, outlasts a project stop (scaled: 600 ms against a 700 ms bound), and a - /// stop past its bound is refused with the raw, uncaused `provider_busy` naming - /// the stop. That is the refusal the CLI reports as "nothing ran". + /// owner, outlasts a project stop, and a stop past its bound is refused with + /// the raw, uncaused `provider_busy` naming the stop. That is the refusal the + /// CLI reports as "nothing ran". #[test] fn the_requested_selection_waits_out_a_project_stop_and_refuses_one_past_its_bound() { let (_home, candidate) = running_pool(); @@ -458,21 +458,21 @@ mod owner_lease_tests { let hold = Duration::from_millis(600); let (_release, holder) = hold_lease_as_project_stop(&candidate, hold); let started = Instant::now(); - let after = requested_selection(&candidate, &run, started + Duration::from_millis(700)) + // The backoff caps at 50 ms and is not scaled with the hold. Allow ten + // poll intervals for the holder to run, unlock and be observed on CI. + let deadline = started + hold + Duration::from_millis(500); + let after = requested_selection(&candidate, &run, deadline) .err() .unwrap(); - assert!(started.elapsed() >= hold - Duration::from_millis(50)); - assert_eq!(after.code, PAST_ACQUISITION, "{}", after.message); + assert_waited_then_acquired(started, hold, &after); holder.join().unwrap(); let (release, holder) = hold_lease_as_project_stop(&candidate, Duration::from_secs(30)); - let refused = requested_selection( - &candidate, - &run, - Instant::now() + Duration::from_millis(300), - ) - .err() - .unwrap(); + let deadline = Instant::now() + Duration::from_millis(300); + let refused = requested_selection(&candidate, &run, deadline) + .err() + .unwrap(); + assert!(Instant::now() >= deadline); assert_eq!(refused.code, "provider_busy"); assert_eq!(refused.message, PROJECT_STOP_REFUSAL); assert!(refused.cause_code.is_none()); diff --git a/tests/models/tla/native-command-lease/COVERAGE.md b/tests/models/tla/native-command-lease/COVERAGE.md index d2c56bc09..761b44c65 100644 --- a/tests/models/tla/native-command-lease/COVERAGE.md +++ b/tests/models/tla/native-command-lease/COVERAGE.md @@ -129,13 +129,17 @@ Tests: - `provider/engine/command_lease_tests.rs`: the constants (35 s is the start's bound, the owner keeps 15 s, `command_lease_deadline` caps a far - deadline at each); scaled 50 times, a project stop held 600 ms is refused - at the owner bound with the exact project-stop message and waited out by - `connect_command_request`; a stop past the bound is refused with the raw, + deadline at each). The scaled 300 ms owner wait refuses a stop that still + holds the lease and returns the exact project-stop message. The test releases + the stop on the requester's first retry and verifies acquisition. A stop + past the requester's deadline yields the raw, uncaused `provider_busy`. -- `dependency_refresh.rs` `owner_lease_tests`: the same for the refresh's - `requested_selection`, and the owner's selection refusal mapped to - `graph_dependency_refresh_lease_busy` while other codes are kept. +- `dependency_refresh.rs` `owner_lease_tests`: the refresh's `requested_selection` + waits out a 600 ms stop with 500 ms of slack for scheduling, unlock, and + polling at up to 50 ms per interval (HACK-2741). A stop held past the + deadline yields the exact raw `provider_busy` with no cause. The owner's + selection refusal maps to `graph_dependency_refresh_lease_busy`, and other + codes are kept. - `tests/native-command-lease.test.ts`, `tests/native-project-exec.test.ts`, `tests/native-project-dependency-refresh.test.ts`: the progress line (once across the refresh and the exec), the refusal for `hack exec` and `hack @@ -212,13 +216,13 @@ roles: `requester` is `phase = "refresh"`, `owner` is `phase = "exec"`. Tests: -- `provider/engine/command_lease_tests.rs`, scaled 50 times: the requester's - wait outlasts a project stop held 600 ms and releases the lease; a stop past - its bound is refused with the raw, uncaused `provider_busy` naming the stop; - 100 uncontended waits take well under a second, and on a home with no pool - the wait returns at once and creates no lock file; and the call site, - `native::foreground::exec`, waits out the stop before it contacts the owner - (this test fails when the wait is removed). +- `provider/engine/command_lease_tests.rs`: the requester waits out a 600 ms + stop with 500 ms of polling slack (#1286, HACK-2751), then releases the + lease for the next holder. A stop still holding the lease at the deadline + gets the raw, uncaused `provider_busy` naming the stop. 100 uncontended waits + take well under a second, and on a home with no pool the wait returns at once + and creates no lock file. The call site, `native::foreground::exec`, waits out + the stop before it contacts the owner (this test fails when the wait is removed). - `native/runtime/exec_tests.rs`: only `provider_busy` maps to `graph_authored_exec_lease_busy`, which passes the reply-code filter, and the owner's wait on a real held flock ends in that code.