Skip to content

shim: optimize reap to prevent event lost - #13544

Open
ningmingxiao wants to merge 1 commit into
containerd:mainfrom
ningmingxiao:optimize_reap2
Open

ningmingxiao wants to merge 1 commit into
containerd:mainfrom
ningmingxiao:optimize_reap2

Conversation

@ningmingxiao

@ningmingxiao ningmingxiao commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

root reason

func Reap() error {
	now := time.Now()
	exits, err := reap(false)
	for _, e := range exits {
		done := Default.notify(runc.Exit{
			Timestamp: now,
			Pid:       e.Pid,
			Status:    e.Status,
		})

		select {
		case <-done:
		case <-time.After(1 * time.Second):
		}
	}
	return err
}

if notify timeout the exit event may lost

try to reduce the probability of container event loss try fix #12678

reproduce

  1. cat gen.sh and then run it.
#!/bin/bash
cat > pod-200-sleep.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
  name: two-hundred-sleep-pod
spec:
  containers:
EOF

for i in $(seq -w 1 200); do
cat >> pod-200-sleep.yaml << EOC
    - name: sleep-$i
      image: docker.io/library/busybox:latest
      imagePullPolicy: IfNotPresent
      command: ["sleep", "1000"]
      resources:
        requests:
          cpu: 1m
          memory: 10Mi
        limits:
          cpu: 10m
          memory: 50Mi
EOC
done


cat >> pod-200-sleep.yaml << EOF
  restartPolicy: Never
EOF

echo "已生成 pod-200-sleep.yaml,共200个sleep容器"
  1. add some sleep and set const bufferSize = 1 (default is const bufferSize = 32)
			for _, s := range subscribers {
				s.do(func() {
					if s.closed {
						return
					}
					if _, ok := success[s.c]; ok {
						return
					}
					timer.Reset(timeout)
					recv := true
					if _, err := os.Stat("/tmp/debug2"); err == nil {
						time.Sleep(time.Second)
					}
  1. kubectl create -f pod-200-sleep.yaml
    after pod is running
    echo 11> /tmp/debug2
    4 . kill -9 pidof sleep
    5.crictl ps |grep sleep |wc -l
[root@host-10-234-74-247 nmx]# crictl  ps |grep sleep|grep ddd
ddd071824e258       fa152d3b2cb95       9 minutes ago       Running             sleep-001                  0                   2a0bdd76b4e8c       two-hundred-sleep-pod
[root@host-10-234-74-247 nmx]# crictl  exec ddd date
FATA[0000] execing command in container: Internal error occurred: error executing command in container: failed to exec in container: failed to create exec "086bef8279ee5828d269c317d16b3fb3102948b9db4a91310779daca0f2543b3": cannot exec in a stopped state: unknown 


containerd --version
containerd github.com/containerd/containerd v1.7.27 05044ec

shim stack
666666.txt

repeat log

Jun 08 22:04:20 host-10-234-74-247 containerd[52219]: time="2026-06-08T22:04:20.832301365+08:00" level=info msg="received exit event container_id:\"ddd071824e258f0d65094a903481e2f0e6e10b79aebad1f890066ac83cafedcb\"  id:\"ddd071824e258f0d65094a903481e2f0e6e10b79aebad1f890066ac83cafedcb\"  pid:85061  exit_status:137  exited_at:{seconds:1780927460  nanos:832086580}"
Jun 08 22:04:23 host-10-234-74-247 containerd[52219]: time="2026-06-08T22:04:23.622300440+08:00" level=error msg="collecting metrics for ddd071824e258f0d65094a903481e2f0e6e10b79aebad1f890066ac83cafedcb" error="cgroups: cgroup deleted: unknown"
Jun 08 22:04:30 host-10-234-74-247 containerd[52219]: time="2026-06-08T22:04:30.832839191+08:00" level=error msg="failed to handle container TaskExit event container_id:\"ddd071824e258f0d65094a903481e2f0e6e10b79aebad1f890066ac83cafedcb\"  id:\"ddd071824e258f0d65094a903481e2f0e6e10b79aebad1f890066ac83cafedcb\"  pid:85061  exit_status:137  exited_at:{seconds:1780927460  nanos:832086580}" error="failed to stop container: context deadline exceeded: unknown"

ctr -n k8s.io t ls|grep ddd is hanging

Copilot AI review requested due to automatic review settings June 8, 2026 08:54
@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review Jun 8, 2026

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

Note

Copilot was unable to run its full agentic suite in this review.

This PR changes the SIGCHLD reaping flow to enqueue exit events and notify subscribers asynchronously via a background goroutine.

Changes:

  • Introduces a SafeList-backed global event queue and a notify() goroutine started in init()
  • Updates reap() to push exit events into the queue instead of returning an exit slice
  • Adds logging + increases the wait timeout while notifying subscribers

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/sys/reaper/reaper_unix.go Resolved
Comment thread pkg/sys/reaper/reaper_unix.go Resolved
Comment thread pkg/sys/reaper/reaper_unix.go Resolved
Comment thread pkg/sys/reaper/reaper_unix.go Resolved
Comment thread pkg/sys/reaper/reaper_unix.go Resolved
Comment thread pkg/sys/reaper/reaper_unix.go Resolved
Comment thread pkg/sys/reaper/reaper_unix.go Resolved
Comment thread pkg/sys/reaper/reaper_unix.go Resolved
Comment thread pkg/sys/reaper/reaper_unix.go Resolved

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

Comment on lines +104 to +123
func notify() {
go func() {
for {
event := eventQueue.Pop()
if exitEvt, ok := event.(eventExit); ok {
done := Default.notify(runc.Exit{
Timestamp: exitEvt.EventTime,
Pid: exitEvt.Pid,
Status: exitEvt.Status,
})
select {
case <-done:
// TODO: delete time out
case <-time.After(30 * time.Second):
log.L.Warnf("event with pid %d status:%d may be lost", exitEvt.Pid, exitEvt.Status)
}
}
}
}()
}
Comment on lines +37 to +42
var (
once sync.Once
eventQueue = NewSafeList()
// ErrNoSuchProcess is returned when the process no longer exists
ErrNoSuchProcess = errors.New("no such process")
)
Comment on lines +83 to +102
func (sl *SafeList) Push(value any) {
sl.mu.Lock()
defer sl.mu.Unlock()

sl.queue.PushBack(value)
sl.cond.Signal()
}

func (sl *SafeList) Pop() any {
sl.mu.Lock()
defer sl.mu.Unlock()

for sl.queue.Len() == 0 {
sl.cond.Wait()
}
elem := sl.queue.Front()
value := elem.Value
sl.queue.Remove(elem)
return value
}
Comment on lines +114 to +119
select {
case <-done:
// TODO: delete time out
case <-time.After(30 * time.Second):
log.L.Warnf("event with pid %d status:%d may be lost", exitEvt.Pid, exitEvt.Status)
}
Comment thread pkg/sys/reaper/reaper_unix.go Outdated
})
select {
case <-done:
// TODO: delete time out
@yong-jie-gong

Copy link
Copy Markdown

@ningmingxiao Thanks for the reproduction and for opening this PR! Your SafeList approach nicely decouples Reap() from notification delivery.

After further investigation, I found that the root cause of #12678 is actually in the CRI plugin layer, not the reaper. When handleContainerExit() calls task.Delete() and it times out (IO pressure), the function returns early without updating FinishedAt — leaving crictl inspect stuck at RUNNING even though ctr tasks ls correctly shows STOPPED.

I'll open a separate fix for that (move Status.UpdateSync before task.Delete). Your PR is still valuable as defense-in-depth at the shim layer — they're complementary fixes at different levels.

@ningmingxiao

Copy link
Copy Markdown
Contributor Author

ping @samuelkarp @fuweid

This branch has not been deployed

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

Labels

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

CRI plugin: container stuck as RUNNING after process exit

4 participants