Skip to content

Commit a91e8b7

Browse files
branchseerclaude
andcommitted
Replace sleep polling in vtt barrier with notify filesystem watching
Use the `notify` crate to block on directory events instead of spinning with 10ms sleeps. The watcher receives OS-level notifications (inotify on Linux, FSEvents on macOS, ReadDirectoryChangesW on Windows) and rechecks the file count on each event. A double-check is done around the watcher setup to avoid missing events created between the marker write and watcher registration. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f5e0680 commit a91e8b7

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed

Cargo.lock

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ os_str_bytes = "7.1.1"
9393
ouroboros = "0.18.5"
9494
owo-colors = { version = "4.1.0", features = ["supports-colors"] }
9595
passfd = { git = "https://github.com/polachok/passfd", rev = "d55881752c16aced1a49a75f9c428d38d3767213", default-features = false }
96+
notify = "8.0.0"
9697
path-clean = "1.0.1"
9798
pathdiff = "0.2.3"
9899
petgraph = "0.8.2"

crates/vite_task_bin/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ path = "src/vtt.rs"
1717
[dependencies]
1818
anyhow = { workspace = true }
1919
libc = { workspace = true }
20+
notify = { workspace = true }
2021
async-trait = { workspace = true }
2122
clap = { workspace = true, features = ["derive"] }
2223
jsonc-parser = { workspace = true }

crates/vite_task_bin/src/vtt.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,29 @@ fn cmd_barrier(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
9090
let marker = dir.join(std::format!("{prefix}_{pid}"));
9191
std::fs::write(&marker, "")?;
9292

93-
// Poll until <count> matching files exist.
94-
loop {
95-
let matches = std::fs::read_dir(dir)?
93+
// Wait until <count> matching files exist using filesystem notifications.
94+
let prefix_match = std::format!("{prefix}_");
95+
let count_matches = |d: &std::path::Path| -> Result<bool, Box<dyn std::error::Error>> {
96+
Ok(std::fs::read_dir(d)?
9697
.filter_map(Result::ok)
97-
.filter(|e| e.file_name().to_string_lossy().starts_with(&std::format!("{prefix}_")))
98-
.count();
99-
if matches >= count {
100-
break;
98+
.filter(|e| e.file_name().to_string_lossy().starts_with(prefix_match.as_str()))
99+
.count()
100+
>= count)
101+
};
102+
if !count_matches(dir)? {
103+
use notify::Watcher as _;
104+
let (tx, rx) = std::sync::mpsc::channel();
105+
let mut watcher = notify::recommended_watcher(tx)?;
106+
watcher.watch(dir, notify::RecursiveMode::NonRecursive)?;
107+
// Re-check after setting up the watcher to avoid missing events created
108+
// between our marker write and the watcher registration.
109+
if !count_matches(dir)? {
110+
for _ in rx {
111+
if count_matches(dir)? {
112+
break;
113+
}
114+
}
101115
}
102-
std::thread::sleep(std::time::Duration::from_millis(10));
103116
}
104117

105118
if daemonize {

0 commit comments

Comments
 (0)