Skip to content

fix(shim): support non-CRI callers (BuildKit, ctr) in containerd shim#12873

Open
alan890104 wants to merge 1 commit intogoogle:masterfrom
alan890104:minimal-fix
Open

fix(shim): support non-CRI callers (BuildKit, ctr) in containerd shim#12873
alan890104 wants to merge 1 commit intogoogle:masterfrom
alan890104:minimal-fix

Conversation

@alan890104
Copy link
Copy Markdown

Summary

Fix two issues that prevent gVisor containers from working when created through containerd's direct API (non-CRI), such as BuildKit's containerd worker or ctr run.

1. Sandbox detection for non-CRI callers

SpecContainerType returns ContainerTypeUnspecified for non-CRI callers that don't set the container type annotation (io.kubernetes.cri.container-type). Previously this resulted in p.Sandbox=false, which skipped passing IO file descriptors to runsc create. Without IO, container processes receive SIGPIPE on stdout/stderr writes and exit with code 128.

Fix: treat ContainerTypeUnspecified as sandbox, since non-CRI containers are always root/sandbox containers.

2. Wait error handling for short-lived containers

For short-lived containers, the sandbox may exit before runsc wait retrieves the exit status, producing "sandbox no longer running and its exit status is unavailable". This error was treated as fatal (setting internalErrorCode=128) even when the container exited successfully (status=0).

Fix: extract adjustWaitStatus() function — when status is 0, log the error as a warning but preserve the exit status.

Test plan

  • TestSandboxDetection — 9 cases covering all container type variants including no-annotation (BuildKit/ctr), containerd sandbox/container, CRI-O sandbox/container, unknown values, dual annotations
  • TestAdjustWaitStatus — 9 cases covering status=0 success, non-zero exit, signal exit (137), benign sandbox error with status=0, non-zero with error, negative status, exit 255
  • E2E: BuildKit containerd worker with gVisor on GCE e2-standard-4, containerd v1.7.29, runsc release-20260330.0. Eight consecutive RUN steps (echo, ls, resolv.conf, apk add curl jq + curl HTTPS, exit code, multi-line script, id, uname) all pass with exit code 0.

Fixes #12198

@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 5, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Fix two issues that prevent gVisor containers from working when
created through containerd's direct API (non-CRI), such as BuildKit's
containerd worker or `ctr run`:

1. Sandbox detection: SpecContainerType returns ContainerTypeUnspecified
   for non-CRI callers that don't set the container type annotation.
   Previously this resulted in p.Sandbox=false, which skipped passing
   IO file descriptors to runsc create. Without IO, container processes
   receive SIGPIPE on stdout/stderr writes and exit with code 128.

   Fix: treat ContainerTypeUnspecified as sandbox, since non-CRI
   containers are always root/sandbox containers.

2. Wait error handling: For short-lived containers, the sandbox may
   exit before `runsc wait` retrieves the exit status, producing
   "sandbox no longer running and its exit status is unavailable".
   This error was treated as fatal (setting internalErrorCode=128)
   even when the container exited successfully (status=0).

   Fix: extract adjustWaitStatus() and when status is 0, log the
   error as a warning but preserve the exit status.

Includes unit tests for both fixes:
- TestSandboxDetection: 7 cases covering all container type variants
- TestAdjustWaitStatus: 7 cases covering all status/error combinations

Fixes google#12198

Tested: BuildKit containerd worker with gVisor on GCE e2-standard-4,
containerd v1.7.29, runsc release-20260330.0. Six consecutive RUN
steps (echo, ls, cat, apk add curl) all pass with exit code 0.
copybara-service bot pushed a commit that referenced this pull request Apr 6, 2026
## Summary

Fix two issues that prevent gVisor containers from working when created through containerd's direct API (non-CRI), such as BuildKit's containerd worker or `ctr run`.

### 1. Sandbox detection for non-CRI callers

`SpecContainerType` returns `ContainerTypeUnspecified` for non-CRI callers that don't set the container type annotation (`io.kubernetes.cri.container-type`). Previously this resulted in `p.Sandbox=false`, which skipped passing IO file descriptors to `runsc create`. Without IO, container processes receive SIGPIPE on stdout/stderr writes and exit with code 128.

**Fix:** treat `ContainerTypeUnspecified` as sandbox, since non-CRI containers are always root/sandbox containers.

### 2. Wait error handling for short-lived containers

For short-lived containers, the sandbox may exit before `runsc wait` retrieves the exit status, producing `"sandbox no longer running and its exit status is unavailable"`. This error was treated as fatal (setting `internalErrorCode=128`) even when the container exited successfully (`status=0`).

**Fix:** extract `adjustWaitStatus()` function — when status is 0, log the error as a warning but preserve the exit status.

## Test plan

- [x] `TestSandboxDetection` — 9 cases covering all container type variants including no-annotation (BuildKit/ctr), containerd sandbox/container, CRI-O sandbox/container, unknown values, dual annotations
- [x] `TestAdjustWaitStatus` — 9 cases covering status=0 success, non-zero exit, signal exit (137), benign sandbox error with status=0, non-zero with error, negative status, exit 255
- [x] E2E: BuildKit containerd worker with gVisor on GCE e2-standard-4, containerd v1.7.29, runsc release-20260330.0. Eight consecutive RUN steps (echo, ls, resolv.conf, apk add curl jq + curl HTTPS, exit code, multi-line script, id, uname) all pass with exit code 0.

Fixes #12198

FUTURE_COPYBARA_INTEGRATE_REVIEW=#12873 from alan890104:minimal-fix c5fdfd4
PiperOrigin-RevId: 895531992
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No output and hangs up without TTY mode

2 participants