Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cgroup2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ func Load(group string, opts ...InitOpts) (*Manager, error) {
return nil, err
}
path := filepath.Join(c.mountpoint, group)
_, err := os.Stat(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we should check this. It's just loading into object.
All the operations doesn't hold fd for this path. It could be valid case that Load it first and create it later.

ping @AkihiroSuda @mikebrow @dmcgowan to review this.

Copy link
Member

@dcantah dcantah Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would, maybe naively, expect a function named Load to verify what we were trying to load is valid, and that's how I'd envision the API would be used (try to load an existing cgroup, and if it's not there I'd want to get some classic stdlib error to handle the not existing case). We could discuss whether Stat is a good enough metric for proving it's valid as well (e.g. maybe we want to actually see whether the path is a path in a cg2 mountpoint), but that's another topic. The API for creating and getting a handle to a cg2 in this library is NewManager(), and the only other way to create a new cg through the API is manager.NewChild(), but the expectation is you'd be creating a child cg from the one you already have a handle to.

if err != nil {
return nil, err
}
return &Manager{
unifiedMountpoint: c.mountpoint,
path: path,
Expand Down
20 changes: 20 additions & 0 deletions cgroup2/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cgroup2
import (
"context"
"fmt"
"io/fs"
"os"
"os/exec"
"syscall"
Expand Down Expand Up @@ -466,3 +467,22 @@ func BenchmarkStat(b *testing.B) {
func toPtr[T any](v T) *T {
return &v
}

func TestLoadOKWhenExistent(t *testing.T) {
checkCgroupMode(t)
group := "/existent"

_, err := NewManager(defaultCgroup2Path, group, &Resources{})
require.NoError(t, err)

_, err = Load(group)
require.NoError(t, err)
}

func TestLoadErrorsWhenNonExistent(t *testing.T) {
checkCgroupMode(t)
group := "/nonexistent"

_, err := Load(group)
require.ErrorIs(t, err, fs.ErrNotExist)
}
Loading