mirror of
https://github.com/containerd/containerd.git
synced 2026-11-06 05:17:02 +00:00
The oomWatchers.Stop method stopped the watcher but never removed it from the watchers map. Because Add is the only writer and nothing ever deletes from the map, *watcher entries accumulated for the lifetime of the shim process. This resulted in an unbounded memory leak. Furthermore, this prevented the same container ID from ever being watched again if it was re-created, silently dropping OOM monitoring. This commit modifies Stop to delete(ows.watchers, cid), ensuring the watcher is released and the container ID is freed. Signed-off-by: Harshal Patel <106813066+HarshalPatel1972@users.noreply.github.com>
134 lines
3.4 KiB
Go
134 lines
3.4 KiB
Go
//go:build linux
|
|
|
|
/*
|
|
Copyright The containerd Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package oom
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/containerd/cgroups/v3"
|
|
cgroupsv2 "github.com/containerd/cgroups/v3/cgroup2"
|
|
"github.com/containerd/containerd/v2/pkg/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var defaultCgroup2Path = "/sys/fs/cgroup"
|
|
|
|
func TestWatcher(t *testing.T) {
|
|
testutil.RequiresRoot(t)
|
|
|
|
skipIfCgroupUnavailable(t)
|
|
skipIfBinaryUnavailable(t, "dd")
|
|
|
|
group := fmt.Sprintf("/%s", t.Name())
|
|
mgr, err := cgroupsv2.NewManager(defaultCgroup2Path, group, &cgroupsv2.Resources{})
|
|
require.NoError(t, err)
|
|
|
|
ddCmd := exec.Command("dd", "if=/dev/zero", "of=/dev/null bs=20M")
|
|
err = ddCmd.Start()
|
|
defer func() {
|
|
ddCmd.Wait()
|
|
}()
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, mgr.AddProc(uint64(ddCmd.Process.Pid)))
|
|
|
|
var oomKills atomic.Uint64
|
|
|
|
watchers := New()
|
|
containerID := "dd-test"
|
|
require.NoError(t, watchers.Add(containerID, ddCmd.Process.Pid, func(cid string) {
|
|
assert.Equal(t, containerID, cid)
|
|
oomKills.Add(1)
|
|
}))
|
|
|
|
require.NoError(t, mgr.Update(&cgroupsv2.Resources{
|
|
Memory: &cgroupsv2.Memory{
|
|
Max: toPtr(int64(15 * 1024 * 1024)),
|
|
Swap: toPtr(int64(15 * 1024 * 1024)),
|
|
},
|
|
}))
|
|
defer func() {
|
|
watchers.Stop(containerID)
|
|
}()
|
|
|
|
err = ddCmd.Wait()
|
|
require.ErrorContains(t, err, "signal: killed")
|
|
|
|
require.Eventuallyf(t, func() bool {
|
|
return oomKills.Load() == uint64(1)
|
|
}, 30*time.Second, time.Second, "should receive oom event (%v)", oomKills.Load())
|
|
|
|
require.NoError(t, watchers.Stop(containerID))
|
|
}
|
|
|
|
func skipIfCgroupUnavailable(t *testing.T) {
|
|
if mode := cgroups.Mode(); mode != cgroups.Unified {
|
|
t.Skipf("skip because it's not cgroup v2 (mode: %v)", mode)
|
|
}
|
|
}
|
|
|
|
func skipIfBinaryUnavailable(t *testing.T, binaryName string) {
|
|
_, err := exec.LookPath(binaryName)
|
|
if err != nil {
|
|
t.Skipf("skip because %s is not available (err: %v)", binaryName, err)
|
|
}
|
|
}
|
|
|
|
func toPtr[T comparable](v T) *T {
|
|
return &v
|
|
}
|
|
|
|
func TestWatcherStopRemovesFromMap(t *testing.T) {
|
|
testutil.RequiresRoot(t)
|
|
|
|
skipIfCgroupUnavailable(t)
|
|
|
|
group := fmt.Sprintf("/%s", t.Name())
|
|
mgr, err := cgroupsv2.NewManager(defaultCgroup2Path, group, &cgroupsv2.Resources{})
|
|
require.NoError(t, err)
|
|
|
|
// A dummy process to get a valid pid and cgroup
|
|
cmd := exec.Command("sleep", "10000")
|
|
require.NoError(t, cmd.Start())
|
|
defer func() {
|
|
cmd.Process.Kill()
|
|
cmd.Wait()
|
|
}()
|
|
|
|
require.NoError(t, mgr.AddProc(uint64(cmd.Process.Pid)))
|
|
|
|
watchers := New()
|
|
containerID := "test-stop-removes"
|
|
|
|
fn := func(cid string) {}
|
|
|
|
require.NoError(t, watchers.Add(containerID, cmd.Process.Pid, fn))
|
|
require.NoError(t, watchers.Stop(containerID))
|
|
|
|
// Should be able to add again with the same containerID
|
|
require.NoError(t, watchers.Add(containerID, cmd.Process.Pid, fn))
|
|
require.NoError(t, watchers.Stop(containerID))
|
|
}
|