Skip to content

metadata: bound each snapshot GC pass with a single timeout - #14026

Closed
NahumLitvin wants to merge 1 commit into
containerd:mainfrom
NahumLitvin:bound-snapshot-gc-walk
Closed

NahumLitvin wants to merge 1 commit into
containerd:mainfrom
NahumLitvin:bound-snapshot-gc-walk

Conversation

@NahumLitvin

@NahumLitvin NahumLitvin commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Snapshot GC holds the snapshotter write lock across every snapshotter call it makes: Walk to build the tree, each Remove while pruning, and the final Cleanup. Only Remove was bounded (#13799); a proxy snapshotter that wedges in Walk or Cleanup still holds the lock forever and no new container can start. Walk runs first in the pass, so the bounded Remove path is never even reached. Called out as a known gap in #13799.

Fix

One timeout for the whole GC pass, per review feedback from @mikebrow (move the context up to garbageCollect) and @fuweid (a single timeout instead of per-call keys): io.containerd.timeout.gc.snapshotter (default 30m) created in garbageCollect, covering Walk, every Remove, and Cleanup under one deadline. The per-call io.containerd.timeout.gc.snapshotter.remove key from #13799 is removed; it never shipped in a release, so no deprecation is needed.

On timeout the pass fails before any further pruning, so a partial walk result is never used to remove snapshots; the next pass retries.

The synctest regression tests assert a timed-out Walk triggers zero Remove and zero Cleanup calls, and that a timed-out Remove abandons the pass, deadlocking in under a second if the bound is removed.

Refs #13798.

Copilot AI lite review requested due to automatic review settings August 24, 2026 17:41
@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review Aug 24, 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 prevents snapshot garbage collection from indefinitely holding the metadata snapshotter write lock when a proxy snapshotter stalls during Snapshotter.Walk, by adding a GC-scoped timeout for the walk phase (mirroring the earlier bounded Remove work in #13799).

Changes:

  • Add a new timeout key (io.containerd.timeout.gc.snapshotter.walk, default 30m) and apply it to the GC-path snapshot tree walk.
  • Ensure a timed-out walk fails the GC pass before any pruning and skips the deferred unbounded Cleanup.
  • Add a synctest regression test verifying a hung Walk triggers zero Remove and zero Cleanup, and document the new timeout key.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
docs/man/containerd-config.toml.5.md Documents the new GC snapshotter walk timeout key in the timeouts section.
core/metadata/snapshot.go Introduces and wires a GC-specific Walk timeout via pkg/timeout.WithContext in walkTree.
core/metadata/snapshot_test.go Adds a synctest regression test covering hung Walk behavior (no prune/cleanup on timeout).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +574 to +577
_, err := msn.garbageCollect(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected the bounded Walk to fail with DeadlineExceeded, got %v", err)
}
Copilot AI review requested due to automatic review settings August 24, 2026 17:49
@NahumLitvin
NahumLitvin force-pushed the bound-snapshot-gc-walk branch from 5a09d09 to 69b6f52 Compare August 24, 2026 17:49

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 review requested due to automatic review settings August 24, 2026 17:52
@NahumLitvin
NahumLitvin force-pushed the bound-snapshot-gc-walk branch from 69b6f52 to 891cbfe Compare August 24, 2026 17: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.

@mikebrow mikebrow 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.

alternative for consideration:

sounds like we wanted both the walkTree and each recursive pruneBranch to be included in one timeout..

let's create the ctx with timeout and defer the cancel in the higher level garbageCollect func before calling both the walkTree and the pruneBranch.. methods thus having one timeout for all storage bound funcs in the gc while under the lock.

@github-project-automation github-project-automation Bot moved this from Needs Triage to Needs Update in Pull Request Review Aug 24, 2026
@mikebrow
mikebrow requested a review from fuweid August 24, 2026 21:04
@mikebrow

Copy link
Copy Markdown
Member

see slack thread.. Derek postulates resolving why the snapshotter (via proxy layer?) isn't quickly reporting the snapshotter is offline.. vs using timeouts closer to the client(s) and gc scheduler

Copilot AI review requested due to automatic review settings August 25, 2026 16:15
@NahumLitvin
NahumLitvin force-pushed the bound-snapshot-gc-walk branch from 891cbfe to 08796a7 Compare August 25, 2026 16:15
@kubernetes-prow kubernetes-prow Bot added size/L and removed size/M labels Aug 25, 2026
@NahumLitvin NahumLitvin changed the title metadata: bound snapshotter Walk during garbage collection metadata: bound each snapshot GC pass with a single timeout Aug 25, 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 1 comment.

Comment thread core/metadata/snapshot.go Outdated
Comment on lines +45 to +50
// gcSnapshotterTimeoutKey bounds one full snapshot garbage collection pass,
// which holds the snapshotter write lock across every snapshotter call it
// makes (Walk, each Remove, and the final Cleanup): 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.
// snapshot operations forever. On timeout the pass is abandoned; remaining
// snapshots stay orphaned and are retried on the next GC pass.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

reworded, the lock is released before Cleanup runs, the comment now says which parts run under the lock and which are only under the shared deadline.

@NahumLitvin

Copy link
Copy Markdown
Contributor Author

done, moved the context up to garbageCollect as you suggested, and per @fuweid's comment it's now one io.containerd.timeout.gc.snapshotter key for the whole pass, the remove key from #13799 is gone (never released, so clean removal). the deferred Cleanup runs under the same deadline now too, it was still unbounded before.

@NahumLitvin
NahumLitvin requested a review from mikebrow August 25, 2026 16:34
Comment thread core/metadata/snapshot.go
// garbageCollect removes all snapshots that are no longer used.
func (s *snapshotter) garbageCollect(ctx context.Context) (d time.Duration, err error) {
s.l.Lock()
gcCtx, cancel := timeout.WithContext(ctx, gcSnapshotterTimeoutKey)

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.

thought: maybe this new context and defer should happen before the lock is requested

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done, the context now starts before the lock, so time spent waiting on a lock held by a stuck pass counts against the same deadline.

Copilot AI review requested due to automatic review settings August 26, 2026 06:45
@NahumLitvin
NahumLitvin force-pushed the bound-snapshot-gc-walk branch from 08796a7 to 6c675c0 Compare August 26, 2026 06:45
@fuweid

fuweid commented Sep 2, 2026

Copy link
Copy Markdown
Member

Hi @NahumLitvin and @mikebrow , after revisited the GC component and the discussion in slack, I am thinking that we should move the timeout into proxy client layer, like default_timeout setting in proxy section or proxy.snapshotter.default_timeout timeout section. The proxy snapshotter is external process and it could happen deadlock anytime. I believe GC is just one of victims. So, we can put default_timeout for the proxy snapshotter client and set that into context if there is no any deadline from original context. All the components or functions can be guarded by this timeout. Does it make senses to you?

@NahumLitvin

Copy link
Copy Markdown
Contributor Author

makes sense to me, and it is the better home. a proxy snapshotter can hang on any of its calls, so a timeout in the client covers all 10 methods at once instead of one component at a time.

on sizing: default_timeout on ProxyPlugin, kept on proxySnapshotter, plus a small helper that sets the deadline when the incoming context has none, used in each method. NewSnapshotter is exported and client.go uses it too, so il pass it as a variadic opt instead of changing the signature.

id do it as a separate PR, not a rewrite of this one: different package, and this one is already approved by mikebrow. one open question for the GC path though. the client default only applies when there is no deadline yet, and with this PR merged the GC pass always sets its own 30m. so: merge this one and let the client default cover everything else, or close it and go straight to the client layer?

@NahumLitvin

Copy link
Copy Markdown
Contributor Author

also #14010 is relevant here. it lists the daemon calls into a proxy plugin that run under a shared lock. snapshot GC Walk (what this PR bounds) and content GC Walk/Delete have no limit at all, the rest stop when their request deadline does.

so a default_timeout in the client would cover that whole list at once, instead of a PR per component in content, metadata and the bolt paths. that route is what produced #13799 and this PR.

Snapshot GC holds the snapshotter write lock across every snapshotter
call it makes: Walk to build the tree, each Remove while pruning, and
the final Cleanup. Only Remove was bounded (io.containerd.timeout.gc.
snapshotter.remove); a proxy snapshotter that wedges in Walk or Cleanup
still holds the lock forever and no new container can start.

Replace the per-call Remove timeout with one io.containerd.timeout.gc.
snapshotter key (default 30m) applied to the whole pass in
garbageCollect, covering Walk, Remove, and Cleanup under one deadline.
The removed key never shipped in a release, so no deprecation is
needed.

On timeout the pass is abandoned before any further pruning; orphans
are retried on the next pass.

Signed-off-by: Nahum Litvin <[email protected]>

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.

🟢 Approval recommended

The change is narrowly scoped to snapshot GC timeout semantics, removes the unbounded Walk/Cleanup gap described in the issue, and is backed by targeted synctest regressions.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 2, 2026 14:10

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.

🟢 Approval recommended

The timeout is applied consistently across the full GC pass, the obsolete key is fully removed, and targeted synctest regressions cover the key failure modes (hanging Walk/Remove).

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@NahumLitvin

Copy link
Copy Markdown
Contributor Author

before i write it: #14016 and #14025 cover the content store side of the same list, so a client default probably replaces all three of these, not only this one. say the word and il close them.

design question too. content Writer lives past the call that made it, and Walk streams, so a deadline on each call would cut a long ingest. leave Writer out of it, or give it a key of its own? tell me which and il send it for both proxy clients, with an opt on the constructor so client.go keeps working.

@mikebrow

mikebrow commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

Hi @NahumLitvin and @mikebrow , after revisited the GC component and the discussion in slack, I am thinking that we should move the timeout into proxy client layer, like default_timeout setting in proxy section or proxy.snapshotter.default_timeout timeout section. The proxy snapshotter is external process and it could happen deadlock anytime. I believe GC is just one of victims. So, we can put default_timeout for the proxy snapshotter client and set that into context if there is no any deadline from original context. All the components or functions can be guarded by this timeout. Does it make senses to you?

Two things to consider:

  1. we should not be holding the lock passed some reasonable amount of time for the looping background gc thread. Would it make sense to change to the number of calls sent through the proxy while the lock is held?
    1a) release the lock, re-aquire and re-validate after each recursive branch prune request
    1b) keep the lock but remove the recursion from the gc thread, presuming that is possible

  2. Only having timeouts in the proxy may not catch the situation where the gc ctx without a timeout is taking to long to complete while it holds lock, not in the special case where none of the individual calls through the proxy actually timeout but the total duration is not acceptable..

@fuweid

fuweid commented Sep 2, 2026

Copy link
Copy Markdown
Member

we should not be holding the lock passed some reasonable amount of time for the looping background gc thread. Would it make sense to change to the number of calls sent through the proxy while the lock is held?
1a) release the lock, re-aquire and re-validate after each recursive branch prune request
1b) keep the lock but remove the recursion from the gc thread, presuming that is possible

I understand the concern. Currently, each GC cycle has two phases.

  • containerd metadata GC first performs mark-and-sweep against the metadata DB while holding the DB-wide GC lock. It doesn't invoke any content/snapshotter backends. Ideally, it should only take around 100-300 milliseconds

  • Backend cleanup is then scheduled separately for each dirty snapshotter/content store. Importantly, the DB-wide lock is released before GC waits for those cleanup goroutines.

if len(m.dirtySS) > 0 {
var sl sync.Mutex
stats.SnapshotD = map[string]time.Duration{}
wg.Add(len(m.dirtySS))
for snapshotterName := range m.dirtySS {
log.G(ctx).WithField("snapshotter", snapshotterName).Debug("schedule snapshotter cleanup")
go func(snapshotterName string) {
st1 := time.Now()
m.cleanupSnapshotter(ctx, snapshotterName)
sl.Lock()
stats.SnapshotD[snapshotterName] = time.Since(st1)
sl.Unlock()
wg.Done()
}(snapshotterName)
}
m.dirtySS = map[string]struct{}{}
}
if m.dirtyCS {
wg.Add(1)
log.G(ctx).Debug("schedule content cleanup")
go func() {
ct1 := time.Now()
m.cleanupContent(ctx)
stats.ContentD = time.Since(ct1)
wg.Done()
}()
m.dirtyCS = false
}
c.finish(ctx, &wg)
stats.MetaD = time.Since(t1)
m.wlock.Unlock()
wg.Wait()

Snapshot cleanup does hold a snapshotter-level lock while calling Walk and Remove. Therefore, if one external proxy snapshotter is stuck, it can block subsequent mutating calls on that snapshotter, but it does not keep the DB-wide GC lock held and does not block mutations on other snapshotters.

This is why I proposed an optional default timeout at the proxy snapshotter client boundary. It ensures that a single stuck Walk, Remove, or other proxy RPC cannot hold the snapshotter-level lock indefinitely, not just in GC cycle.

@mikebrow

mikebrow commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

Snapshot cleanup does hold a snapshotter-level lock while calling Walk and Remove. Therefore, if one external proxy snapshotter is stuck, it can block subsequent mutating calls on that snapshotter, but it does not keep the DB-wide GC lock held and does not block mutations on other snapshotters.

This is why I proposed an optional default timeout at the proxy snapshotter client boundary. It ensures that a single stuck Walk, Remove, or other proxy RPC cannot hold the snapshotter-level lock indefinitely, not just in GC cycle.

ah.. I see your reasoning now. Thanks! SGTM!

@mikebrow
mikebrow dismissed their stale review September 2, 2026 22:46

see @fuwei suggestion

@fuweid

fuweid commented Sep 5, 2026

Copy link
Copy Markdown
Member

Since we are going to release 2.4, we need to address that or revert the timeout setting

@mikebrow

mikebrow commented Sep 5, 2026

Copy link
Copy Markdown
Member

Since we are going to release 2.4, we need to address that or revert the timeout setting

nod.. #14119

@kubernetes-prow

Copy link
Copy Markdown

PR needs rebase.

Details

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.

chansuke added a commit to chansuke/containerd that referenced this pull request Sep 8, 2026
The content garbage collector holds the content store write lock while
calling the backend Store.Walk, Store.Delete and Store.Abort.
cleanupContent also detaches cancellation (context.WithoutCancel), so a
backend call 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 snapshot GC pass bounded by containerd#14026.

Follow the design agreed there (a single timeout for the whole pass
instead of per-call keys): io.containerd.timeout.gc.content (default
30m, operator-tunable through config.toml like the other timeout keys)
is created at the top of garbageCollect and covers the Walk/Delete
sweep and the ingest Abort sweep under one deadline. On timeout the
pass fails, and the blobs and ingests left behind stay orphaned for
the next pass.

Signed-off-by: chansuke <[email protected]>
@NahumLitvin

Copy link
Copy Markdown
Contributor Author

Closing, superseded. Per @fuweid in #14026 the bound moves to the proxy client as an optional default_timeout, covering all 10 methods instead of one component at a time.

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.

4 participants