Repository navigation
Conversation
There was a problem hiding this comment.
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
cleanupShimTaskhelper and reuses it from both task creation failure and shim-load cleanup paths. - Wraps
ShimManager.loadShimwork withloadTimeoutto bound bundle loading during plugin initialization. - Updates
shouldCleanupShimtest 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.
| // 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) |
There was a problem hiding this comment.
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.
| // 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() |
There was a problem hiding this comment.
Cleanup should use the cleanup budget.
99e231d to
fdcdb86
Compare
There was a problem hiding this comment.
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
fdcdb86 to
8d6258f
Compare
There was a problem hiding this comment.
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
loadShimnow wrapsctxwithloadTimeout, which means any later cleanup calls that reusectxcan inherit an already-expired context. In particular, theif err != nil { cleanupAfterDeadShim(ctx, ...) }path will pass a timed-out context intocleanupAfterDeadShim, and its internaltimeout.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
cleanupShimTaskpropagateserrdefs.ErrNotFoundfromshimTask.delete, but in the leaked-shim cleanup case a NotFound fromtask.Deleteis expected (no task was ever created) andshimTask.deletestill proceeds to shutdown/delete the shim instance. Returning NotFound here causesloadShimto 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
}
8d6258f to
3787b0f
Compare
There was a problem hiding this comment.
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
cleanupShimTaskcurrently propagates the error returned byst.delete(...).shimTask.deletereturnserrdefs.ErrNotFoundwhen the task has already disappeared (e.g., shim dies between thePids()check andDelete()), but it still proceeds to clean up (shutdown/delete bundle/close client). TreatingErrNotFoundas an error can makeloadShimfail 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]>
3787b0f to
fd29ff1
Compare
| ctx, cancel := timeout.WithContext(ctx, loadTimeout) | ||
| defer cancel() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
dctxwith 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()
}
|
/cherry-pick release/2.3 |
|
@austinvazquez: new pull request created: #13983 DetailsIn response to this:
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. |
Carries #13852
This change applies the
io.containerd.timeout.shim.loadbudget to all of shim manager load shim andio.containerd.timeout.shim.cleanupto cleanup shim calls to prevent a previously leaked / wedged shim from stalling containerd startup.