Skip to content

internal/oom: Fix memory leak by removing watcher from map on Stop - #13856

Merged
mxpv merged 1 commit into
containerd:mainfrom
HarshalPatel1972:fix-oom-leak
Jul 28, 2026
Merged

mxpv merged 1 commit into
containerd:mainfrom
HarshalPatel1972:fix-oom-leak

Conversation

@HarshalPatel1972

@HarshalPatel1972 HarshalPatel1972 commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

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. It also adds a unit test in watcher_test.go to ensure Stop successfully allows a watcher to be re-added under the same container ID.

Fix memory leak in OOM watcher map when stopping container monitoring

Comment thread internal/oom/watcher.go Outdated
Comment on lines +84 to +86
if exist {
delete(ows.watchers, cid)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit; no need to check if it exists before deleting.

Suggested change
if exist {
delete(ows.watchers, cid)
}
delete(ows.watchers, cid)

@fuweid fuweid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

THANKS. Left one minor suggestion

Comment thread internal/oom/watcher_test.go Outdated
require.NoError(t, err)

// A dummy process to get a valid pid and cgroup
cmd := exec.Command("sleep", "10")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
cmd := exec.Command("sleep", "10")
cmd := exec.Command("sleep", "10000")

@fuweid fuweid added the cherry-pick/2.3.x Change to be cherry picked to release/2.3 label Jul 27, 2026
@HarshalPatel1972

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews. I've applied both of your suggestions.

@fuweid

fuweid commented Jul 27, 2026

Copy link
Copy Markdown
Member

@HarshalPatel1972 could you please squash commits into one? thanks

@thaJeztah

Copy link
Copy Markdown
Member

@fuweid could use your thoughts on #13853 (comment) and the comment before it as well; not sure what's best

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 <[email protected]>
@HarshalPatel1972

Copy link
Copy Markdown
Contributor Author

@fuweid Done. Commits are squashed.

@fuweid

fuweid commented Jul 28, 2026

Copy link
Copy Markdown
Member

could use your thoughts on #13853 (comment) and the comment before it as well; not sure what's best

@thaJeztah well, I would like to remove it and close it. Currently, Stop() is to close FD and wait for exit.
It's not complicated. If we want to make it as blocker, we should do it after fix this issue

@github-project-automation github-project-automation Bot moved this from Needs Triage to Review In Progress in Pull Request Review Jul 28, 2026
@mxpv
mxpv added this pull request to the merge queue Jul 28, 2026
Merged via the queue into containerd:main with commit 1aabbdc Jul 28, 2026
88 of 90 checks passed
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Pull Request Review Jul 28, 2026
@fuweid

fuweid commented Jul 28, 2026

Copy link
Copy Markdown
Member

/cherry-pick release/2.3

@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@fuweid: new pull request created: #13870

Details

In response to this:

/cherry-pick release/2.3

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@fuweid fuweid added cherry-picked/2.3.x PR commits are cherry picked into release/2.3 branch and removed cherry-pick/2.3.x Change to be cherry picked to release/2.3 labels Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/runtime Runtime cherry-picked/2.3.x PR commits are cherry picked into release/2.3 branch kind/bug size/M

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

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

6 participants