Skip to content

Fix: cgroupsv2 never returns ErrCgroupDeleted#5739

Closed
thaJeztah wants to merge 1 commit intocontainerd:mainfrom
thaJeztah:cgroupv2_deleted_detection
Closed

Fix: cgroupsv2 never returns ErrCgroupDeleted#5739
thaJeztah wants to merge 1 commit intocontainerd:mainfrom
thaJeztah:cgroupv2_deleted_detection

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

I noticed that this code is checking for cgroupsv2.ErrCgroupDeleted

However, cgroupsv2.LoadManager() will never return this error type, rendering
the cgroupsv2.NewManager() code to be unused.

Even if cgroupsv2. would be used, calling NewManager() with a nil resources
would produce an error ("resources reference is nil");
https://github.com/containerd/cgroups/blob/v1.0.1/v2/manager.go#L169-L172

func NewManager(mountpoint string, group string, resources *Resources) (*Manager, error) {
    if resources == nil {
        return nil, errors.New("resources reference is nil")
    }

This patch removes checks for cgroupsv2.ErrCgroupDeleted, as it is not used
in cgroups v2. As outlined above, this change should not affect the current
behavior.

Question remains: should containerd create a new manager? In the current
codebase, cgroupsv2.NewManager() is not used, which means that the cgroup
path is never (explicitly) created, and controllers are never enabled (by
containerd); https://github.com/containerd/cgroups/blob/v1.0.1/v2/manager.go#L176-L192

Cgroup paths will be created implicitly, as the code continues with cg.AddProc(),
which:

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Jul 15, 2021

Build succeeded.

@thaJeztah thaJeztah closed this Jul 15, 2021
@thaJeztah thaJeztah reopened this Jul 15, 2021
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Jul 15, 2021

Build succeeded.

@thaJeztah
Copy link
Copy Markdown
Member Author

Cgroup paths will be created implicitly, as the code continues with cg.AddProc(),
which:

Actually, this is not correct, because it won't create the parent path, so we will fail in this case; https://play.golang.org/p/6noxWN225Dh

package main

import (
        "fmt"
        "io/ioutil"
)

func main() {
        err := ioutil.WriteFile("/no/such/path/myfile.txt", []byte("hello world"), 0755)
        fmt.Println(err)
}

@thaJeztah
Copy link
Copy Markdown
Member Author

So (if I see it correctly) apply() will currently return an ErrNotExist if the cgroup path isn't there.

Perhaps LoadManager should return ErrCgroupDeleted (or perhaps it should be a ErrInvalidGroupPath) if it doesn't exist. However then we need to call NewManager() with a non-nil resources

Alternatively, we modify cg.AddProc() to create the cgroup path (depending on what the desirable behavior is).

@dmcgowan
Copy link
Copy Markdown
Member

If this is ready to be reviewed, can you rebase to get the tests passing?

I noticed that this code is checking for `cgroupsv2.ErrCgroupDeleted`

However, `cgroupsv2.LoadManager()` will never return this error type, rendering
the `cgroupsv2.NewManager()` code to be unused.

Even _if_ `cgroupsv2.` would be used, calling `NewManager()` with a `nil` resources
would produce an error ("resources reference is nil");
https://github.com/containerd/cgroups/blob/v1.0.1/v2/manager.go#L169-L172

    func NewManager(mountpoint string, group string, resources *Resources) (*Manager, error) {
        if resources == nil {
            return nil, errors.New("resources reference is nil")
        }

This patch removes checks for `cgroupsv2.ErrCgroupDeleted`, as it is not used
in cgroups v2. As outlined above, this change should not affect the current
behavior.

Question remains: *should* containerd create a new manager? In the current
codebase, `cgroupsv2.NewManager()` is not used, which means that the cgroup
path is never (explicitly) created, and controllers are never enabled (by
containerd); https://github.com/containerd/cgroups/blob/v1.0.1/v2/manager.go#L176-L192

Cgroup paths *will* be created implicitly, as the code continues with `cg.AddProc()`,
which:

- calls `writeValues()` https://github.com/containerd/cgroups/blob/v1.0.1/v2/manager.go#L319
- calls `Value.write()` https://github.com/containerd/cgroups/blob/v1.0.1/v2/manager.go#L162
- calls `ioutil.Writefile()` https://github.com/containerd/cgroups/blob/v1.0.1/v2/manager.go#L147.
    which creates files when missing: https://pkg.go.dev/io/ioutil#WriteFile

    > WriteFile writes data to a file named by filename. If the file does not exist,
    > WriteFile creates it with permissions perm (before umask) (...)

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah force-pushed the cgroupv2_deleted_detection branch from 3f3dbd6 to 031c749 Compare October 8, 2021 13:44
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Oct 8, 2021

Build succeeded.

@thaJeztah
Copy link
Copy Markdown
Member Author

Rebased.

Yes, I think this should be ok to review; at least it changes the code to match the actual situation (and cgroupsv2.ErrCgroupDeleted has been removed in containerd/cgroups master/main)

We'd still need to look at if it actually should return a specific error (and thus, if the code that's removed here should be performed in this situation); that requires a follow-up in any case (with changes in containerd/cgroups)

Perhaps @AkihiroSuda could provide input on that (he's more familiar with the cgroups v2)

@thaJeztah
Copy link
Copy Markdown
Member Author

This was included in 18d483b, so no longer needed

@thaJeztah thaJeztah closed this Mar 23, 2022
@thaJeztah thaJeztah deleted the cgroupv2_deleted_detection branch March 23, 2022 13:46
kzys pushed a commit to kzys/containerd that referenced this pull request May 19, 2022
This commit brings the resource leak fix below.
containerd/cgroups#212

- The upgrade PR in main was containerd#6498. I didn't cherry-pick the commit due
  to conflicts.
- ErrCgroupDeleted check is removed since LoadManager won't return
  the error (see containerd#5739) and the variable was removed in 1.0.3.

Signed-off-by: Kazuyoshi Kato <[email protected]>
kzys pushed a commit to kzys/containerd that referenced this pull request May 19, 2022
This commit brings the resource leak fix below.
containerd/cgroups#212

- The original upgrade PR was containerd#6498. I didn't cherry-pick the commit due
  to conflicts.
- ErrCgroupDeleted check is removed since LoadManager won't return
  the error (see containerd#5739) and the variable was removed in 1.0.3.

Signed-off-by: Kazuyoshi Kato <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants