-
Notifications
You must be signed in to change notification settings - Fork 10.3k
fix: close modsdir snapshot file descriptors #38311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
buley
wants to merge
2
commits into
hashicorp:main
Choose a base branch
from
buley:buley/issue-38302-close-modsdir-snapshot-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| kind: BUG FIXES | ||
| body: Close module manifest snapshot files after writing to avoid leaking file descriptors. | ||
| time: 2026-03-24T11:31:49-07:00 | ||
| custom: | ||
| Issue: "38302" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| //go:build darwin || linux | ||
|
|
||
| // Copyright IBM Corp. 2014, 2026 | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package modsdir | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime/debug" | ||
| "testing" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func TestManifestWriteSnapshotToDirClosesFile(t *testing.T) { | ||
| oldGCPercent := debug.SetGCPercent(-1) | ||
| defer debug.SetGCPercent(oldGCPercent) | ||
|
|
||
| manifest := Manifest{ | ||
| "root": { | ||
| Key: "root", | ||
| Dir: "modules/root", | ||
| }, | ||
| } | ||
|
|
||
| baseDir := t.TempDir() | ||
| before := countOpenFileDescriptors(t) | ||
|
|
||
| const iterations = 32 | ||
| for i := range iterations { | ||
| dir := filepath.Join(baseDir, fmt.Sprintf("manifest-%d", i)) | ||
| if err := os.Mkdir(dir, 0o755); err != nil { | ||
| t.Fatalf("creating manifest dir %d: %s", i, err) | ||
| } | ||
|
|
||
| if err := manifest.WriteSnapshotToDir(dir); err != nil { | ||
| t.Fatalf("writing manifest %d: %s", i, err) | ||
| } | ||
| } | ||
|
|
||
| after := countOpenFileDescriptors(t) | ||
| if leaked := after - before; leaked > 2 { | ||
| t.Fatalf("expected WriteSnapshotToDir to close its file descriptor, but open descriptor count increased by %d", leaked) | ||
| } | ||
| } | ||
|
|
||
| func countOpenFileDescriptors(t *testing.T) int { | ||
| t.Helper() | ||
|
|
||
| var limit unix.Rlimit | ||
| if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &limit); err != nil { | ||
| t.Fatalf("reading RLIMIT_NOFILE: %s", err) | ||
| } | ||
|
|
||
| maxFD := int(limit.Cur) | ||
| if maxFD > 4096 { | ||
| maxFD = 4096 | ||
| } | ||
|
|
||
| openDescriptors := 0 | ||
| for fd := range maxFD { | ||
| if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err == nil { | ||
| openDescriptors++ | ||
| } else if err != unix.EBADF { | ||
| t.Fatalf("checking file descriptor %d: %s", fd, err) | ||
| } | ||
| } | ||
|
|
||
| return openDescriptors | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are 2 file descriptors expected to leak?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Appreciate your question, @DanielMSchmidt. The 2 isn't an expected leak count: it's a small tolerance margin to keep the test from being flaky due to unrelated runtime activity. The Go runtime, the test harness, and the logging subsystem may transiently open or close a file descriptor between the before and after snapshots, so a strict leaked > 0 check could produce false positives.
The test writes 32 iterations specifically so that a real leak is unambiguous: without the fix all 32 descriptors leak, which is well above the threshold. With the fix, the count stays at or near zero. So the invariant is really "no meaningful growth" rather than "exactly zero," with 2 as a conservative noise floor.
I could tighten this or use a different approach if you'd prefer — for example, I could check that leaked < iterations with a comment explaining the margin, or switch to a percentage-based threshold.