Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(list): Pre-start fsmonitor daemons on macOS to avoid race condition (#81)

Maximilian Roos42fc42b0ea6a
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index 2b3bd9811..462c53fad 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -646,6 +646,25 @@ pub fn collect(
return Ok(None);
}
+ // Pre-start fsmonitor daemons on macOS to avoid auto-start races.
+ //
+ // Git's builtin fsmonitor on macOS has race conditions under parallel load that can
+ // cause git commands to hang. When multiple git status commands try to auto-start
+ // the daemon simultaneously, they can wedge. Pre-starting the daemons before parallel
+ // operations avoids this race.
+ //
+ // See: https://gitlab.com/gitlab-org/git/-/merge_requests/148 (scalar's workaround)
+ // See: https://github.com/jj-vcs/jj/issues/6440 (jj hit same issue)
+ #[cfg(target_os = "macos")]
+ {
+ let main_repo = Repository::at(&main_worktree.path);
+ if main_repo.is_builtin_fsmonitor_enabled() {
+ for wt in &sorted_worktrees {
+ Repository::at(&wt.path).start_fsmonitor_daemon();
+ }
+ }
+ }
+
// Cache last rendered (unclamped) message per row to avoid redundant updates.
let mut last_rendered_lines: Vec<String> = vec![String::new(); all_items.len()];
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index 14eff0e53..99a81a9cd 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -580,6 +580,28 @@ impl Repository {
Ok(git_dir.join("MERGE_HEAD").exists())
}
+ /// Check if git's builtin fsmonitor daemon is enabled.
+ ///
+ /// Returns true only for `core.fsmonitor=true` (the builtin daemon).
+ /// Returns false for Watchman hooks, disabled, or unset.
+ pub fn is_builtin_fsmonitor_enabled(&self) -> bool {
+ self.run_command(&["config", "--get", "core.fsmonitor"])
+ .ok()
+ .map(|s| s.trim() == "true")
+ .unwrap_or(false)
+ }
+
+ /// Start the fsmonitor daemon for this worktree.
+ ///
+ /// This is idempotent - if the daemon is already running, this is a no-op.
+ /// Used to avoid auto-start races when running many parallel git commands.
+ pub fn start_fsmonitor_daemon(&self) {
+ // Best effort - log errors at debug level for troubleshooting
+ if let Err(e) = self.run_command(&["fsmonitor--daemon", "start"]) {
+ log::debug!("fsmonitor daemon start failed (usually fine): {e}");
+ }
+ }
+
/// Check if base is an ancestor of head (i.e., would be a fast-forward).
pub fn is_ancestor(&self, base: &str, head: &str) -> anyhow::Result<bool> {
self.run_command_check(&["merge-base", "--is-ancestor", base, head])
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index 8ce7e8d14..6a74efbab 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -158,9 +158,14 @@ fn generate_wrapper(repo: &TestRepo, shell: &str) -> String {
// Configure environment
repo.clean_cli_env(&mut cmd);
- let output = cmd
- .output()
- .unwrap_or_else(|_| panic!("Failed to run wt config shell init {}", shell));
+ let output = cmd.output().unwrap_or_else(|e| {
+ panic!(
+ "Failed to run wt config shell init {}: {} (binary: {})",
+ shell,
+ e,
+ wt_bin.display()
+ )
+ });
if !output.status.success() {
panic!(