8000 Use os.ReadDir instead of filepath.Walk in netns cleanup · moby/moby@5d8286b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d8286b

Browse files
committed
Use os.ReadDir instead of filepath.Walk in netns cleanup
- Use `os.ReadDir()` instead of `filepath.WalkDir()`, because `filepath.WalkDir()` does a lot of extra things we don't need - Log errors on directory removal (omitting `os.IsNotExist`) - Touch-up error messages for consistency Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 17c3829 commit 5d8286b

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

testutil/daemon/daemon_linux.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package daemon // import "github.com/docker/docker/testutil/daemon"
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -18,13 +19,32 @@ func cleanupNetworkNamespace(t testing.TB, d *Daemon) {
1819
// daemon instance and has no chance of getting
1920
// cleaned up when a new daemon is instantiated with a
2021
// new exec root.
21-
filepath.WalkDir(filepath.Join(d.execRoot, "netns"), func(path string, _ os.DirEntry, _ error) error {
22+
netnsPath := filepath.Join(d.execRoot, "netns")
23+
files, err := os.ReadDir(netnsPath)
24+
if err != nil {
25+
if !errors.Is(err, os.ErrNotExist) {
26+
t.Logf("[%s] unable to read netns path (%s): %s", d.id, netnsPath, err)
27+
}
28+
return
29+
}
30+
31+
for _, dir := range files {
32+
if !dir.IsDir() {
33+
continue
34+
}
35+
36+
path := filepath.Join(netnsPath, dir.Name())
37+
38+
// Unmount the network namespace using MNT_DETACH (lazy unmount).
39+
// Ignore EINVAL ("target is not a mount point"), and ENOENT (not found),
40+
// which could occur if the network namespace was already gone.
2241
if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT {
23-
t.Logf("[%s] unmount of %s failed: %v", d.id, path, err)
42+
t.Logf("[%s] unmount of network namespace %s failed: %v", d.id, path, err)
43+
}
44+
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
45+
t.Logf("[%s] unable to remove network namespace %s: %s", d.id, path, err)
2446
}
25-
os.Remove(path)
26-
return nil
27-
})
47+
}
2848
}
2949

3050
// CgroupNamespace returns the cgroup namespace the daemon is running in

testutil/daemon/daemon_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func cleanupMount(t testing.TB, d *Daemon) {
2222

2323
// SignalDaemonDump sends a signal to the daemon to write a dump file
2424
func SignalDaemonDump(pid int) {
25-
unix.Kill(pid, unix.SIGQUIT)
25+
_ = unix.Kill(pid, unix.SIGQUIT)
2626
}
2727

2828
func signalDaemonReload(pid int) error {

0 commit comments

Comments
 (0)
0