Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Fix/copy ignored too many open files (#1864)
Fraser Speirs840e97ecee06
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rsindex 234a93ca3..6a86fdbab 100644--- a/src/commands/step_commands.rs+++ b/src/commands/step_commands.rs@@ -24,7 +24,7 @@ use ignore::gitignore::GitignoreBuilder;use rayon::prelude::*;use worktrunk::HookType;use worktrunk::config::{CopyIgnoredConfig, UserConfig};-use worktrunk::copy::{copy_dir_recursive, create_symlink, remove_if_exists};+use worktrunk::copy::{MAX_COPY_THREADS, copy_dir_recursive, create_symlink, remove_if_exists};use worktrunk::git::Repository;use worktrunk::path::format_path_for_display;use worktrunk::shell_exec::Cmd;@@ -801,68 +801,80 @@ pub fn step_copy_ignored(}}- // Copy entries in parallel — each entry has a distinct destination path- entries_to_copy- .par_iter()- .try_for_each(|(src_entry, is_dir)| -> anyhow::Result<()> {- // Paths from git ls-files are always under source_path- let relative = src_entry- .strip_prefix(&source_path)- .expect("git ls-files path under worktree");- let dest_entry = dest_path.join(relative);-- if *is_dir {- copy_dir_recursive(src_entry, &dest_entry, force).with_context(|| {- format!("copying directory {}", format_path_for_display(relative))- })?;- copied_count.fetch_add(1, Ordering::Relaxed);- } else {- if let Some(parent) = dest_entry.parent() {- fs::create_dir_all(parent).with_context(|| {- format!(- "creating directory for {}",- format_path_for_display(relative)- )+ // Copy entries in parallel — each entry has a distinct destination path.+ // A single dedicated thread pool is created once and shared across all copy+ // operations (outer loop + inner directory recursion). This prevents+ // concurrent callers from each creating their own pool simultaneously,+ // which would exhaust the OS file-descriptor limit on large trees+ // (EMFILE / "too many open files"). copy_dir_recursive() detects that it is+ // already running inside a rayon worker and reuses this pool automatically.+ let pool = rayon::ThreadPoolBuilder::new()+ .num_threads(MAX_COPY_THREADS)+ .build()+ .context("building copy thread pool")?;+ pool.install(|| {+ entries_to_copy+ .par_iter()+ .try_for_each(|(src_entry, is_dir)| -> anyhow::Result<()> {+ // Paths from git ls-files are always under source_path+ let relative = src_entry+ .strip_prefix(&source_path)+ .expect("git ls-files path under worktree");+ let dest_entry = dest_path.join(relative);++ if *is_dir {+ copy_dir_recursive(src_entry, &dest_entry, force).with_context(|| {+ format!("copying directory {}", format_path_for_display(relative))})?;- }- if force {- remove_if_exists(&dest_entry)?;- }- // Check if source is a symlink — preserve it instead of following it- let display_path = format_path_for_display(relative);- let source_is_symlink = fs::symlink_metadata(src_entry)- .context(format!("reading metadata for {display_path}"))?- .file_type()- .is_symlink();- if source_is_symlink {- // Skip existing symlinks for idempotent hook usage- if dest_entry.symlink_metadata().is_err() {- let target = fs::read_link(src_entry)- .context(format!("reading symlink {display_path}"))?;- create_symlink(&target, src_entry, &dest_entry)?;- copied_count.fetch_add(1, Ordering::Relaxed);- }+ copied_count.fetch_add(1, Ordering::Relaxed);} else {- // Skip existing entries (files or symlinks) for idempotent hook usage.- // Check symlink_metadata (not exists()) because exists() follows symlinks- // and returns false for broken ones, which would cause reflink_or_copy to- // fail with ENOENT on some platforms when copying through the broken symlink.- if dest_entry.symlink_metadata().is_err() {- match reflink_copy::reflink_or_copy(src_entry, &dest_entry) {- Ok(_) => {- copied_count.fetch_add(1, Ordering::Relaxed);- }- Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}- Err(e) => {- return Err(anyhow::Error::from(e)- .context(format!("copying {display_path}")));+ if let Some(parent) = dest_entry.parent() {+ fs::create_dir_all(parent).with_context(|| {+ format!(+ "creating directory for {}",+ format_path_for_display(relative)+ )+ })?;+ }+ if force {+ remove_if_exists(&dest_entry)?;+ }+ // Check if source is a symlink — preserve it instead of following it+ let display_path = format_path_for_display(relative);+ let source_is_symlink = fs::symlink_metadata(src_entry)+ .context(format!("reading metadata for {display_path}"))?+ .file_type()+ .is_symlink();+ if source_is_symlink {+ // Skip existing symlinks for idempotent hook usage+ if dest_entry.symlink_metadata().is_err() {+ let target = fs::read_link(src_entry)+ .context(format!("reading symlink {display_path}"))?;+ create_symlink(&target, src_entry, &dest_entry)?;+ copied_count.fetch_add(1, Ordering::Relaxed);+ }+ } else {+ // Skip existing entries (files or symlinks) for idempotent hook usage.+ // Check symlink_metadata (not exists()) because exists() follows symlinks+ // and returns false for broken ones, which would cause reflink_or_copy to+ // fail with ENOENT on some platforms when copying through the broken symlink.+ if dest_entry.symlink_metadata().is_err() {+ match reflink_copy::reflink_or_copy(src_entry, &dest_entry) {+ Ok(_) => {+ copied_count.fetch_add(1, Ordering::Relaxed);+ }+ Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}+ Err(e) => {+ return Err(anyhow::Error::from(e)+ .context(format!("copying {display_path}")));+ }}}}}- }- Ok(())- })?;+ Ok(())+ })+ })?;// Show summarylet copied_count = copied_count.load(Ordering::Relaxed);diff --git a/src/copy.rs b/src/copy.rsindex f3794cf18..bcef3afb4 100644--- a/src/copy.rs+++ b/src/copy.rs@@ -19,7 +19,7 @@ use rayon::prelude::*;/// contention causes performance to regress (benchmarked on APFS and ext4)./// The global rayon pool is sized for network I/O (2x cores) which is too/// many for local filesystem work.-const MAX_COPY_THREADS: usize = 4;+pub const MAX_COPY_THREADS: usize = 4;/// Copy a directory tree recursively using reflink (COW) per file.///@@ -31,8 +31,19 @@ const MAX_COPY_THREADS: usize = 4;/// removed before copying.////// Uses a dedicated rayon thread pool capped at `MAX_COPY_THREADS` to avoid-/// SSD I/O contention from the larger global pool.+/// SSD I/O contention from the larger global pool. When called from within an+/// existing rayon pool context (e.g. via `pool.install()`), that pool is reused+/// rather than creating a new one — this prevents concurrent callers from each+/// spawning their own pool and exhausting OS file-descriptor limits on large+/// trees (EMFILE / "too many open files").pub fn copy_dir_recursive(src: &Path, dest: &Path, force: bool) -> anyhow::Result<()> {+ // If we are already executing inside a rayon worker thread, reuse that+ // pool rather than creating a new one. This avoids concurrent callers+ // (e.g. from an outer par_iter) each allocating their own pool.+ if rayon::current_thread_index().is_some() {+ return copy_dir_recursive_inner(src, dest, force);+ }+let pool = rayon::ThreadPoolBuilder::new().num_threads(MAX_COPY_THREADS).build()diff --git a/tests/integration_tests/step_copy_ignored.rs b/tests/integration_tests/step_copy_ignored.rsindex fec170517..ef6c2c229 100644--- a/tests/integration_tests/step_copy_ignored.rs+++ b/tests/integration_tests/step_copy_ignored.rs@@ -1372,3 +1372,57 @@ fn test_copy_ignored_skips_nested_worktrees(mut repo: TestRepo) {".worktrees directory should NOT be copied (contains nested worktree)");}++/// Regression test for EMFILE ("too many open files") when copying many ignored+/// directories concurrently. The fix ensures step_copy_ignored creates a single+/// shared thread pool that copy_dir_recursive reuses, rather than each directory+/// spawning its own pool.+#[rstest]+fn test_copy_ignored_many_directories_no_emfile(mut repo: TestRepo) {+ let feature_path = repo.add_worktree("feature");++ // Create enough ignored directories to trigger concurrent pool pressure.+ // 200 directories × 10 files = 2000 files exercises the parallel copy path.+ let mut gitignore_entries = String::new();+ let mut worktreeinclude_entries = String::new();+ for i in 0..200 {+ let dir_name = format!("ignored-dir-{i}/");+ let dir = repo.root_path().join(format!("ignored-dir-{i}"));+ fs::create_dir_all(&dir).unwrap();+ for j in 0..10 {+ fs::write(+ dir.join(format!("file-{j}.txt")),+ format!("content {i}-{j}"),+ )+ .unwrap();+ }+ gitignore_entries.push_str(&dir_name);+ gitignore_entries.push('\n');+ worktreeinclude_entries.push_str(&dir_name);+ worktreeinclude_entries.push('\n');+ }+ fs::write(repo.root_path().join(".gitignore"), &gitignore_entries).unwrap();+ fs::write(+ repo.root_path().join(".worktreeinclude"),+ &worktreeinclude_entries,+ )+ .unwrap();++ run_copy_ignored(&repo, &feature_path);++ // Spot-check a sample of copied directories+ for i in [0, 99, 199] {+ for j in [0, 5, 9] {+ let dst_file = feature_path.join(format!("ignored-dir-{i}/file-{j}.txt"));+ assert!(+ dst_file.exists(),+ "ignored-dir-{i}/file-{j}.txt should be copied"+ );+ assert_eq!(+ fs::read_to_string(&dst_file).unwrap(),+ format!("content {i}-{j}"),+ "ignored-dir-{i}/file-{j}.txt content should match"+ );+ }+ }+}