Skip to content

internal/oom: Stop never removes the watcher from the map, leaking watchers and burning container IDs #13853

Closed

Description

@locker95

Description

internal/oom's oomWatchers.Stop stops a container's watcher but never removes it from oomWatchers.watchers:

func (ows *oomWatchers) Stop(cid string) error {
	ows.mu.Lock()
	w, exist := ows.watchers[cid]
	ows.mu.Unlock()

	if !exist {
		return nil
	}
	return w.stop()
}

Add is the only writer of that map and nothing ever deletes from it, so entries accumulate for the lifetime of the shim process. Two consequences:

1. Stopped watchers are leaked. Every container that exits leaves a stopped *watcher behind, holding a cid, a cgroupPath, an *os.File and a channel. stop() does close the event FD, so this is a memory leak rather than an FD leak, but it is unbounded in the number of containers the shim has ever run.

2. A container ID can never be watched twice. Add rejects an ID that is already in the map with errdefs.ErrAlreadyExists. Once a container ID has been stopped it can never be added again in the same shim, so a container re-created under that ID silently runs with no OOM monitoring — the Create path only logs the failure:

https://github.com/containerd/containerd/blob/main/cmd/containerd-shim-runc-v2/task/service.go#L274-L276

Both matter because a shim is grouped per sandbox (manager_linux.go groups by io.kubernetes.cri.sandbox-id), so one shim process hosts every container of a pod, across restarts, for the pod's whole lifetime.

The only production caller is cmd/containerd-shim-runc-v2/task/service.go: Add in the create path, Stop in handleProcessExit.

Steps to reproduce

Directly observable as a unit test in internal/oom:

watchers := New()
require.NoError(t, watchers.Add(cid, pid, fn))
require.NoError(t, watchers.Stop(cid))
require.NoError(t, watchers.Add(cid, pid, fn)) // fails: already exists

Expected

Stop removes the entry, releasing the watcher and freeing the container ID to be watched again.

Version

main. Affects internal/oom since the package was introduced.

Note

Separate from #13851, which bounds the wait inside watcher.stop() for #13814. The two touch the same function but are independent problems.

Activity

  1. gautamrizwani commented on Jul 26, 2026

    @gautamrizwani

    While looking into the fix, I realized it's not as simple as it looks. If we remove the entry before w.stop(), a concurrent Add can sneak in while the old goroutine is still shutting down - which could briefly run two watchers for the same container. If we remove it after w.stop() returns, it's safer but a ~concurrent Add for the same container ID will get ErrAlreadyExists until the goroutine fully exits, and cleanup is also delayed if w.stop() takes longer than expected.

    I think this needs a call from the maintainers on which tradeoff is acceptable. Closing the PR for now, happy to reopen once there's some clarity on the approach.

  2. thaJeztah commented on Jul 26, 2026

    @thaJeztah
    Member

    Heh; I saw @HarshalPatel1972 just opened #13856 and I was about to comment "should it be removed before Close returns successfully?" but I didn't, and not realizing the other related ticket.

    So ... yes, possibly more logic is needed 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions