@@ -20,9 +20,9 @@ package process
20
20
21
21
import (
22
22
"context"
23
- "fmt"
24
23
"net/url"
25
24
"os"
25
+ "path/filepath"
26
26
"strings"
27
27
"testing"
28
28
@@ -69,30 +69,29 @@ func TestNewBinaryIOCleanup(t *testing.T) {
69
69
70
70
func descriptorCount (t * testing.T ) int {
71
71
t .Helper ()
72
- files , _ := os .ReadDir ("/proc/self/fd" )
72
+ const dir = "/proc/self/fd"
73
+ files , _ := os .ReadDir (dir )
73
74
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.
79
79
//
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" ) {
88
91
continue
89
92
}
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 ++
95
94
}
96
95
97
- return len ( files )
96
+ return count
98
97
}
0 commit comments