Skip to content

Commit bb30cf1

Browse files
committed
chore: test code to verify fd leaks
See siderolabs#9413 Not sure how to turn this into a useful test. Signed-off-by: Andrey Smirnov <[email protected]>
1 parent dec653b commit bb30cf1

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ COPY --chmod=0644 hack/cri-containerd.toml /rootfs/etc/cri/containerd.toml
659659
COPY --chmod=0644 hack/cri-plugin.part /rootfs/etc/cri/conf.d/00-base.part
660660
COPY --chmod=0644 hack/udevd/80-net-name-slot.rules /rootfs/usr/lib/udev/rules.d/
661661
COPY --chmod=0644 hack/lvm.conf /rootfs/etc/lvm/lvm.conf
662+
COPY --chmod=0755 hack/fdspy/fdspy /rootfs/usr/bin/fdspy
662663
RUN <<END
663664
ln -s /usr/share/zoneinfo/Etc/UTC /rootfs/etc/localtime
664665
touch /rootfs/etc/{extensions.yaml,resolv.conf,hosts,os-release,machine-id,cri/conf.d/cri.toml,cri/conf.d/01-registries.part,cri/conf.d/20-customization.part,ssl/certs/ca-certificates}

hack/fdspy/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/siderolabs/fdspy
2+
3+
go 1.23.1

hack/fdspy/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
"os"
10+
)
11+
12+
func main() {
13+
fds, err := os.ReadDir("/proc/self/fd")
14+
if err != nil {
15+
panic(err)
16+
}
17+
18+
for _, fd := range fds {
19+
fname, err := os.Readlink(fmt.Sprintf("/proc/self/fd/%s", fd.Name()))
20+
if err != nil {
21+
fmt.Fprintln(os.Stderr, fd.Name(), " --> ", err)
22+
} else {
23+
fmt.Fprintln(os.Stderr, fd.Name(), " --> ", fname)
24+
}
25+
}
26+
27+
os.Exit(1)
28+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package runtime
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"time"
11+
12+
"github.com/cosi-project/runtime/pkg/controller"
13+
"github.com/siderolabs/go-cmd/pkg/cmd"
14+
"go.uber.org/zap"
15+
)
16+
17+
// FDSpyController activates LVM volumes when they are discovered by the block.DiscoveryController.
18+
type FDSpyController struct {
19+
}
20+
21+
// Name implements controller.Controller interface.
22+
func (ctrl *FDSpyController) Name() string {
23+
return "runtime.FDSpyController"
24+
}
25+
26+
// Inputs implements controller.Controller interface.
27+
func (ctrl *FDSpyController) Inputs() []controller.Input {
28+
return nil
29+
}
30+
31+
// Outputs implements controller.Controller interface.
32+
func (ctrl *FDSpyController) Outputs() []controller.Output {
33+
return nil
34+
}
35+
36+
// Run implements controller.Controller interface.
37+
//
38+
//nolint:gocyclo
39+
func (ctrl *FDSpyController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
40+
ticker := time.NewTicker(5 * time.Second)
41+
defer ticker.Stop()
42+
43+
for {
44+
select {
45+
case <-ctx.Done():
46+
return nil
47+
case <-r.EventCh():
48+
case <-ticker.C:
49+
}
50+
51+
if _, err := cmd.RunContext(ctx,
52+
"/usr/bin/fdspy",
53+
); err != nil {
54+
return fmt.Errorf("failed to run fdspy: %w", err)
55+
}
56+
}
57+
}

internal/app/machined/pkg/runtime/v1alpha2/v1alpha2_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ func (ctrl *Controller) Run(ctx context.Context, drainer *runtime.Drainer) error
298298
ConfigPath: constants.ExtensionServiceConfigPath,
299299
},
300300
&runtimecontrollers.ExtensionStatusController{},
301+
&runtimecontrollers.FDSpyController{},
301302
&runtimecontrollers.KernelModuleConfigController{},
302303
&runtimecontrollers.KernelModuleSpecController{
303304
V1Alpha1Mode: ctrl.v1alpha1Runtime.State().Platform().Mode(),

0 commit comments

Comments
 (0)