Преглед изворни кода

Not use goroutine for container's auto-removal

Before this, container's auto-removal after exit is done in a goroutine,
this commit will get ContainerRm out of the goroutine.

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
Zhang Wei пре 9 година
родитељ
комит
1537dbe2d6
2 измењених фајлова са 17 додато и 6 уклоњено
  1. 9 0
      daemon/monitor.go
  2. 8 6
      daemon/start.go

+ 9 - 0
daemon/monitor.go

@@ -11,6 +11,7 @@ import (
 	"github.com/docker/docker/daemon/exec"
 	"github.com/docker/docker/libcontainerd"
 	"github.com/docker/docker/runconfig"
+	"github.com/docker/engine-api/types"
 )
 
 // StateChanged updates daemon state changes from containerd
@@ -29,6 +30,14 @@ func (daemon *Daemon) StateChanged(id string, e libcontainerd.StateInfo) error {
 		daemon.updateHealthMonitor(c)
 		daemon.LogContainerEvent(c, "oom")
 	case libcontainerd.StateExit:
+		// if containers AutoRemove flag is set, remove it after clean up
+		if c.HostConfig.AutoRemove {
+			defer func() {
+				if err := daemon.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {
+					logrus.Errorf("can't remove container %s: %v", c.ID, err)
+				}
+			}()
+		}
 		c.Lock()
 		defer c.Unlock()
 		c.Wait()

+ 8 - 6
daemon/start.go

@@ -113,6 +113,14 @@ func (daemon *Daemon) containerStart(container *container.Container) (err error)
 			}
 			container.ToDisk()
 			daemon.Cleanup(container)
+			// if containers AutoRemove flag is set, remove it after clean up
+			if container.HostConfig.AutoRemove {
+				container.Unlock()
+				if err := daemon.ContainerRm(container.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {
+					logrus.Errorf("can't remove container %s: %v", container.ID, err)
+				}
+				container.Lock()
+			}
 		}
 	}()
 
@@ -198,10 +206,4 @@ func (daemon *Daemon) Cleanup(container *container.Container) {
 		}
 	}
 	container.CancelAttachContext()
-
-	// if containers AutoRemove flag is set, remove it after clean up
-	if container.HostConfig.AutoRemove {
-		// If containers lock is not released, goroutine will guarantee no block
-		go daemon.ContainerRm(container.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true})
-	}
 }