From ebe1a56fb28e7de7128167973a99061e6aa0222a Mon Sep 17 00:00:00 2001
From: chenjiankun <chenjiankun1@huawei.com>
Date: Thu, 7 Jul 2022 10:18:03 +0800
Subject: [PATCH] docker: Add an ExitPid field for State struct to record exit
 process id

---
 components/engine/container/state.go | 1 +
 components/engine/daemon/monitor.go  | 4 ++++
 2 files changed, 5 insertions(+)

diff --git a/components/engine/container/state.go b/components/engine/container/state.go
index da19cc49e..292b0ec0b 100644
--- a/components/engine/container/state.go
+++ b/components/engine/container/state.go
@@ -27,6 +27,7 @@ type State struct {
 	RemovalInProgress bool // Not need for this to be persistent on disk.
 	Dead              bool
 	Pid               int
+	ExitPid           uint32 // record exit process id.
 	ExitCodeValue     int    `json:"ExitCode"`
 	ErrorMsg          string `json:"Error"` // contains last known error during container start, stop, or remove
 	StartedAt         time.Time
diff --git a/components/engine/daemon/monitor.go b/components/engine/daemon/monitor.go
index a5c7ff5c8..89c05f3da 100644
--- a/components/engine/daemon/monitor.go
+++ b/components/engine/daemon/monitor.go
@@ -57,6 +57,7 @@ func (daemon *Daemon) ProcessEvent(id string, e libcontainerd.EventType, ei libc
 
 		daemon.LogContainerEvent(c, "oom")
 	case libcontainerd.EventExit:
+		c.ExitPid = ei.Pid
 		if int(ei.Pid) == c.Pid {
 			logrus.Infof("handle container %s exit event pid=%d", c.ID, c.Pid)
 			c.Lock()
@@ -169,6 +170,9 @@ func (daemon *Daemon) ProcessEvent(id string, e libcontainerd.EventType, ei libc
 
 		// This is here to handle start not generated by docker
 		if !c.Running {
+			if c.ExitPid == ei.Pid && time.Now().UTC().Sub(c.FinishedAt).Seconds() < 3 {
+				return nil
+			}
 			c.SetRunning(int(ei.Pid), false)
 			c.HasBeenManuallyStopped = false
 			c.HasBeenStartedBefore = true
-- 
2.23.0

