Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit fe6605d

Browse files
committed
close all namespace handles to avoid leakes deleting the net namespace
Change-Id: Ie630f6206fd58da2d5575dd1799934cd32a339b6
1 parent 297bb91 commit fe6605d

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

pkg/driver/hostdevice.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func nsAttachNetdev(hostIfName string, containerNsPAth string, ifName string) er
4646
if err != nil {
4747
return err
4848
}
49+
defer containerNs.Close()
4950

5051
attrs := hostDev.Attrs()
5152
// Store the original name
@@ -93,6 +94,7 @@ func nsAttachNetdev(hostIfName string, containerNsPAth string, ifName string) er
9394
if err != nil {
9495
return err
9596
}
97+
defer nhNs.Close()
9698

9799
nsLink, err := nhNs.LinkByName(attrs.Name)
98100
if err != nil {
@@ -121,12 +123,14 @@ func nsDetachNetdev(containerNsPAth string, devName string) error {
121123
if err != nil {
122124
return fmt.Errorf("could not get network namespace from path %s for network device %s : %w", containerNsPAth, devName, err)
123125
}
126+
defer containerNs.Close()
124127
// to avoid golang problem with goroutines we create the socket in the
125128
// namespace and use it directly
126129
nhNs, err := netlink.NewHandleAt(containerNs)
127130
if err != nil {
128131
return fmt.Errorf("could not get network namespace handle: %w", err)
129132
}
133+
defer nhNs.Close()
130134

131135
nsLink, err := nhNs.LinkByName(devName)
132136
if err != nil {

pkg/inventory/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (db *DB) AddPodNetns(pod string, netnsPath string) {
8080
klog.Infof("fail to get pod %s network namespace %s handle: %v", pod, netnsPath, err)
8181
return
8282
}
83+
defer ns.Close()
8384
id, err := netlink.GetNetNsIdByFd(int(ns))
8485
if err != nil {
8586
klog.Infof("fail to get pod %s network namespace %s netnsid: %v", pod, netnsPath, err)

0 commit comments

Comments
 (0)