Skip to content

plugins/services/tasks: add timeout to per-task Stats() call in getTasksMetrics - #13877

Open
Ayush-Rathor wants to merge 2 commits into
containerd:mainfrom
Ayush-Rathor:fix/task-metrics-stats-timeout
Open

Ayush-Rathor wants to merge 2 commits into
containerd:mainfrom
Ayush-Rathor:fix/task-metrics-stats-timeout

Conversation

@Ayush-Rathor

Copy link
Copy Markdown

What this PR fixes

Fixes/complements #13848 (companion to #13852). PR #13852 bounds the unbounded shim.delete() call that can hang containerd's startup path (loadShims) forever if a leftover shim never replies. This PR bounds a second, structurally identical unbounded call that we found while investigating why containerd was also observed hanging on the graceful shutdown path in the same incident described in #13848.

Root cause

getTasksMetrics (plugins/services/tasks/local.go) is the implementation behind the Tasks service's Metrics RPC. It iterates every task on the node and calls tk.Stats(ctx) for each one, sequentially, using whatever context the caller passed in — with no timeout applied at this layer:

On the CRI side, internal/cri/server/stats_collector.go's StatsCollector calls this RPC once a second from a background goroutine (collectLoop), using a bare context.Background()-derived context.

Put together: if even one task's shim hangs on Stats() (the same shim-unresponsive condition described in #13848 — sandbox already exited, shim doesn't answer a specific request), the entire per-second metrics-collection loop wedges on that one task indefinitely. Every other task silently stops getting metrics collected too, since the loop is sequential and never reaches them. And because StatsCollector.Stop() waits for that same wedged goroutine to exit, containerd's graceful shutdown (criService.Close() → c.statsCollector.Stop()) hangs for as long as the shim stays unresponsive — in practice, until systemd's stop timeout gives up and sends SIGKILL. This matches the "Stop-side hang" behavior observed in #13848 before the node hit the (separately-fixed) startup hang on its next boot.

We also note getTasksMetrics's "collecting metrics for %s" error log line matches log lines we independently captured from an affected production node, which is what led us to this function.

The fix

Wrap the per-task Stats() call with a bounded timeout, using the timeout key containerd's own cgroups-based metrics collector (core/metrics/cgroups/v1 and v2) already uses for exactly this same kind of call (ShimStatsRequestTimeout, io.containerd.timeout.metrics.shimstats, 2s default) — so this reuses an existing, already-tunable timeout rather than introducing a new config key, consistent with the approach in #13852.

sctx, cancel := timeout.WithContext(ctx, cmetrics.ShimStatsRequestTimeout)
stats, err := tk.Stats(sctx)
cancel()

With this change, one wedged shim can no longer block metrics collection for every other task, and can no longer block StatsCollector.Stop() indefinitely.

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.

🟡 Not ready to approve

The new lines introduce inconsistent indentation (not gofmt-aligned), which is likely to fail style/CI checks and should be corrected before merging.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

This PR prevents containerd’s Tasks service Metrics RPC from hanging indefinitely on a single unresponsive shim by applying an existing, configurable timeout to each per-task Stats() call during metrics collection.

Changes:

  • Wrap each tk.Stats() call in getTasksMetrics with timeout.WithContext(..., cmetrics.ShimStatsRequestTimeout).
  • Reuse the existing shim stats timeout key (io.containerd.timeout.metrics.shimstats, default 2s) rather than introducing a new timeout setting.
File summaries
File Description
plugins/services/tasks/local.go Adds a per-task timeout around Stats() to avoid wedging metrics collection/shutdown on an unresponsive shim.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Comment thread plugins/services/tasks/local.go Resolved
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Signed-off-by: Ayush Rathor <[email protected]>
Copilot AI review requested due to automatic review settings July 31, 2026 07:30

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.

🟢 Ready to approve

The change is small, consistent with existing timeout mechanisms, and directly addresses an identified indefinite-blocking failure mode without introducing API or behavior ambiguity beyond the intended timeout.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

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

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

@Ayush-Rathor

Copy link
Copy Markdown
Author

@austinvazquez Anything on this?

This branch has not been deployed

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

Labels

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

2 participants