Skip to content

metadata: bound snapshotter Remove during garbage collection - #13799

Merged
mikebrow merged 1 commit into
containerd:mainfrom
NahumLitvin:bound-snapshot-gc-remove
Aug 24, 2026
Merged

mikebrow merged 1 commit into
containerd:mainfrom
NahumLitvin:bound-snapshot-gc-remove

Conversation

@NahumLitvin

@NahumLitvin NahumLitvin commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #13798

Problem

During snapshot GC, cleanupSnapshotter calls the snapshotter's Remove while holding the metadata store write lock, with no deadline (it even strips the caller's cancellation). If the snapshotter never answers, the lock is held forever. Every snapshot operation on the node queues behind it, including CRI RunPodSandbox, so no new container can start. Nothing on the readiness path takes this lock, so the node keeps reporting Ready the whole time.

flowchart TD
    K[kubelet] -->|Ready probe| S["CRI Status()"]
    K -->|RunPodSandbox| P[snapshot Prepare]
    S --> OK["returns OK - never takes the lock"]
    P --> L{{metadata store write lock}}
    GC[snapshot GC] -->|holds| L
    GC -->|"Remove() - no deadline"| SOCI[soci snapshotter<br/>out of fds, never answers]
    L -.->|blocked behind GC| P
Loading

We hit this in production: our soci snapshotter ran out of file descriptors and stopped answering, the GC's Remove parked in waitOnHeader, and the node could not start a single pod for 4.5 hours while showing Ready. Goroutine dumps are in the issue. The fd leak was a soci bug (fixed upstream in awslabs/soci-snapshotter#2043), but any proxy snapshotter that stalls reproduces this: one stuck RPC should not brick the node.

Fix

Bound each GC-path Remove with a pkg/timeout key (io.containerd.timeout.snapshot.remove, default 30m), the same pattern as shim cleanup and bolt open timeouts.

On the first timed-out Remove the rest of the pass is abandoned, so a wedged snapshotter costs one timeout per GC cycle instead of one per orphan. Skipped snapshots stay orphaned and are retried next pass. The deferred Cleanup call is skipped in that case too, since it is an unbounded RPC to the same snapshotter that just failed to answer.

Not in this PR

Walk runs under the same lock and is also unbounded. Left out to keep this change small; can follow up.

Copilot AI lite review requested due to automatic review settings July 15, 2026 16:37
@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review Jul 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a bounded timeout for snapshot garbage-collection removals in core/metadata to prevent the snapshotter write lock from being held indefinitely when an underlying snapshotter Remove hangs (e.g., a wedged proxy snapshotter RPC).

Changes:

  • Introduces a pkg/timeout key (io.containerd.timeout.metadata.snapshot.remove, default 5m) and applies it to Snapshotter.Remove calls made during metadata snapshot GC.
  • Abandons the remainder of a GC pass after the first timed-out removal to avoid paying the timeout repeatedly for many orphans in a single cycle.
  • Adds tests covering a hanging Remove and the existing FailedPrecondition tolerance path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
core/metadata/snapshot.go Adds a GC-path Remove timeout and aborts the GC pass after the first timeout to prevent lock hold starvation.
core/metadata/snapshot_test.go Adds regression tests for hanging Remove behavior and FailedPrecondition tolerance during GC.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread core/metadata/snapshot_test.go Resolved
Comment thread core/metadata/snapshot.go Resolved
Copilot AI review requested due to automatic review settings July 15, 2026 16:46
@NahumLitvin
NahumLitvin force-pushed the bound-snapshot-gc-remove branch from 5e8e2da to 3b4ebc0 Compare July 15, 2026 16:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread core/metadata/snapshot.go Resolved
Comment thread core/metadata/snapshot_test.go Resolved
Copilot AI review requested due to automatic review settings July 15, 2026 16:54
@NahumLitvin
NahumLitvin force-pushed the bound-snapshot-gc-remove branch from 3b4ebc0 to fb05a0e Compare July 15, 2026 16:54
@NahumLitvin

Copy link
Copy Markdown
Contributor Author

Addressed the Copilot comments in fb05a0e:

  • Test now captures the previous timeout via timeout.Get and restores it with t.Cleanup instead of hardcoding the default.
  • Dropped the redundant nil check before errors.Is(removeCtx.Err(), ...).
  • Fixed the stale timeout key name in the PR description (io.containerd.timeout.snapshot.remove is the actual key).

Kept cancel() non-deferred deliberately: pruneBranch is recursive, and deferring would keep every ancestor frame's timer alive until the whole branch unwinds; the explicit cancel releases each timer as soon as its Remove returns.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread core/metadata/snapshot_test.go Resolved
Comment thread core/metadata/snapshot.go Resolved
Copilot AI review requested due to automatic review settings July 15, 2026 17:01
@NahumLitvin
NahumLitvin force-pushed the bound-snapshot-gc-remove branch from fb05a0e to a0b3d39 Compare July 15, 2026 17:01
@NahumLitvin

Copy link
Copy Markdown
Contributor Author

Addressed the second review round in a0b3d39: reworded the test's Cleanup comment (the stub returns immediately; only real snapshotters hang), and documented io.containerd.timeout.snapshot.remove in the man page's timeouts example. The key-name mismatch comment referred to the PR description, which was already fixed to match the code.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

core/metadata/snapshot.go:1019

  • removeTimedOut is only handled when Remove returns a non-nil error. If a snapshotter ignores the context deadline and eventually returns nil, removeCtx.Err() will still be DeadlineExceeded and this code will treat the operation as a success (and continue pruning / run Cleanup), even though it exceeded the configured bound. Consider treating a reached deadline as a timeout regardless of Remove's returned error.
		removeCtx, cancel := timeout.WithContext(ctx, snapshotRemoveTimeout)
		err := s.Snapshotter.Remove(removeCtx, node.info.Name)
		removeTimedOut := errors.Is(removeCtx.Err(), context.DeadlineExceeded)
		cancel()
		if err != nil {

@NahumLitvin
NahumLitvin requested review from fuweid and mikebrow August 19, 2026 08:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

core/metadata/snapshot.go:57

  • The PR description states the GC-bounded Remove uses timeout key io.containerd.timeout.snapshot.remove with a default of 5m, but the implementation introduces io.containerd.timeout.gc.snapshotter.remove with a 30m default (and the manpage matches the code). Please reconcile this discrepancy (either update the PR description / docs to match the intended key+default, or adjust the code constants if the intended key/default are different).
// gcSnapshotterRemoveTimeoutKey bounds each Snapshotter.Remove during garbage
// collection, which holds the snapshotter write lock: an unresponsive
// snapshotter (e.g. a hung proxy plugin RPC) would otherwise block all
// snapshot operations forever. On timeout the rest of the pass is abandoned;
// remaining snapshots stay orphaned and are retried on the next GC pass.
const (
	gcSnapshotterRemoveTimeoutKey     = "io.containerd.timeout.gc.snapshotter.remove"
	defaultGCSnapshotterRemoveTimeout = 30 * time.Minute
)

func init() {
	timeout.Set(gcSnapshotterRemoveTimeoutKey, defaultGCSnapshotterRemoveTimeout)
}

The snapshot garbage collector holds the snapshotter write lock while
calling the underlying Snapshotter.Remove. cleanupSnapshotter also
detaches cancellation (context.WithoutCancel), so a Remove that never
returns - e.g. a proxy snapshotter whose RPC connection has wedged -
holds the write lock forever. Every subsequent snapshot operation on
the node then blocks behind the lock, including CRI RunPodSandbox,
turning the node into a permanent scheduling black hole while it still
reports Ready.

Observed in production with a proxy snapshotter (soci) that stopped
answering after a file-descriptor exhaustion storm: the GC goroutine
sat in WaitGroup.Wait for 4.5 hours with the Remove RPC parked in
waitOnHeader, and 17 CRI operations queued behind the metadata lock.

Bound each GC-path Remove with a timeout registered via pkg/timeout
(io.containerd.timeout.gc.snapshotter.remove, default 30m,
operator-tunable through config.toml like the other timeout keys). A
timed-out Remove returns DeadlineExceeded, which is not a
FailedPrecondition, so it propagates out of pruneBranch and ends the
pass: a globally wedged snapshotter costs one timeout per GC cycle
rather than one per orphaned snapshot, the deferred Cleanup is skipped
because the pass failed, and the snapshots left behind stay orphaned
for the next pass.

Signed-off-by: Nahum Litvin <[email protected]>
Copilot AI review requested due to automatic review settings August 20, 2026 13:56
@mikebrow
mikebrow force-pushed the bound-snapshot-gc-remove branch from b3740a1 to a9d5caf Compare August 20, 2026 13:56
@mikebrow mikebrow added this to the 2.4 milestone Aug 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

core/metadata/snapshot.go:57

  • The PR description states the timeout key is io.containerd.timeout.snapshot.remove with a default of 5m, but the implementation (and the config manpage) introduce io.containerd.timeout.gc.snapshotter.remove with a 30m default. Please align the PR description and/or the key+default so users aren’t misled about how to configure this behavior.
// gcSnapshotterRemoveTimeoutKey bounds each Snapshotter.Remove during garbage
// collection, which holds the snapshotter write lock: an unresponsive
// snapshotter (e.g. a hung proxy plugin RPC) would otherwise block all
// snapshot operations forever. On timeout the rest of the pass is abandoned;
// remaining snapshots stay orphaned and are retried on the next GC pass.
const (
	gcSnapshotterRemoveTimeoutKey     = "io.containerd.timeout.gc.snapshotter.remove"
	defaultGCSnapshotterRemoveTimeout = 30 * time.Minute
)

func init() {
	timeout.Set(gcSnapshotterRemoveTimeoutKey, defaultGCSnapshotterRemoveTimeout)
}

@mikebrow
mikebrow added this pull request to the merge queue Aug 24, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 24, 2026
@mikebrow
mikebrow added this pull request to the merge queue Aug 24, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 24, 2026
@mikebrow
mikebrow added this pull request to the merge queue Aug 24, 2026
chansuke added a commit to chansuke/containerd that referenced this pull request Aug 24, 2026
The content garbage collector holds the content store write lock while
calling the backend Store.Delete and Store.Abort. cleanupContent also
detaches cancellation (context.WithoutCancel), so a Delete that never
returns - e.g. a proxy content store whose RPC connection has wedged -
holds the write lock forever. Every subsequent content operation then
blocks behind the lock, including pull ingest, until the plugin
recovers on its own. This is the content-side twin of the snapshotter
Remove hang bounded by containerd#13799.

Bound each GC-path Delete and Abort with timeouts registered via
pkg/timeout (io.containerd.timeout.gc.content.delete and
io.containerd.timeout.gc.content.abort, default 30m, operator-tunable
through config.toml like the other timeout keys). A timed-out call
returns DeadlineExceeded, which propagates out of garbageCollect and
ends the pass: a globally wedged content store costs one timeout per
GC cycle rather than one per orphaned blob, and the blobs and ingests
left behind stay orphaned for the next pass.

Signed-off-by: chansuke <[email protected]>
chansuke added a commit to chansuke/containerd that referenced this pull request Aug 24, 2026
The content garbage collector holds the content store write lock while
calling the backend Store.Delete and Store.Abort. cleanupContent also
detaches cancellation (context.WithoutCancel), so a Delete that never
returns - e.g. a proxy content store whose RPC connection has wedged -
holds the write lock forever. Every subsequent content operation then
blocks behind the lock, including pull ingest, until the plugin
recovers on its own. This is the content-side twin of the snapshotter
Remove hang bounded by containerd#13799.

Bound each GC-path Delete and Abort with timeouts registered via
pkg/timeout (io.containerd.timeout.gc.content.delete and
io.containerd.timeout.gc.content.abort, default 30m, operator-tunable
through config.toml like the other timeout keys). A timed-out call
returns DeadlineExceeded, which propagates out of garbageCollect and
ends the pass: a globally wedged content store costs one timeout per
GC cycle rather than one per orphaned blob, and the blobs and ingests
left behind stay orphaned for the next pass.

Signed-off-by: chansuke <[email protected]>
Merged via the queue into containerd:main with commit 1adcdb1 Aug 24, 2026
88 of 90 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Snapshot GC holding write lock across deadline-less proxy Remove permanently wedges all snapshot operations

4 participants