Skip to content

glob: fix fd leak on NAMETOOLONG and remove no-op arena frees#29380

Merged
dylan-conway merged 1 commit into
mainfrom
ali/glob-walker-cleanup
Apr 20, 2026
Merged

glob: fix fd leak on NAMETOOLONG and remove no-op arena frees#29380
dylan-conway merged 1 commit into
mainfrom
ali/glob-walker-cleanup

Conversation

@alii

@alii alii commented Apr 16, 2026

Copy link
Copy Markdown
Member

Three small cleanups in GlobWalker.zig:

  • transitionToDirIterState: close work_item.fd before returning NAMETOOLONG, matching the cleanup pattern used in the other error paths in this function.
  • prepareMatchedPath/appendMatchedPath: remove arena.allocator().free() calls on the duplicate-match path. These are no-ops on std.heap.ArenaAllocator, and one of them ran before a log statement that still referenced the freed buffer.
  • Remove redundant dupeZ of an already-arena-backed path on the lstat error path.

- transitionToDirIterState: close work_item.fd before returning NAMETOOLONG,
  matching the cleanup pattern used in the other error paths in this function.
- prepareMatchedPath/appendMatchedPath: remove arena.allocator().free() calls
  on the duplicate-match path. These are no-ops on an arena, and one of them
  ran before a log statement that still referenced the freed buffer.
- Remove redundant dupeZ of an already-arena-backed path on the lstat error
  path.
@robobun

robobun commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator
Updated 6:04 PM PT - Apr 16th, 2026

@alii, your commit 8bc7ee0 has 6 failures in Build #45947 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 29380

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

bun-29380 --bun

@github-actions

Copy link
Copy Markdown
Contributor

Found 1 issue this PR may fix:

  1. Glob sometimes fails on directories mounted via sshfs #7412 - Glob fails on sshfs-mounted directories on subsequent runs; the fd leak on NAMETOOLONG could cause fd exhaustion, explaining the empty results and recovery after ~30-60 seconds

If this is helpful, copy the block below into the PR description to auto-close this issue on merge.

Fixes #7412

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

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: af015332-8fbe-4259-b0f7-24069f4b327c

📥 Commits

Reviewing files that changed from the base of the PR and between b2d3f5d and 8bc7ee0.

📒 Files selected for processing (1)
  • src/glob/GlobWalker.zig

Walkthrough

Modified error handling and file descriptor lifecycle management in glob walker. Removed unnecessary path duplication in error path handling, added conditional file descriptor closure for path length errors, and changed duplicate matched-path bookkeeping to retain instead of free newly created strings.

Changes

Cohort / File(s) Summary
Resource Management and Error Handling
src/glob/GlobWalker.zig
Modified error path handling to attach original path directly without duplicating into arena allocator; added conditional file descriptor closure via closeDisallowingCwd when handling path length errors; changed duplicate matched-path bookkeeping to retain newly created strings instead of freeing them from arena allocator.
🚥 Pre-merge checks | ✅ 1 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The pull request description is incomplete. It provides detailed technical information about the changes but does not follow the required template structure with 'What does this PR do?' and 'How did you verify your code works?' sections. Restructure the description to match the repository template by adding 'What does this PR do?' and 'How did you verify your code works?' sections with corresponding content.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title accurately summarizes the main changes: fixing a file descriptor leak on NAMETOOLONG errors and removing unnecessary arena allocator free operations.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

@alii

alii commented Apr 16, 2026

Copy link
Copy Markdown
Member Author

@robobun adopt

@robobun

robobun commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator

✅ Adopted.

Local: path-length.test.ts passes (4/4) on debug+ASAN. leak.test.ts times out in the capped container at the 5s default — same on base, unrelated.

CI #45947: all Linux (incl. ASAN) + Windows x64 green. Remaining reds are pre-existing on the base commit — #29358 merged with the same darwin-13-x64 / windows-11-aarch64 exit-2 failures, and darwin-13-aarch64 expired (no agent). Nothing glob-related.

On testability: the NAMETOOLONG-with-live-fd branch is defensive only — newWithFd (L850) reuses work_item.path which already cleared the identical length check at L797, so the new close is unreachable with current callers. The arena free removals and the dropped dupeZ are no-ops / redundant allocs with no observable behavior to assert on.

Approved by Jarred; no open review threads.

@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 — three targeted fixes with no side effects.

Extended reasoning...

Overview

Single-file PR touching src/glob/GlobWalker.zig. Three independent cleanups: an fd leak on the NAMETOOLONG error path, removal of no-op ArenaAllocator.free() calls (one of which preceded a log statement still referencing the buffer), and elimination of a redundant dupeZ on a path that was already null-terminated and arena-backed.

Security risks

None. No auth, crypto, or permission-sensitive code paths are touched.

Level of scrutiny

Low. Each change is a contained, mechanically verifiable fix with no branching logic or design decisions involved. The fd-close pattern matches what the rest of the function already does. The arena-free calls are documented no-ops in Zig's stdlib. The dupeZ removal is safe because path is typed [:0]const u8 at that point.

Other factors

No outstanding reviewer comments. Build CI was triggered. Bug hunting system found nothing. All three changes reduce the gap between intent and implementation without altering observable behaviour in the non-error paths.

@dylan-conway dylan-conway merged commit 0fb375d into main Apr 20, 2026
61 of 67 checks passed
@dylan-conway dylan-conway deleted the ali/glob-walker-cleanup branch April 20, 2026 23:14
structwafel pushed a commit to structwafel/bun that referenced this pull request Apr 25, 2026
…h#29380)

Three small cleanups in `GlobWalker.zig`:

- `transitionToDirIterState`: close `work_item.fd` before returning
`NAMETOOLONG`, matching the cleanup pattern used in the other error
paths in this function.
- `prepareMatchedPath`/`appendMatchedPath`: remove
`arena.allocator().free()` calls on the duplicate-match path. These are
no-ops on `std.heap.ArenaAllocator`, and one of them ran *before* a log
statement that still referenced the freed buffer.
- Remove redundant `dupeZ` of an already-arena-backed path on the lstat
error path.
xhjkl pushed a commit to xhjkl/bun that referenced this pull request May 14, 2026
…h#29380)

Three small cleanups in `GlobWalker.zig`:

- `transitionToDirIterState`: close `work_item.fd` before returning
`NAMETOOLONG`, matching the cleanup pattern used in the other error
paths in this function.
- `prepareMatchedPath`/`appendMatchedPath`: remove
`arena.allocator().free()` calls on the duplicate-match path. These are
no-ops on `std.heap.ArenaAllocator`, and one of them ran *before* a log
statement that still referenced the freed buffer.
- Remove redundant `dupeZ` of an already-arena-backed path on the lstat
error path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants