From 893aa4e8c70c5f5fc3e36f760a00949d84d9cb20 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Fri, 1 Dec 2017 11:04:51 -0500 Subject: [PATCH] Be more diligent about cleaning up failed-to-create containers If server/Server.createSandboxContainer() fails after calling server/Server.StorageRuntimeServer().CreateContainer(), cleanup logic in server/Server.CreateContainer() won't try to clean it up, but we still need to clean up the on-disk container and its layer. Signed-off-by: Nalin Dahyabhai --- server/container_create.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/container_create.go b/server/container_create.go index e23dedb6..080a1af7 100644 --- a/server/container_create.go +++ b/server/container_create.go @@ -1065,6 +1065,14 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string, if err != nil { return nil, err } + defer func() { + if err != nil { + err2 := s.StorageRuntimeServer().DeleteContainer(containerInfo.ID) + if err2 != nil { + logrus.Warnf("Failed to cleanup container directory: %v", err2) + } + } + }() mountPoint, err := s.StorageRuntimeServer().StartContainer(containerID) if err != nil { @@ -1074,7 +1082,8 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string, containerImageConfig := containerInfo.Config if containerImageConfig == nil { - return nil, fmt.Errorf("empty image config for %s", image) + err = fmt.Errorf("empty image config for %s", image) + return nil, err } if containerImageConfig.Config.StopSignal != "" {