Skip to content

Commit 59cf2f9

Browse files
authored
Merge pull request containerd#10776 from austinvazquez/cherry-pick-94c163209db612dbf1394e73060031953b34ac88-to-1.7
[release/1.7 backport] TestNewBinaryIOCleanup: fix a comment, minor rewrite
2 parents e9360b7 + 7fd794a commit 59cf2f9

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

pkg/process/io_test.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ package process
2020

2121
import (
2222
"context"
23-
"fmt"
2423
"net/url"
2524
"os"
25+
"path/filepath"
2626
"strings"
2727
"testing"
2828

@@ -69,30 +69,29 @@ func TestNewBinaryIOCleanup(t *testing.T) {
6969

7070
func descriptorCount(t *testing.T) int {
7171
t.Helper()
72-
files, _ := os.ReadDir("/proc/self/fd")
72+
const dir = "/proc/self/fd"
73+
files, _ := os.ReadDir(dir)
7374

74-
// Go 1.23 introduced a new internal file descriptor type "pidfd"
75-
// that we don't want to count towards the total file descriptors in
76-
// use by the process. This retains the behavior of previous Go
77-
// versions.
78-
// See https://go.dev/issues/62654.
75+
// Go 1.23+ uses pidfd instead of PID for processes started by a user,
76+
// if possible (see https://go.dev/cl/570036). As a side effect, every
77+
// os.StartProcess or os.FindProcess call results in an extra opened
78+
// file descriptor, which is only closed in p.Wait or p.Release.
7979
//
80-
// Once the proposal to check for internal file descriptors is
81-
// accepted, we can use that instead to detect internal fds in use
82-
// by the Go runtime.
83-
// See https://go.dev/issues/67639.
84-
for i, file := range files {
85-
sym, err := os.Readlink(fmt.Sprintf("/proc/self/fd/%s", file.Name()))
86-
if err != nil {
87-
// ignore fds that cannot be followed.
80+
// To retain compatibility with previous Go versions (or Go 1.23+
81+
// behavior on older kernels), let's not count pidfds.
82+
//
83+
// TODO: if the proposal to check for internal file descriptors
84+
// (https://go.dev/issues/67639) is accepted, we can use that
85+
// instead to detect internal fds in use by the Go runtime.
86+
count := 0
87+
for _, file := range files {
88+
sym, err := os.Readlink(filepath.Join(dir, file.Name()))
89+
// Either pidfd:[70517] or anon_inode:[pidfd] (on Linux 5.4).
90+
if err == nil && strings.Contains(sym, "pidfd") {
8891
continue
8992
}
90-
91-
if strings.Contains(sym, "pidfd") {
92-
// Either pidfd:[70517] or anon_inode:[pidfd] (on Linux 5.4)
93-
files = append(files[:i], files[i+1:]...)
94-
}
93+
count++
9594
}
9695

97-
return len(files)
96+
return count
9897
}

0 commit comments

Comments
 (0)