Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/v1.16/BUG FIXES-20260324-113149.yaml
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"
6 changes: 5 additions & 1 deletion internal/modsdir/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package modsdir

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -172,12 +173,15 @@ func (m Manifest) WriteSnapshot(w io.Writer) error {
return err
}

func (m Manifest) WriteSnapshotToDir(dir string) error {
func (m Manifest) WriteSnapshotToDir(dir string) (retErr error) {
fn := filepath.Join(dir, ManifestSnapshotFilename)
log.Printf("[TRACE] modsdir: writing modules manifest to %s", fn)
w, err := os.Create(fn)
if err != nil {
return err
}
defer func() {
retErr = errors.Join(retErr, w.Close())
}()
return m.WriteSnapshot(w)
}
73 changes: 73 additions & 0 deletions internal/modsdir/manifest_test.go
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 {
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.

Why are 2 file descriptors expected to leak?

Copy link
Copy Markdown
Author

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.

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
}
Loading