From 6f451082531257dc3196ea2dc9fe9a1407e6c536 Mon Sep 17 00:00:00 2001 From: Wei Fu Date: Sun, 20 Feb 2022 13:58:37 +0800 Subject: [PATCH] runc.v1/v2: return init pid when clean dead shim If containerd-shim-runc-v1/v2 process dead abnormally, such as received kill 9 signal, panic or other unkown reasons, the containerd-shim-runc-v1/v2 server can not reap runc container and forward init process exit event. This will lead the container leaked in dockerd. When shim dead, containerd will clean dead shim, here read init process pid and forward exit event with pid at the same time. Signed-off-by: Jeff Zvier Signed-off-by: Wei Fu --- runtime/v2/runc/v1/service.go | 8 ++++++++ runtime/v2/runc/v2/service.go | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/runtime/v2/runc/v1/service.go b/runtime/v2/runc/v1/service.go index 71f2346d35c0..2fb4626bc10a 100644 --- a/runtime/v2/runc/v1/service.go +++ b/runtime/v2/runc/v1/service.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux /* @@ -247,9 +248,16 @@ func (s *service) Cleanup(ctx context.Context) (*taskAPI.DeleteResponse, error) if err := mount.UnmountAll(filepath.Join(path, "rootfs"), 0); err != nil { logrus.WithError(err).Warn("failed to cleanup rootfs mount") } + + pid, err := runcC.ReadPidFile(filepath.Join(path, process.InitPidFile)) + if err != nil { + logrus.WithError(err).Warn("failed to read init pid file") + } + return &taskAPI.DeleteResponse{ ExitedAt: time.Now(), ExitStatus: 128 + uint32(unix.SIGKILL), + Pid: uint32(pid), }, nil } diff --git a/runtime/v2/runc/v2/service.go b/runtime/v2/runc/v2/service.go index 317c3f8f08ca..ed39b73324ec 100644 --- a/runtime/v2/runc/v2/service.go +++ b/runtime/v2/runc/v2/service.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux /* @@ -321,9 +322,16 @@ func (s *service) Cleanup(ctx context.Context) (*taskAPI.DeleteResponse, error) if err := mount.UnmountAll(filepath.Join(path, "rootfs"), 0); err != nil { logrus.WithError(err).Warn("failed to cleanup rootfs mount") } + + pid, err := runcC.ReadPidFile(filepath.Join(path, process.InitPidFile)) + if err != nil { + logrus.WithError(err).Warn("failed to read init pid file") + } + return &taskAPI.DeleteResponse{ ExitedAt: time.Now(), ExitStatus: 128 + uint32(unix.SIGKILL), + Pid: uint32(pid), }, nil }