From c56df3dd08d709e8ee81675661527aac47a7cba2 Mon Sep 17 00:00:00 2001
From: xiadanni1 <xiadanni1@huawei.com>
Date: Fri, 6 Nov 2020 10:19:26 +0800
Subject: [PATCH] containerd: check if bundle exists before create bundle

reason: If container starts following tightly the last stop, bundle
directory may be deleted by the not yet completed stop, which may cause
container start fail. So we add bundle check during start to avoid this,
if bundle exists, wait for it to clean up.

Signed-off-by: xiadanni1 <xiadanni1@huawei.com>
---
 runtime/v1/linux/bundle.go | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/runtime/v1/linux/bundle.go b/runtime/v1/linux/bundle.go
index d73866a..b4f7b4c 100644
--- a/runtime/v1/linux/bundle.go
+++ b/runtime/v1/linux/bundle.go
@@ -23,12 +23,14 @@ import (
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"time"
 
 	"github.com/containerd/containerd/events/exchange"
 	"github.com/containerd/containerd/runtime/linux/runctypes"
 	"github.com/containerd/containerd/runtime/v1/shim"
 	"github.com/containerd/containerd/runtime/v1/shim/client"
 	"github.com/pkg/errors"
+	"github.com/sirupsen/logrus"
 )
 
 // loadBundle loads an existing bundle from disk
@@ -46,6 +48,20 @@ func newBundle(id, path, workDir string, spec []byte) (b *bundle, err error) {
 		return nil, err
 	}
 	path = filepath.Join(path, id)
+	workDir = filepath.Join(workDir, id)
+
+	for waitTime := 10 * time.Millisecond; ; waitTime *= 2 {
+		if _, err = os.Stat(workDir); err != nil {
+			break
+		}
+		logrus.Debugf("bundle-check: wait time %v", waitTime)
+		if waitTime > 2*time.Second {
+			logrus.Warnf("bundle-check: waiting cleanup bundle timeout, start anyway")
+			break
+		}
+		time.Sleep(waitTime)
+	}
+
 	if err := os.Mkdir(path, 0711); err != nil {
 		return nil, err
 	}
@@ -54,7 +70,6 @@ func newBundle(id, path, workDir string, spec []byte) (b *bundle, err error) {
 			os.RemoveAll(path)
 		}
 	}()
-	workDir = filepath.Join(workDir, id)
 	if err := os.MkdirAll(workDir, 0711); err != nil {
 		return nil, err
 	}
-- 
1.8.3.1

