From d25db5b7f82d357faba96d24316ead34a03359f5 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 23 Oct 2026 13:45:12 +0200 Subject: [PATCH 1/5] fix(watch): one change makes one deadline for every service it concerns (#233) A candidate's pipeline and a debounce looked for the end of their quiet period every 200 ms, counted from when their thread began. The OS reports one change more than once (notify makes an event per FSEvents flag; FSEvents may send a second record; inotify reports a save's truncation and write apart). Each report re-arms every service it concerns at about the same moment, but a thread that began just before the repeat missed its deadline at one look and fired at the next, 200 ms later than another service's. Two services after the same job then built for one edit 240 ms apart, and the job ran again for the second (macOS CI). The waiters now sleep until the deadline itself, wherever a later report moved it: a repeat moves it by the time between the reports, the same for every service. Co-Authored-By: Claude Opus 5.5 --- .../src/supervisor/candidate.rs | 98 ++++++++++++++++++- crates/devstack-daemon/src/supervisor/mod.rs | 48 +++++++-- 2 files changed, 134 insertions(+), 12 deletions(-) diff --git a/crates/devstack-daemon/src/supervisor/candidate.rs b/crates/devstack-daemon/src/supervisor/candidate.rs index 50fe9dec..d3e75384 100644 --- a/crates/devstack-daemon/src/supervisor/candidate.rs +++ b/crates/devstack-daemon/src/supervisor/candidate.rs @@ -6,7 +6,7 @@ use devstack_config::model::Service; use std::collections::{BTreeMap, BTreeSet}; use std::path::PathBuf; use std::sync::Arc; -use std::time::{Duration, Instant}; +use std::time::Instant; impl Supervisor { /// The watcher saw `path` change, and `name` watches it. Debounced: a @@ -60,10 +60,11 @@ impl Supervisor { fn pipeline(self: &Arc, name: &str) { loop { let (paths, epoch) = loop { - { + let left = { let mut changes = self.changes.lock().unwrap(); let c = changes.get_mut(name).expect("changed() created it"); - if c.last.is_some_and(|t| t.elapsed() >= DEBOUNCE) { + let left = super::quiet_left(c.last, DEBOUNCE); + if left.is_zero() { if c.absorbed >= c.epoch { // A manual restart already took these changes in. c.paths.clear(); @@ -75,8 +76,9 @@ impl Supervisor { } break (std::mem::take(&mut c.paths), c.epoch); } - } - std::thread::sleep(Duration::from_millis(200)); + left + }; + std::thread::sleep(left); }; let superseded = self.candidate_from(name, &paths, epoch); let mut changes = self.changes.lock().unwrap(); @@ -460,3 +462,89 @@ fn hold_reason(svc: &Service, paths: &BTreeSet) -> Option { .collect(); (!hit.is_empty()).then(|| format!("changes schema inputs ({})", hit.join(", "))) } + +#[cfg(test)] +mod tests { + use super::*; + use std::time::Duration; + + /// One edit, reported twice about 200 ms apart (FSEvents, #233), makes + /// one deadline for every service it concerns: two services after the + /// same job take their candidates at the same moment, the second report's + /// time plus the quiet period, not one of them a poll later. The first + /// report reaches the second service 20 ms after the first service (a + /// busy daemon writing its log): the thread of the first then began well + /// before the repeat, which made it miss its deadline by one poll. + #[test] + fn a_change_reported_twice_makes_one_deadline_for_every_service() { + let d = tempfile::tempdir().unwrap(); + let file = d.path().join("devstack.toml"); + let built = "build = \"git --version\"\noutput = \"src/Site\"\nwatch = [\"src/Site/**\"]\nrun = \"x\"\nafter = [\"seed\"]\n"; + let text = format!( + "version = 1\n[stack]\nname = \"s\"\n[services.seed]\nkind = \"job\"\nrun = \"x\"\n[services.a]\n{built}[services.b]\n{built}" + ); + let stack = devstack_config::from_str(&text, &file).unwrap(); + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let sup = Arc::new( + Supervisor::new( + stack, + file, + d.path().to_path_buf(), + d.path().join(".devstack"), + devstack_proc::Options::default(), + rt.handle().clone(), + ) + .unwrap(), + ); + let path = "src/Site/index.html"; + let taken = |name: &str| { + sup.changes + .lock() + .unwrap() + .get(name) + .is_some_and(|c| c.paths.is_empty()) + }; + + sup.changed("a", path); + std::thread::sleep(Duration::from_millis(20)); + sup.changed("b", path); + std::thread::sleep(Duration::from_millis(200)); + let repeat = Instant::now(); + sup.changed("a", path); + sup.changed("b", path); + let deadline = repeat + DEBOUNCE; + + let mut fired: BTreeMap<&str, Instant> = BTreeMap::new(); + while fired.len() < 2 && repeat.elapsed() < DEBOUNCE * 3 { + for name in ["a", "b"] { + if !fired.contains_key(name) && taken(name) { + fired.insert(name, Instant::now()); + } + } + std::thread::sleep(Duration::from_millis(1)); + } + assert_eq!(fired.len(), 2, "both took their changes: {fired:?}"); + for (name, at) in &fired { + assert!(*at >= deadline, "{name} fired before the quiet period"); + let late = *at - deadline; + assert!( + late < Duration::from_millis(100), + "{name} fired {late:?} after its deadline" + ); + } + let (a, b) = (fired["a"], fired["b"]); + let apart = a.max(b) - a.min(b); + assert!(apart < Duration::from_millis(100), "{apart:?} apart"); + // Each pipeline took the change once, and is done with it. + std::thread::sleep(Duration::from_millis(300)); + for name in ["a", "b"] { + let changes = sup.changes.lock().unwrap(); + let c = &changes[name]; + assert_eq!(c.epoch, 2, "{name}"); + assert!(!c.worker && c.last.is_none(), "{name}"); + } + } +} diff --git a/crates/devstack-daemon/src/supervisor/mod.rs b/crates/devstack-daemon/src/supervisor/mod.rs index c3b997a5..3248be4f 100644 --- a/crates/devstack-daemon/src/supervisor/mod.rs +++ b/crates/devstack-daemon/src/supervisor/mod.rs @@ -903,13 +903,17 @@ impl Supervisor { .name(format!("debounce-{key}")) .spawn(move || { loop { - std::thread::sleep((quiet / 4).min(Duration::from_millis(200))); - let mut q = me.quiet.lock().unwrap(); - let e = q.get_mut(&key).expect("inserted above"); - if e.0.elapsed() >= quiet { - e.1 = false; - break; - } + let left = { + let mut q = me.quiet.lock().unwrap(); + let e = q.get_mut(&key).expect("inserted above"); + let left = quiet_left(Some(e.0), quiet); + if left.is_zero() { + e.1 = false; + break; + } + left + }; + std::thread::sleep(left); } action(&me); }); @@ -996,6 +1000,36 @@ const DEBOUNCE: Duration = Duration::from_secs(2); /// a dev server's hot reload waits for it. const COPY_QUIET: Duration = Duration::from_millis(150); +/// How much of a quiet period of `quiet` since the last change, at `last`, is +/// still to come; zero once it is over. A waiter (a candidate's pipeline, a +/// debounce) sleeps exactly this long and asks again: it wakes at the +/// deadline itself, wherever a later change moved it. +/// +/// It used to look every 200 ms instead, from when its thread began, and that +/// made one change two deadlines (#233). The OS reports a change more than +/// once: notify turns one FSEvents record into an event per flag (created, +/// modified, metadata), FSEvents may send a second record a moment later, +/// inotify reports the truncation and the write of one save apart. Each +/// report calls `changed()` for every service it concerns, one after the +/// other, so each of them is re-armed by about the same moment. But a +/// service whose thread began a millisecond before the repeat missed its +/// deadline at its tenth look and fired at its eleventh, 200 ms after one +/// whose thread began a few microseconds before it: two services after the +/// same job built for one edit 240 ms apart, the job's run for the first had +/// begun by then, and it ran again for the second (macOS CI). +/// +/// Not chosen: dropping a report whose file looks as it did at the last +/// report (mtime, length) asks the file system about every event, which the +/// watcher avoids (`watch.rs`, `LIST_NEW_DIRS`); FAT and exFAT keep the mtime +/// to 2 s, and a lane's copy, quiet for only 150 ms, would miss a second save +/// of the same length. Not moving a deadline for a path already waiting +/// would build while an editor is still saving, on every OS. A repeat moves +/// the deadline by the time between the reports, the same for every service; +/// the debounce is there to absorb it. +fn quiet_left(last: Option, quiet: Duration) -> Duration { + last.map_or(quiet, |t| quiet.saturating_sub(t.elapsed())) +} + /// A free local port for an instance behind the proxy. fn free_port() -> Result { std::net::TcpListener::bind(("127.0.0.1", 0)) From 2821f7192c1da1f14b7735f0706ec5c7922c171d Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 23 Oct 2026 13:45:13 +0200 Subject: [PATCH 2/5] ci: TEMPORARY, a_job_runs_once_for_two_candidates 10 times on macOS (#233) Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4fbafdd0..77727fed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -234,6 +234,40 @@ jobs: DEVSTACK_INTEGRATION: '1' DEVSTACK_INTEGRATION_COMPOSE: ${{ matrix.os == 'ubuntu-latest' && '1' || '' }} + # TEMPORARY (#233): the job-runs-once test ten times in a row on macOS. + repeat-233: + needs: changes + if: needs.changes.outputs.rust == 'true' + runs-on: macos-14 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # master + with: + toolchain: stable + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - name: no Spotlight + run: sudo mdutil -a -i off + - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + with: + dotnet-version: 10.0.x + - name: build the test + run: cargo test --locked -p devstack --test daemon_lifecycle --no-run + - name: a_job_runs_once_for_two_candidates, 10 runs + shell: bash + run: | + failed=0 + for i in $(seq 1 10); do + if cargo test --locked -p devstack --test daemon_lifecycle a_job_runs_once_for_two_candidates -- --include-ignored --nocapture > "run-$i.log" 2>&1 && grep -q "test result: ok. 1 passed" "run-$i.log"; then + echo "run $i: ok" + else + echo "run $i: FAILED"; failed=$((failed + 1)); cat "run-$i.log" + fi + done + echo "$failed of 10 failed" + test "$failed" -eq 0 + env: + DEVSTACK_INTEGRATION: '1' + remote-lane: needs: changes if: needs.changes.outputs.rust == 'true' From 0c33b68b9a53a161e85773396808ca1a640b4db3 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 23 Oct 2026 13:54:05 +0200 Subject: [PATCH 3/5] fix(candidate): a change reported after a restart began waits for it (#233) `devstack restart` right after an edit builds the tree with the edit, but FSEvents reported it a few milliseconds after the restart had taken in the changes so far. Its candidate came out of the quiet period while the restart was still switching, was no build of main yet, and took over again, running the jobs the service comes after once more (macOS CI, `lifecycle_services_only`). Once the quiet period is over, a candidate now waits for a restart of its service under way; built after it, it is the build main has and is dropped as unchanged. Co-Authored-By: Claude Opus 5.5 --- .../src/supervisor/candidate.rs | 122 ++++++++++++------ 1 file changed, 85 insertions(+), 37 deletions(-) diff --git a/crates/devstack-daemon/src/supervisor/candidate.rs b/crates/devstack-daemon/src/supervisor/candidate.rs index d3e75384..3eb1ca5b 100644 --- a/crates/devstack-daemon/src/supervisor/candidate.rs +++ b/crates/devstack-daemon/src/supervisor/candidate.rs @@ -6,7 +6,11 @@ use devstack_config::model::Service; use std::collections::{BTreeMap, BTreeSet}; use std::path::PathBuf; use std::sync::Arc; -use std::time::Instant; +use std::time::{Duration, Instant}; + +/// How often a candidate whose quiet period is over looks whether a restart +/// of its service is still under way. +const RESTART_POLL: Duration = Duration::from_millis(100); impl Supervisor { /// The watcher saw `path` change, and `name` watches it. Debounced: a @@ -74,9 +78,20 @@ impl Supervisor { self.clear_changed(name); return; } - break (std::mem::take(&mut c.paths), c.epoch); + // A restart under way may cover the change: one asked + // for right after an edit builds the tree with it, but + // the report can come after the restart began (FSEvents + // takes a few milliseconds, #233). Built now, the + // candidate is no build of main yet and takes over + // again, its jobs with it; built once the restart is + // done, it is the build main has and is dropped. + if !self.restart_under_way(name) { + break (std::mem::take(&mut c.paths), c.epoch); + } + RESTART_POLL + } else { + left } - left }; std::thread::sleep(left); }; @@ -466,17 +481,12 @@ fn hold_reason(svc: &Service, paths: &BTreeSet) -> Option { #[cfg(test)] mod tests { use super::*; - use std::time::Duration; - /// One edit, reported twice about 200 ms apart (FSEvents, #233), makes - /// one deadline for every service it concerns: two services after the - /// same job take their candidates at the same moment, the second report's - /// time plus the quiet period, not one of them a poll later. The first - /// report reaches the second service 20 ms after the first service (a - /// busy daemon writing its log): the thread of the first then began well - /// before the repeat, which made it miss its deadline by one poll. - #[test] - fn a_change_reported_twice_makes_one_deadline_for_every_service() { + const PATH: &str = "src/Site/index.html"; + + /// A supervisor of `seed`, a job, and `a` and `b`, built services after + /// it that watch [`PATH`]; none of them running. + fn two_after_a_job() -> (tempfile::TempDir, tokio::runtime::Runtime, Arc) { let d = tempfile::tempdir().unwrap(); let file = d.path().join("devstack.toml"); let built = "build = \"git --version\"\noutput = \"src/Site\"\nwatch = [\"src/Site/**\"]\nrun = \"x\"\nafter = [\"seed\"]\n"; @@ -488,39 +498,51 @@ mod tests { .enable_all() .build() .unwrap(); - let sup = Arc::new( - Supervisor::new( - stack, - file, - d.path().to_path_buf(), - d.path().join(".devstack"), - devstack_proc::Options::default(), - rt.handle().clone(), - ) - .unwrap(), - ); - let path = "src/Site/index.html"; - let taken = |name: &str| { - sup.changes - .lock() - .unwrap() - .get(name) - .is_some_and(|c| c.paths.is_empty()) - }; + let sup = Supervisor::new( + stack, + file, + d.path().to_path_buf(), + d.path().join(".devstack"), + devstack_proc::Options::default(), + rt.handle().clone(), + ) + .unwrap(); + (d, rt, sup) + } + + /// Has the pipeline of `name` taken the changes it waited for? + fn taken(sup: &Supervisor, name: &str) -> bool { + sup.changes + .lock() + .unwrap() + .get(name) + .is_some_and(|c| c.paths.is_empty()) + } - sup.changed("a", path); + /// One edit, reported twice about 200 ms apart (FSEvents, #233), makes + /// one deadline for every service it concerns: two services after the + /// same job take their candidates at the same moment, the second report's + /// time plus the quiet period, not one of them a poll later. The first + /// report reaches the second service 20 ms after the first service (a + /// busy daemon writing its log): the thread of the first then began well + /// before the repeat, which made it miss its deadline by one poll. + #[test] + fn a_change_reported_twice_makes_one_deadline_for_every_service() { + let (_d, _rt, sup) = two_after_a_job(); + + sup.changed("a", PATH); std::thread::sleep(Duration::from_millis(20)); - sup.changed("b", path); + sup.changed("b", PATH); std::thread::sleep(Duration::from_millis(200)); let repeat = Instant::now(); - sup.changed("a", path); - sup.changed("b", path); + sup.changed("a", PATH); + sup.changed("b", PATH); let deadline = repeat + DEBOUNCE; let mut fired: BTreeMap<&str, Instant> = BTreeMap::new(); while fired.len() < 2 && repeat.elapsed() < DEBOUNCE * 3 { for name in ["a", "b"] { - if !fired.contains_key(name) && taken(name) { + if !fired.contains_key(name) && taken(&sup, name) { fired.insert(name, Instant::now()); } } @@ -547,4 +569,30 @@ mod tests { assert!(!c.worker && c.last.is_none(), "{name}"); } } + + /// A change reported after a restart began waits for the restart, past + /// its quiet period: the restart may have built it already (#233). + #[test] + fn a_candidate_waits_for_a_restart_under_way() { + let (_d, _rt, sup) = two_after_a_job(); + *sup.restarts_under_way + .lock() + .unwrap() + .entry("a".into()) + .or_default() += 1; + sup.changed("a", PATH); + std::thread::sleep(DEBOUNCE + Duration::from_millis(500)); + assert!(!taken(&sup, "a"), "it waits for the restart"); + *sup.restarts_under_way.lock().unwrap().get_mut("a").unwrap() -= 1; + let done = Instant::now(); + while !taken(&sup, "a") && done.elapsed() < Duration::from_secs(5) { + std::thread::sleep(Duration::from_millis(10)); + } + assert!(taken(&sup, "a"), "taken once the restart is done"); + assert!( + done.elapsed() < Duration::from_secs(1), + "{:?}", + done.elapsed() + ); + } } From 13852d336afdf8f037e012e4f3d50468fd466923 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 23 Oct 2026 13:54:05 +0200 Subject: [PATCH 4/5] ci: TEMPORARY, lifecycle_services_only 5 times on macOS (#233) Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77727fed..719a38bc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -267,6 +267,26 @@ jobs: test "$failed" -eq 0 env: DEVSTACK_INTEGRATION: '1' + - name: prime the .NET CLI + shell: bash + run: | + cp -r examples/fixture-stack/src/Api "$RUNNER_TEMP/prime-api" + dotnet build "$RUNNER_TEMP/prime-api/Api.csproj" -nologo -v q + - name: lifecycle_services_only, 5 runs + shell: bash + run: | + failed=0 + for i in $(seq 1 5); do + if cargo test --locked -p devstack --test daemon_lifecycle lifecycle_services_only -- --include-ignored --nocapture > "life-$i.log" 2>&1 && grep -q "test result: ok. 1 passed" "life-$i.log"; then + echo "run $i: ok" + else + echo "run $i: FAILED"; failed=$((failed + 1)); cat "life-$i.log" + fi + done + echo "$failed of 5 failed" + test "$failed" -eq 0 + env: + DEVSTACK_INTEGRATION: '1' remote-lane: needs: changes From 3885ec70890b1e54bd74370f7edc20c24f41a074 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 23 Oct 2026 14:02:45 +0200 Subject: [PATCH 5/5] ci: remove the temporary macOS repeat job (#233) Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 54 ---------------------------------------- 1 file changed, 54 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 719a38bc..4fbafdd0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -234,60 +234,6 @@ jobs: DEVSTACK_INTEGRATION: '1' DEVSTACK_INTEGRATION_COMPOSE: ${{ matrix.os == 'ubuntu-latest' && '1' || '' }} - # TEMPORARY (#233): the job-runs-once test ten times in a row on macOS. - repeat-233: - needs: changes - if: needs.changes.outputs.rust == 'true' - runs-on: macos-14 - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # master - with: - toolchain: stable - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 - - name: no Spotlight - run: sudo mdutil -a -i off - - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 - with: - dotnet-version: 10.0.x - - name: build the test - run: cargo test --locked -p devstack --test daemon_lifecycle --no-run - - name: a_job_runs_once_for_two_candidates, 10 runs - shell: bash - run: | - failed=0 - for i in $(seq 1 10); do - if cargo test --locked -p devstack --test daemon_lifecycle a_job_runs_once_for_two_candidates -- --include-ignored --nocapture > "run-$i.log" 2>&1 && grep -q "test result: ok. 1 passed" "run-$i.log"; then - echo "run $i: ok" - else - echo "run $i: FAILED"; failed=$((failed + 1)); cat "run-$i.log" - fi - done - echo "$failed of 10 failed" - test "$failed" -eq 0 - env: - DEVSTACK_INTEGRATION: '1' - - name: prime the .NET CLI - shell: bash - run: | - cp -r examples/fixture-stack/src/Api "$RUNNER_TEMP/prime-api" - dotnet build "$RUNNER_TEMP/prime-api/Api.csproj" -nologo -v q - - name: lifecycle_services_only, 5 runs - shell: bash - run: | - failed=0 - for i in $(seq 1 5); do - if cargo test --locked -p devstack --test daemon_lifecycle lifecycle_services_only -- --include-ignored --nocapture > "life-$i.log" 2>&1 && grep -q "test result: ok. 1 passed" "life-$i.log"; then - echo "run $i: ok" - else - echo "run $i: FAILED"; failed=$((failed + 1)); cat "life-$i.log" - fi - done - echo "$failed of 5 failed" - test "$failed" -eq 0 - env: - DEVSTACK_INTEGRATION: '1' - remote-lane: needs: changes if: needs.changes.outputs.rust == 'true'