{"record":{"id":"4f106a019dd24774","repo":"containerd/containerd","slug":"an-error-occurred-when-trying-to-find-sandbox-s","errorCode":null,"errorMessage":"an error occurred when trying to find sandbox %s: %w","messagePattern":"an error occurred when trying to find sandbox (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/cri/server/sandbox_stats.go","lineNumber":35,"sourceCode":"package server\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\tcg1 \"github.com/containerd/cgroups/v3/cgroup1/stats\"\n\tcg2 \"github.com/containerd/cgroups/v3/cgroup2/stats\"\n\truntime \"k8s.io/cri-api/pkg/apis/runtime/v1\"\n)\n\nfunc (c *criService) PodSandboxStats(\n\tctx context.Context,\n\tr *runtime.PodSandboxStatsRequest,\n) (*runtime.PodSandboxStatsResponse, error) {\n\n\tsandbox, err := c.sandboxStore.Get(r.GetPodSandboxId())\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"an error occurred when trying to find sandbox %s: %w\", r.GetPodSandboxId(), err)\n\t}\n\n\tpodSandboxStats, err := c.podSandboxStats(ctx, sandbox)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode pod sandbox metrics %s: %w\", r.GetPodSandboxId(), err)\n\t}\n\n\treturn &runtime.PodSandboxStatsResponse{Stats: podSandboxStats}, nil\n}\n\ntype cgroupMetrics struct {\n\tv1 *cg1.Metrics\n\tv2 *cg2.Metrics\n}\n","sourceCodeStart":17,"sourceCodeEnd":50,"githubUrl":"https://github.com/containerd/containerd/blob/4246446a2bf7d03837b0244118d858799393bd80/internal/cri/server/sandbox_stats.go#L17-L50","documentation":"PodSandboxStats first resolves the requested sandbox ID in the in-memory sandbox store; this error wraps the store's not-found (or other store) error. It means the CRI server has no record of the given pod sandbox ID.","triggerScenarios":"Kubelet calls PodSandboxStats with r.GetPodSandboxId() that is not in the sandboxStore — the sandbox was removed, the runtime restarted and lost in-memory state, or the ID string is wrong/empty.","commonSituations":"Pod deleted concurrently while kubelet polls stats; containerd restart wiping the sandbox store while the kubelet's cache still holds the ID; metrics pipeline scraping stale sandbox IDs.","solutions":["List sandboxes (PodSandbox/ListSandboxes) and confirm the ID exists before requesting stats","Handle the wrapped errdefs.ErrNotFound in the caller and drop/invalidate the stale cache entry","Verify the sandbox ID is exact (64-hex containerd ID), not a pod name or partial prefix","If it occurs after restarts, reconcile kubelet/runtime state (restart kubelet or let it resync)"],"exampleFix":"// before\nstats, err := runtimeSvc.PodSandboxStats(ctx, &runtime.PodSandboxStatsRequest{PodSandboxId: id})\n// after: guard with existence check\nif _, err := store.Get(id); err != nil { return nil // sandbox gone; skip stats }\nstats, err := runtimeSvc.PodSandboxStats(ctx, &runtime.PodSandboxStatsRequest{PodSandboxId: id})","handlingStrategy":"type-guard","validationCode":"// confirm existence before requesting stats\nresp, err := client.ListPodSandbox(ctx, &runtime.ListPodSandboxRequest{})\nif err != nil { return err }\nfound := false\nfor _, s := range resp.GetItems() {\n    if s.GetId() == sandboxID { found = true; break }\n}\nif !found { return errdefs.ErrNotFound } // skip stats call","typeGuard":"func sandboxExists(items []*runtime.PodSandbox, id string) bool {\n    for _, s := range items {\n        if s.GetId() == id { return true }\n    }\n    return false\n}","tryCatchPattern":"stats, err := client.PodSandboxStats(ctx, req)\nif err != nil {\n    if errors.Is(err, errdefs.ErrNotFound) || strings.Contains(err.Error(), \"trying to find sandbox\") {\n        cache.Invalidate(sandboxID) // stale entry; skip without alerting\n        return nil\n    }\n    return err\n}","preventionTips":["Invalidate stats caches when ListPodSandbox shows the sandbox is gone","Always use exact 64-char sandbox IDs, never names or prefixes","After runtime restarts, resync before serving stale IDs"],"tags":["cri","sandbox-not-found","stats","kubelet"],"backgroundTag":"sandbox-not-found","analyzedSha":"4246446a2bf7d03837b0244118d858799393bd80","analyzedAt":"2026-09-02T00:14:43.053Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}