Skip to content

fix(runtime): apply load timeout to load shim - #13954

Merged
mxpv merged 1 commit into
containerd:mainfrom
austinvazquez:fix/bound-load-shim-to-prevent-stalled-startup
Aug 14, 2026
Merged

mxpv merged 1 commit into
containerd:mainfrom
austinvazquez:fix/bound-load-shim-to-prevent-stalled-startup

Conversation

@austinvazquez

@austinvazquez austinvazquez commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

Carries #13852

This change applies the io.containerd.timeout.shim.load budget to all of shim manager load shim and io.containerd.timeout.shim.cleanup to cleanup shim calls to prevent a previously leaked / wedged shim from stalling containerd startup.

Avoid containerd startup hangs when loading shims

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 aims to prevent containerd startup from stalling on unresponsive shims by applying a load timeout during shim bundle loading, and it refactors shim cleanup into a shared helper.

Changes:

  • Refactors failed/abandoned shim cleanup into a new cleanupShimTask helper and reuses it from both task creation failure and shim-load cleanup paths.
  • Wraps ShimManager.loadShim work with loadTimeout to bound bundle loading during plugin initialization.
  • Updates shouldCleanupShim test coverage to ensure a PID-lookup timeout doesn’t get treated as proof a shim is dead.

Reviewed changes

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

File Description
core/runtime/v2/task_manager.go Uses shared cleanupShimTask when task creation fails to ensure bounded cleanup.
core/runtime/v2/shim.go Adds cleanupShimTask helper implementing bounded delete/shutdown/close behavior.
core/runtime/v2/shim_load.go Applies loadTimeout in shim load path and uses cleanupShimTask for leaked shim cleanup.
core/runtime/v2/shim_load_test.go Adds a case ensuring PID lookup timeouts don’t trigger shim cleanup.

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

Comment on lines 174 to 181
// TODO: It seems we can only call loadShim here if it is a sandbox shim?
shim, err := loadShimTask(ctx, bundle, func() {
//
// A loaded shim outlives this call and its context drives log forwarding,
// so hand it one without the load deadline.
shim, err := loadShimTask(context.WithoutCancel(ctx), bundle, func() {
log.G(ctx).WithField("id", id).Info("shim disconnected")

cleanupAfterDeadShim(context.WithoutCancel(ctx), id, m.shims, m.events, binaryCall)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Passthrough context with existing load deadline. Maybe others can weigh in but load timeout should be applied to the entire load operation instead of having each external call apply it resulting in a multiple of the timeout.

Comment thread core/runtime/v2/shim_load.go Outdated
Comment on lines +134 to +137
// Bound the work done for this bundle. Shims are loaded during plugin
// initialization, so one that never answers would stall containerd startup.
ctx, cancel := timeout.WithContext(ctx, loadTimeout)
defer cancel()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Cleanup should use the cleanup budget.

@austinvazquez
austinvazquez force-pushed the fix/bound-load-shim-to-prevent-stalled-startup branch from 99e231d to fdcdb86 Compare August 13, 2026 00:42
Copilot AI review requested due to automatic review settings August 13, 2026 00:42

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 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (2)

core/runtime/v2/shim_load.go:209

  • Since loadShim now wraps ctx with loadTimeout (lines 139-140), errors from loadShimTask can be caused by ctx timing out/canceling. In that case cleanupAfterDeadShim(ctx, ...) (line 186) inherits the canceled context, so its own cleanupTimeout budget won’t help and the cleanup may be skipped.
		if err := cleanupShimTask(ctx, shim, false); err != nil {
			// Returning an error makes loadShims remove the bundle; a shim we
			// cannot reap would otherwise be reloaded on every start.
			return fmt.Errorf("failed to clean up leaked shim %q: %w", id, err)

core/runtime/v2/shim.go:196

  • The comment says a failed delete returns before shutting the shim down and closing its client, but shimTask.delete only returns early for non-NotFound/transport errors. For ErrNotFound (and closed transports) it continues and may still shutdown/delete/close, even though it returns a non-nil error.
// A failed delete returns before shutting the shim down and closing its client,
// so both are done here. It also leaves the bundle in place (only a successful
// delete removes it), so callers that own one must remove it on error. Nothing

Copilot AI review requested due to automatic review settings August 13, 2026 21:21
@austinvazquez
austinvazquez force-pushed the fix/bound-load-shim-to-prevent-stalled-startup branch from fdcdb86 to 8d6258f Compare August 13, 2026 21:21

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 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (2)

core/runtime/v2/shim_load.go:139

  • loadShim now wraps ctx with loadTimeout, which means any later cleanup calls that reuse ctx can inherit an already-expired context. In particular, the if err != nil { cleanupAfterDeadShim(ctx, ...) } path will pass a timed-out context into cleanupAfterDeadShim, and its internal timeout.WithContext(ctx, cleanupTimeout) will be canceled immediately, preventing cleanup. Cleanup should use a detached context (e.g., context.WithoutCancel(ctx) or the pre-timeout parent) so it still gets the cleanup budget after a load timeout.
	// One budget for the whole load: shims are loaded during plugin
	// initialization, so a shim that never answers would otherwise stall
	// containerd startup. Nested timeouts can only shorten a deadline, so this
	// bounds the load however many calls it makes.
	ctx, cancel := timeout.WithContext(ctx, loadTimeout)
	defer cancel()

core/runtime/v2/shim.go:207

  • cleanupShimTask propagates errdefs.ErrNotFound from shimTask.delete, but in the leaked-shim cleanup case a NotFound from task.Delete is expected (no task was ever created) and shimTask.delete still proceeds to shutdown/delete the shim instance. Returning NotFound here causes loadShim to fail and log errors even when cleanup succeeded. Consider treating NotFound as a successful cleanup outcome.
	_, err := st.delete(dctx, sandboxed, func(context.Context, string) {})
	if err == nil {
		return nil
	}

Copilot AI review requested due to automatic review settings August 13, 2026 21:31
@austinvazquez
austinvazquez force-pushed the fix/bound-load-shim-to-prevent-stalled-startup branch from 8d6258f to 3787b0f Compare August 13, 2026 21:31

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 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (1)

core/runtime/v2/shim.go:207

  • cleanupShimTask currently propagates the error returned by st.delete(...). shimTask.delete returns errdefs.ErrNotFound when the task has already disappeared (e.g., shim dies between the Pids() check and Delete()), but it still proceeds to clean up (shutdown/delete bundle/close client). Treating ErrNotFound as an error can make loadShim fail and log/return an error even though cleanup effectively succeeded.

	_, err := st.delete(dctx, sandboxed, func(context.Context, string) {})
	if err == nil {
		return nil
	}

loadShim runs during runtime plugin initialization and issued its task
RPCs on the caller's context, which carries no deadline. A shim that
still owns its socket but has stopped serving, for example one leaked by
a previous containerd whose shutdown timed out, accepts the connection
and then never answers. containerd then never finishes starting and its
API socket is never created.

Give loadShim a single loadTimeout budget covering the whole load. Nested
timeouts can only shorten a deadline, so the load stays bounded however
many calls it makes.

Reaping a shim we give up on runs on its own cleanup budget, detached
from the caller so that a cancelled load cannot leave the shim behind. A
failed delete returns before shutting the shim down and closing its
client, so both are done here, and the failure is now propagated: delete
removes the bundle only when it succeeds, and returning an error lets
loadShims remove it, so a shim that cannot be reaped is no longer
reloaded on every subsequent start.

Co-authored-by: Harshal Patel <[email protected]>
Signed-off-by: Austin Vazquez <[email protected]>
Copilot AI review requested due to automatic review settings August 13, 2026 21:49
@austinvazquez
austinvazquez force-pushed the fix/bound-load-shim-to-prevent-stalled-startup branch from 3787b0f to fd29ff1 Compare August 13, 2026 21:49
Comment on lines -228 to -229
ctx, cancel := timeout.WithContext(ctx, loadTimeout)
defer cancel()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Drop the load timeout here since context.WithTimeout does not extend a deadline; it will only shorten it. So here this is now meaningless since load shim task is only called from load shim. It's a little more intuitive for the load shim budget to be for the entire load operation as well.

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 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (1)

core/runtime/v2/shim.go:216

  • When the cleanup timeout context has already expired, this code re-wraps dctx with a new timeout but leaves the original timer/context active until function return. Canceling the first context before creating the second avoids keeping an extra timer around and makes the control flow clearer (you still keep the deferred cancel for the first context).
	if dctx.Err() != nil {
		dctx, cancel = timeout.WithContext(context.WithoutCancel(ctx), cleanupTimeout)
		defer cancel()
	}

@dmcgowan dmcgowan added the cherry-pick/2.3.x Change to be cherry picked to release/2.3 label Aug 14, 2026
@dmcgowan dmcgowan moved this from Needs Triage to Review In Progress in Pull Request Review Aug 14, 2026
@mxpv
mxpv added this pull request to the merge queue Aug 14, 2026
Merged via the queue into containerd:main with commit e1bbb3d Aug 14, 2026
51 checks passed
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Pull Request Review Aug 14, 2026
@austinvazquez
austinvazquez deleted the fix/bound-load-shim-to-prevent-stalled-startup branch August 15, 2026 03:01
@austinvazquez

Copy link
Copy Markdown
Member Author

/cherry-pick release/2.3

@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@austinvazquez: new pull request created: #13983

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.

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

Labels

area/runtime Runtime cherry-pick/2.3.x Change to be cherry picked to release/2.3 size/M

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants