Skip to content

internal/render: prevent index out of range panic in initContainerStats#3870

Open
umut-polat wants to merge 1 commit intoderailed:masterfrom
umut-polat:fix/init-container-stats-panic
Open

internal/render: prevent index out of range panic in initContainerStats#3870
umut-polat wants to merge 1 commit intoderailed:masterfrom
umut-polat:fix/init-container-stats-panic

Conversation

@umut-polat
Copy link

During pod initialization, InitContainerStatuses can have more entries than InitContainers (or they may temporarily be out of sync). initContainerStats iterates over the status slice and directly indexes into the container slice at the same position, causing:

panic: runtime error: index out of range [1] with length 1
goroutine 440 [running]:
github.com/derailed/k9s/internal/render.(*Pod).initContainerStats(...)
    github.com/derailed/k9s/internal/render/pod.go:430

Added a bounds check to break early when the status index exceeds the container list length.

Fixes #3866

initContainerStats iterates over InitContainerStatuses and accesses
InitContainers at the same index. When the status list is longer than
the container list (e.g. during pod initialization), this causes an
index out of range panic.

Add a bounds check to break early when the status index exceeds the
container list length.

Fixes derailed#3866
Copy link
Owner

@derailed derailed left a comment

Choose a reason for hiding this comment

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

@umut-polat Good catch!

@@ -427,6 +427,9 @@ func (*Pod) ContainerStats(cc []v1.ContainerStatus) (readyCnt, terminatedCnt, re

func (*Pod) initContainerStats(cc []v1.Container, cos []v1.ContainerStatus) (ready, total, restart int) {
for i := range cos {
Copy link
Owner

@derailed derailed Mar 6, 2026

Choose a reason for hiding this comment

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

I think best to match container names here and 'continue' not break if containerStatus is not present.
Also we should include some tests to make sure we handle this correctly.

@derailed derailed added bug Something isn't working needs-tlc Pr needs additional updates labels Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working needs-tlc Pr needs additional updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic on the pod view during initContainerStats

2 participants