From 68ea83ecea0e38d084c0d15c9e99c0b4494b1f32 Mon Sep 17 00:00:00 2001
From: zhongjiawei <zhongjiawei1@huawei.com>
Date: Thu, 22 Aug 2024 20:22:43 +0800
Subject: [PATCH] docker: try to reconnect when containerd grpc return
 unexpected EOF

---
 .../engine/libcontainerd/client_daemon.go     | 26 ++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/components/engine/libcontainerd/client_daemon.go b/components/engine/libcontainerd/client_daemon.go
index 09ce6e1f5..14f420ed8 100755
--- a/components/engine/libcontainerd/client_daemon.go
+++ b/components/engine/libcontainerd/client_daemon.go
@@ -38,9 +38,12 @@ import (
 	"google.golang.org/grpc/status"
 )
 
-// InitProcessName is the name given to the first process of a
-// container
-const InitProcessName = "init"
+const (
+	// InitProcessName is the name given to the first process of a container
+	InitProcessName = "init"
+	// RetryMax is the max num to connect containerd grpc
+	RetryMax = 10
+)
 
 type container struct {
 	mu sync.Mutex
@@ -167,9 +170,20 @@ func (c *client) Restore(ctx context.Context, id string, attachStdio StdioCallba
 		err = wrapError(err)
 	}()
 
-	ctr, err := c.client.LoadContainer(ctx, id)
-	if err != nil {
-		return false, -1, errors.WithStack(wrapError(err))
+	var ctr containerd.Container
+	var err1 error
+	for retry := 1; retry <= RetryMax; retry++ {
+		ctr, err1 = c.client.LoadContainer(ctx, id)
+		if err1 == nil {
+			break
+		} else if strings.Contains(err1.Error(), "unexpected EOF") {
+			time.Sleep(time.Millisecond * 100)
+			continue
+		}
+		return false, -1, errors.WithStack(wrapError(err1))
+	}
+	if err1 != nil {
+		return false, -1, errors.Wrap(wrapError(err1), "reconnect load contianer failed")
 	}
 
 	attachIO := func(fifos *cio.FIFOSet) (cio.IO, error) {
-- 
2.33.0

