Wait for git without polling
Browse filesrun_git polled try_wait every 2 ms so the watchdog's kill could always get in, which added about 1.1 ms to every git command (1.9 ms against 0.76 ms for a plain Command::output). It now blocks in waitid with WNOWAIT: that sees git exit without reaping it, so its pid can't be reused while the watchdog may still kill it, then reaps at once. Each command now costs about 0.85 ms; a cold folder page runs up to 50. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Author
- Um9i vulgarmessiah@gmail.com
- Date
- 22 Nov 2026, 22:11
- Commit
- 4f9e91e4c91c
- Parent
- 12b9bf3
2 files changed +11−8
+1−1
| @@ -22,7 +22,7 @@ futures-util = { version = "0.3.34", default-features = false, features = ["std" | ||
| getrandom = "0.4.3" | ||
| hmac = "0.13.0" | ||
| html-escape = "0.3.1" | ||
| nix = { version = "0.32.0", features = ["user", "fs", "dir", "resource", "hostname"] } | ||
| nix = { version = "0.32.0", features = ["user", "fs", "dir", "resource", "hostname", "process"] } | ||
| pulldown-cmark = { version = "0.13.6", default-features = false, features = ["html"] } | ||
| qrcode = { version = "0.15.0", default-features = false } | ||
| rpassword = "7.5.5" | ||
| @@ -17,6 +17,7 @@ use std::time::{Duration, Instant}; | ||
| use nix::dir::Dir; | ||
| use nix::fcntl::{AtFlags, OFlag, open, openat}; | ||
| use nix::sys::stat::{Mode, SFlag, fstatat}; | ||
| use nix::sys::wait::{Id, WaitPidFlag, waitid}; | ||
| use nix::unistd::{Gid, Uid, UnlinkatFlags, fchown, fchownat, unlinkat}; | ||
| /// Where bare repositories live: `<root>/<owner>/<name>.git`. `GITV_REPOS` overrides it for local runs. | ||
| @@ -237,6 +238,7 @@ fn run_git<S: AsRef<OsStr>>( | ||
| } | ||
| _ => None, | ||
| }; | ||
| let pid = nix::unistd::Pid::from_raw(i32::try_from(child.id()).map_err(|_| "git has no pid")?); | ||
| let child = Arc::new(Mutex::new(child)); | ||
| let kill = |child: &Mutex<Child>| { | ||
| // The child is never reaped while the lock is held, so this can't hit a reused pid. | ||
| @@ -264,14 +266,15 @@ fn run_git<S: AsRef<OsStr>>( | ||
| kill(&child); | ||
| } | ||
| drop(stdout); | ||
| // Poll rather than block in wait(), taking the lock only briefly, so a kill can always | ||
| // get in, here or from the watchdog, even if git closed its output but kept running. | ||
| // Wait for git to exit without reaping it (WNOWAIT), holding no lock, so the watchdog can | ||
| // still kill it if it closed its output but kept running. Until it is reaped its pid can't | ||
| // be reused, so that kill can never reach another process. Then reap it at once. | ||
| let status = loop { | ||
| let polled = child.lock().map_err(|_| "git watchdog failed")?.try_wait(); | ||
| match polled { | ||
| Ok(Some(status)) => break Ok(status), | ||
| Ok(None) if Instant::now() >= deadline => kill(&child), | ||
| Ok(None) => thread::sleep(Duration::from_millis(2)), | ||
| match waitid(Id::Pid(pid), WaitPidFlag::WEXITED | WaitPidFlag::WNOWAIT) { | ||
| Ok(_) => { | ||
| break child.lock().map_err(|_| "git watchdog failed")?.wait().map_err(|e| format!("git failed: {e}")); | ||
| } | ||
| Err(nix::errno::Errno::EINTR) => continue, | ||
| Err(e) => break Err(format!("git failed: {e}")), | ||
| } | ||
| }; | ||