Merge pull request #13856 from HarshalPatel1972/fix-oom-leak

internal/oom: Fix memory leak by removing watcher from map on Stop
This commit is contained in:
Maksym Pavlenko
2026-07-28 17:11:20 +00:00
committed by GitHub
2 changed files with 35 additions and 2 deletions

View File

@@ -80,10 +80,11 @@ func (ows *oomWatchers) Add(cid string, pid int, fn EventFunc) (retErr error) {
func (ows *oomWatchers) Stop(cid string) error {
ows.mu.Lock()
w, exist := ows.watchers[cid]
w := ows.watchers[cid]
delete(ows.watchers, cid)
ows.mu.Unlock()
if !exist {
if w == nil {
return nil
}
return w.stop()

View File

@@ -99,3 +99,35 @@ func skipIfBinaryUnavailable(t *testing.T, binaryName string) {
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))
}