Skip to content
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

Avoid leaking fds in test suite and NewMapWithOptions() #725

Merged
merged 2 commits into from
Jul 1, 2022
Merged
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
9 changes: 9 additions & 0 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ func TestCollectionSpecLoadCopy(t *testing.T) {
if err != nil {
t.Fatal("Loading original spec:", err)
}
defer objs.Prog.Close()

if err := spec2.LoadAndAssign(&objs, nil); err != nil {
t.Fatal("Loading copied spec:", err)
}
defer objs.Prog.Close()
}

func TestCollectionSpecRewriteMaps(t *testing.T) {
Expand Down Expand Up @@ -309,6 +311,9 @@ func TestCollectionSpecMapReplacements_SpecMismatch(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// Map fd is duplicated by MapReplacements, this one can be safely closed.
defer newMap.Close()

coll, err := NewCollectionWithOptions(cs, CollectionOptions{
MapReplacements: map[string]*Map{
"test-map": newMap,
Expand Down Expand Up @@ -365,6 +370,8 @@ func TestCollectionSpec_LoadAndAssign_LazyLoading(t *testing.T) {
if err := spec.LoadAndAssign(&objs, nil); err != nil {
t.Fatal("Assign loads a map or program that isn't requested in the struct:", err)
}
defer objs.Prog.Close()
defer objs.Map.Close()

if objs.Prog == nil {
t.Error("Program is nil")
Expand Down Expand Up @@ -572,6 +579,8 @@ func ExampleCollectionSpec_LoadAndAssign() {
if err := spec.LoadAndAssign(&objs, nil); err != nil {
panic(err)
}
defer objs.Program.Close()
defer objs.Map.Close()

fmt.Println(objs.Program.Type())
fmt.Println(objs.Map.Type())
Expand Down
2 changes: 2 additions & 0 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ func TestTailCall(t *testing.T) {
t.Fatal(err)
}
defer obj.TailMain.Close()
defer obj.ProgArray.Close()

ret, _, err := obj.TailMain.Test(make([]byte, 14))
testutils.SkipIfNotSupported(t, err)
Expand Down Expand Up @@ -518,6 +519,7 @@ func TestSubprogRelocation(t *testing.T) {
t.Fatal(err)
}
defer obj.Main.Close()
defer obj.HashMap.Close()

ret, _, err := obj.Main.Test(make([]byte, 14))
testutils.SkipIfNotSupported(t, err)
Expand Down
2 changes: 2 additions & 0 deletions linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestFindReferences(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer prog.Close()

ret, _, err := prog.Test(make([]byte, 14))
if err != nil {
Expand Down Expand Up @@ -90,6 +91,7 @@ func TestForwardFunctionDeclaration(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer prog.Close()

ret, _, err := prog.Test(make([]byte, 14))
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ func NewMapWithOptions(spec *MapSpec, opts MapOptions) (*Map, error) {
return nil, fmt.Errorf("creating map: %w", err)
}

err = m.finalize(spec)
if err != nil {
if err := m.finalize(spec); err != nil {
m.Close()
return nil, fmt.Errorf("populating map: %w", err)
}

Expand Down Expand Up @@ -1039,7 +1039,8 @@ func (m *Map) Iterate() *MapIterator {
return newMapIterator(m)
}

// Close removes a Map
// Close the Map's underlying file descriptor, which could unload the
// Map from the kernel if it is not pinned or in use by a loaded Program.
func (m *Map) Close() error {
if m == nil {
// This makes it easier to clean up when iterating maps
Expand Down
4 changes: 3 additions & 1 deletion map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func TestBatchMapWithLock(t *testing.T) {
if err != nil {
t.Fatal("Can't parse ELF:", err)
}
defer coll.Close()

type spinLockValue struct {
Cnt uint32
Expand Down Expand Up @@ -353,6 +354,7 @@ func TestMapWithLock(t *testing.T) {
if err != nil {
t.Fatal("Can't parse ELF:", err)
}
defer coll.Close()

type spinLockValue struct {
Cnt uint32
Expand Down Expand Up @@ -1240,11 +1242,11 @@ func TestIterateMapInMap(t *testing.T) {
m *Map
entries = parent.Iterate()
)
defer m.Close()

if !entries.Next(&key, &m) {
t.Fatal("Iterator encountered error:", entries.Err())
}
m.Close()

if key != 1 {
t.Error("Iterator didn't skip first entry")
Expand Down
4 changes: 3 additions & 1 deletion prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,9 @@ func (p *Program) IsPinned() bool {
return p.pinnedPath != ""
}

// Close unloads the program from the kernel.
// Close the Program's underlying file descriptor, which could unload
// the program from the kernel if it is not pinned or attached to a
// kernel hook.
func (p *Program) Close() error {
if p == nil {
return nil
Expand Down
1 change: 1 addition & 0 deletions prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ func TestProgramMarshaling(t *testing.T) {
if err := arr.Lookup(idx, &prog2); err != nil {
t.Fatal("Can't unmarshal program:", err)
}
defer prog2.Close()

if prog2 == nil {
t.Fatal("Unmarshalling set program to nil")
Expand Down