Skip to content

Commit

Permalink
pin: fix a bunch of fd leaks in tests
Browse files Browse the repository at this point in the history
The new pin package didn't import fdtrace.TestMain until now, which
is why the leaks went by unnoticed.

Signed-off-by: Lorenz Bauer <[email protected]>
  • Loading branch information
lmb authored and ti-mo committed Jan 24, 2025
1 parent f224bff commit a0e13c4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pin/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pin

import (
"fmt"
"io"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/internal/sys"
Expand All @@ -11,6 +12,7 @@ import (
// Pinner is an interface implemented by all eBPF objects that support pinning
// to a bpf virtual filesystem.
type Pinner interface {
io.Closer
Pin(string) error
}

Expand Down
7 changes: 7 additions & 0 deletions pin/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/testutils/fdtrace"
)

func mustPinnedProgram(t *testing.T, path string) *ebpf.Program {
Expand Down Expand Up @@ -77,9 +78,15 @@ func TestLoad(t *testing.T) {

m, err := Load(mpath, nil)
qt.Assert(t, qt.IsNil(err))
defer m.Close()
qt.Assert(t, qt.Satisfies(m, testutils.Contains[*ebpf.Map]))

p, err := Load(ppath, nil)
qt.Assert(t, qt.IsNil(err))
defer p.Close()
qt.Assert(t, qt.Satisfies(p, testutils.Contains[*ebpf.Program]))
}

func TestMain(m *testing.M) {
fdtrace.TestMain(m)
}
4 changes: 4 additions & 0 deletions pin/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func TestWalkDir(t *testing.T) {
bpffn := func(path string, d fs.DirEntry, obj Pinner, err error) error {
qt.Assert(t, qt.IsNil(err))

if obj != nil {
defer obj.Close()
}

if path == "." {
return nil
}
Expand Down

0 comments on commit a0e13c4

Please sign in to comment.