Skip to content

fs.readdir: release pending_err mutex in recursive subtask error path - #32748

Merged
Jarred-Sumner merged 2 commits into
mainfrom
farm/c2622b73/readdir-recursive-pending-err-deadlock
Jun 26, 2026
Merged

Jarred-Sumner merged 2 commits into
mainfrom
farm/c2622b73/readdir-recursive-pending-err-deadlock

Conversation

@robobun

@robobun robobun commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Problem

fs.promises.readdir(path, { recursive: true }) hangs forever (release) or panics Deadlock detected on a thread-pool worker (debug) when two or more of its per-directory subtasks fail. The returned promise never settles, with nothing on stderr in a release build.

panic: Deadlock detected
  <bun_threading::mutex::Mutex>::lock                      src/threading/Mutex.rs:181
  <bun_runtime::node::fs::_async_tasks::AsyncReaddirRecursiveTask>::perform_work
  <bun_runtime::node::fs::_async_tasks::ReaddirSubtask>::run_owned
  <bun_threading::thread_pool::Thread>::run

Cause

AsyncReaddirRecursiveTask::perform_work takes the per-task pending_err_mutex on the error path with

let _lock = self.pending_err_mutex.lock();

bun_threading::Mutex::lock() returns (), not a guard, so _lock is the unit value and the mutex is never released. The first failing subtask locks it and decrements subtask_count; the next failing subtask on the same worker trips the debug deadlock detector (locking_thread == current_thread_id), and on any other worker blocks forever in FutexImpl::lock_slow. subtask_count never reaches zero, so finish_concurrently never runs and the user-facing promise never settles.

This is the only site in the tree where a raw bun_threading::Mutex::lock() result was bound to a variable; every other let _g = x.lock() pattern is on Guarded<T>, bun_core::Mutex<T>, or bun_alloc::Mutex, all of which return a real guard.

Fix

Use lock_guard() so the mutex is released when the scope exits.

Verification

New test in test/js/node/fs/fs.test.ts creates 64 self-referencing symlinks (each subtask's openat(..., O_DIRECTORY) fails with ELOOP, which is not in the walker's swallowed set of ENOENT/ENOTDIR/EPERM) and asserts the spawned child rejects with ELOOP and exits 0. Without the fix the child panics in debug and hangs in release; with it the promise rejects in well under a second.

AsyncReaddirRecursiveTask::perform_work acquired pending_err_mutex via
bun_threading::Mutex::lock(), which returns (), not a guard. The unit
binding meant the mutex was never unlocked. When a second failing subtask
ran on the same thread-pool worker the debug deadlock detector panicked
("Deadlock detected"); in release builds every other worker blocked on
the mutex forever, subtask_count never reached zero, and the returned
promise from fs.promises.readdir(path, {recursive: true}) never settled.

Use lock_guard() so the mutex is released when the scope exits.
@robobun

robobun commented Jun 26, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 3:46 AM PT - Jun 26th, 2026

✅ @autofix-ci[bot], your commit ccc086488efd8f845dca1e068f70ac7e564b3065 passed in Build #64908! 🎉


🧪   To try this PR locally:

bunx bun-pr 32748

That installs a local version of the PR into your bun-32748 executable, so you can run:

bun-32748 --bun

@coderabbitai

coderabbitai Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9e2ddd1e-cfb6-4df0-a07f-7b12cfcd55c7

📥 Commits

Reviewing files that changed from the base of the PR and between 3818d2d and ccc0864.

📒 Files selected for processing (1)
  • test/js/node/fs/fs.test.ts

Walkthrough

fs.promises.readdir({ recursive: true }) now uses guard-based mutex locking in its recursive error path, and the test suite adds a subprocess regression that expects ELOOP from multiple symlink failures without hanging.

Changes

Recursive readdir error handling

Layer / File(s) Summary
Mutex acquisition in recursive readdir
src/runtime/node/node_fs.rs
AsyncReaddirRecursiveTask::perform_work acquires pending_err_mutex with lock_guard() in the recursive readdir error path.
Recursive readdir regression test
test/js/node/fs/fs.test.ts
A subprocess test creates many self-referencing symlinks, calls fs.promises.readdir(dir, { recursive: true }), and asserts rejection with ELOOP and a clean exit.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main fix to the recursive readdir error path.
Description check ✅ Passed The description covers the problem, fix, and verification, though it uses custom headings instead of the template labels.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — one-line swap from lock() (returns ()) to lock_guard() so the pending_err mutex actually releases; verified against the bun_threading::Mutex API.

Extended reasoning...

Overview

A single-token change in src/runtime/node/node_fs.rs: self.pending_err_mutex.lock() → self.pending_err_mutex.lock_guard() in the error path of AsyncReaddirRecursiveTask::perform_work. The field is a bun_threading::Mutex, whose lock() returns () (manual unlock() required) while lock_guard() returns an RAII MutexGuard that unlocks on drop — I verified both signatures in src/threading/Mutex.rs. Binding () to _lock meant the mutex was never released, so a second failing subtask would deadlock (debug panic / release hang). The accompanying test spawns a subprocess with 64 self-referencing symlinks to force many concurrent ELOOP failures and asserts the promise rejects cleanly within a 10s timeout.

Security risks

None. No new inputs, no auth/crypto/permissions surface; this only affects whether an internal mutex is released on an error path.

Level of scrutiny

Low. The diagnosis in the PR description is precise and matches the Mutex API in-tree, the fix is the minimal correct change, and the guard is held over exactly the same scope the original code intended. The test is run in a subprocess with a timeout so a regression can't hang CI.

Other factors

The robobun build-failure comment references the pre-autofix commit (3818d2d); an autofix commit (ccc0864) landed after, and the source change itself is trivially correct Rust that compiles by inspection. No outstanding human reviewer comments.

@Jarred-Sumner
Jarred-Sumner merged commit ba72810 into main Jun 26, 2026
78 checks passed
@Jarred-Sumner
Jarred-Sumner deleted the farm/c2622b73/readdir-recursive-pending-err-deadlock branch June 26, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants