From 5cd314cb00a7a532704f75ad5cd5ea56fd0aad13 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 20 Nov 2026 17:16:57 +0100 Subject: [PATCH] fix(daemon): a file written right after its new folder is seen on Linux notify's inotify backend watches a new directory only after the handler of its event batch returned (notify 8.2, `inotify.rs`: handlers first, `add_watch` after). The watcher listed the new directory inside that handler, before the watch: a file written in between was in neither the listing nor any event, and its change made no candidate until the next edit. This is how `edits_become_candidates` failed now and then on Linux. A new directory is listed once more 200 ms after its event was handled, on one thread per watcher (`feed`), and what it finds is delivered as any change. The handling of events moved into `feed`, which the new test drives with real inotify. Closes #587 Bug-Class: race-on-start Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 6 + crates/devstack-daemon/src/watch.rs | 205 +++++++++++++++++++++------- 2 files changed, 159 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43d77709..f2198b62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,12 @@ and listed with their assets on the overview's "healthy" count, its filter and a job's shortcut card put a running job (a migration) there; it counts as starting now until it completed, as the daemon and the hub's summary count it (#591). +- **On Linux, a file written right after its new folder makes its + candidate.** inotify watches a new folder only after the watcher handled + its event, and the watcher listed the folder before that: a file written + in between was in neither, and its change went unseen until the next + edit. The new folder is listed once more after its watch is in place + (#587). ## [0.51.0] - 2026-11-19 diff --git a/crates/devstack-daemon/src/watch.rs b/crates/devstack-daemon/src/watch.rs index c0366ab2..6a3ec78e 100644 --- a/crates/devstack-daemon/src/watch.rs +++ b/crates/devstack-daemon/src/watch.rs @@ -234,6 +234,53 @@ fn rels(repo: &Path, ev: notify::Event) -> Vec { event_paths(repo, ev, &Disk, LIST_NEW_DIRS) } +/// What the watcher does with the paths of a change that count. +type Deliver = Arc) + Send + Sync>; + +/// How long after its event was handled a new directory is listed once more +/// ([`LIST_NEW_DIRS`]). inotify watches the directory only after the +/// handler of the batch returned (notify 8, `inotify.rs`): a file written in +/// it between the first listing and that watch is in neither (#587). +const RELIST_AFTER: std::time::Duration = std::time::Duration::from_millis(200); + +/// The OS events of `repo`, as their paths that count, to `deliver`. A new +/// directory is listed again [`RELIST_AFTER`] later, on a thread of its own +/// that ends with the watcher; what it finds again is delivered again, +/// which costs nothing (a candidate or a restart waits for its debounce). +fn feed(repo: PathBuf, deliver: Deliver) -> impl FnMut(notify::Event) + Send + 'static { + let (tx, rx) = std::sync::mpsc::channel::<(Instant, Vec)>(); + if LIST_NEW_DIRS { + let (repo, deliver) = (repo.clone(), deliver.clone()); + let _ = std::thread::Builder::new() + .name("watch-relist".into()) + .spawn(move || { + while let Ok((due, dirs)) = rx.recv() { + std::thread::sleep(due.saturating_duration_since(Instant::now())); + let ev = dirs.into_iter().fold( + notify::Event::new(EventKind::Create(notify::event::CreateKind::Folder)), + notify::Event::add_path, + ); + deliver(rels(&repo, ev)); + } + }); + } + move |ev| { + let new_dirs: Vec = if LIST_NEW_DIRS && matches!(ev.kind, EventKind::Create(_)) { + ev.paths + .iter() + .filter(|p| counted(&repo, p).is_some() && Disk.is_dir(p)) + .cloned() + .collect() + } else { + Vec::new() + }; + deliver(rels(&repo, ev)); + if !new_dirs.is_empty() { + let _ = tx.send((Instant::now() + RELIST_AFTER, new_dirs)); + } + } +} + /// The paths an event is about that count, repository-relative. With /// `list_new_dirs`, a directory that was just created is reported with the /// files it already holds; generated directories are neither listed nor @@ -369,6 +416,64 @@ fn watch(sup: Arc) -> Result { } } }; + let deliver: Deliver = { + let (me, rules) = (me.clone(), rules.clone()); + Arc::new(move |rels: Vec| { + let ignores = rules.read().unwrap().clone(); + for rel in rels { + // An ignored file is not in a snapshot: no input of a build, no + // part of a lane's copy (module doc). + let in_snapshot = !ignores.ignored(&rel); + if lane_copy && in_snapshot { + me.copy_changed(); + } + if stack_rel.as_deref() == Some(rel.as_str()) { + me.stack_file_touched(); + continue; + } + if in_snapshot { + // A build file (`Directory.Build.targets`, a lockfile) may be + // an input of any build; the build id decides (#13). + let build_file = devstack_build::is_build_file(&rel); + for (n, g) in &built { + if g.is_match(&rel) || build_file { + me.changed(n, &rel); + } + } + } + for (n, g) in &devservers { + if covers(g, &rel) { + let key = format!("devserver:{n}"); + pending.add(&key, &rel); + let (n, p) = (n.clone(), pending.clone()); + me.debounce(key.clone(), move |s| restart_devserver(s, key, n, p)); + } + } + for (group, service, g) in &containers { + if covers(g, &rel) { + let key = format!("compose:{group}.{service}"); + pending.add(&key, &rel); + let (group, service, p) = (group.clone(), service.clone(), pending.clone()); + me.debounce(key.clone(), move |s| { + let Some(seen) = p.take(&key) else { + return; + }; + seen.log( + "restart_on", + serde_json::json!({ + "lane": s.lane, + "group": group, + "service": service, + }), + ); + s.restart_container(&group, &service); + }); + } + } + } + }) + }; + let mut feed = feed(repo.clone(), deliver); let mut inner = notify::recommended_watcher(move |res: notify::Result| { let mut ev = match res { Ok(ev) if ev.need_rescan() => return rescan("the OS asked for a rescan".into()), @@ -390,58 +495,7 @@ fn watch(sup: Arc) -> Result { *rules.write().unwrap() = fresh; }); } - let ignores = rules.read().unwrap().clone(); - for rel in rels(&repo, ev) { - // An ignored file is not in a snapshot: no input of a build, no - // part of a lane's copy (module doc). - let in_snapshot = !ignores.ignored(&rel); - if lane_copy && in_snapshot { - me.copy_changed(); - } - if stack_rel.as_deref() == Some(rel.as_str()) { - me.stack_file_touched(); - continue; - } - if in_snapshot { - // A build file (`Directory.Build.targets`, a lockfile) may be - // an input of any build; the build id decides (#13). - let build_file = devstack_build::is_build_file(&rel); - for (n, g) in &built { - if g.is_match(&rel) || build_file { - me.changed(n, &rel); - } - } - } - for (n, g) in &devservers { - if covers(g, &rel) { - let key = format!("devserver:{n}"); - pending.add(&key, &rel); - let (n, p) = (n.clone(), pending.clone()); - me.debounce(key.clone(), move |s| restart_devserver(s, key, n, p)); - } - } - for (group, service, g) in &containers { - if covers(g, &rel) { - let key = format!("compose:{group}.{service}"); - pending.add(&key, &rel); - let (group, service, p) = (group.clone(), service.clone(), pending.clone()); - me.debounce(key.clone(), move |s| { - let Some(seen) = p.take(&key) else { - return; - }; - seen.log( - "restart_on", - serde_json::json!({ - "lane": s.lane, - "group": group, - "service": service, - }), - ); - s.restart_container(&group, &service); - }); - } - } - } + feed(ev); }) .map_err(|e| e.to_string())?; let mode = if only_stack_file { @@ -1202,4 +1256,51 @@ mod tests { .or_else(|_| std::fs::remove_file(&up)) .unwrap(); } + + /// #587: inotify watches a new directory only after the handler of its + /// event returned; a file written in it between the handler's listing and + /// that watch was neither listed nor reported. The handler here takes a + /// while (as the daemon's, which builds and restarts), and the file comes + /// after the listing, before the watch. + #[cfg(target_os = "linux")] + #[test] + fn a_file_written_right_after_its_new_folder_is_reported() { + use notify::Watcher as _; + let dir = tempfile::tempdir().unwrap(); + let repo = std::fs::canonicalize(dir.path()).unwrap(); + let seen = Arc::new(Mutex::new(Vec::::new())); + let deliver: Deliver = { + let seen = seen.clone(); + Arc::new(move |rels| { + seen.lock().unwrap().extend(rels); + std::thread::sleep(std::time::Duration::from_millis(30)); + }) + }; + let mut feed = feed(repo.clone(), deliver); + let mut w = notify::recommended_watcher(move |res: notify::Result| { + if let Ok(ev) = res { + feed(ev); + } + }) + .unwrap(); + w.watch(&repo, RecursiveMode::Recursive).unwrap(); + for i in 0..5 { + let d = repo.join(format!("src{i}")); + std::fs::create_dir(&d).unwrap(); + std::thread::sleep(std::time::Duration::from_millis(10)); + std::fs::write(d.join("v2.txt"), "x").unwrap(); + std::thread::sleep(std::time::Duration::from_millis(100)); + } + let want: Vec = (0..5).map(|i| format!("src{i}/v2.txt")).collect(); + let deadline = Instant::now() + std::time::Duration::from_secs(5); + let missing = loop { + let got = seen.lock().unwrap().clone(); + let missing: Vec<&String> = want.iter().filter(|w| !got.contains(w)).collect(); + if missing.is_empty() || Instant::now() > deadline { + break missing.into_iter().cloned().collect::>(); + } + std::thread::sleep(std::time::Duration::from_millis(50)); + }; + assert!(missing.is_empty(), "never reported: {missing:?}"); + } }