Browse code

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 authored on 2016/08/03 11:11:01
Showing 2 changed files
... ...
@@ -11,6 +11,7 @@ import (
11 11
 	"github.com/docker/docker/daemon/exec"
12 12
 	"github.com/docker/docker/libcontainerd"
13 13
 	"github.com/docker/docker/runconfig"
14
+	"github.com/docker/engine-api/types"
14 15
 )
15 16
 
16 17
 // StateChanged updates daemon state changes from containerd
... ...
@@ -29,6 +30,14 @@ func (daemon *Daemon) StateChanged(id string, e libcontainerd.StateInfo) error {
29 29
 		daemon.updateHealthMonitor(c)
30 30
 		daemon.LogContainerEvent(c, "oom")
31 31
 	case libcontainerd.StateExit:
32
+		// if containers AutoRemove flag is set, remove it after clean up
33
+		if c.HostConfig.AutoRemove {
34
+			defer func() {
35
+				if err := daemon.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {
36
+					logrus.Errorf("can't remove container %s: %v", c.ID, err)
37
+				}
38
+			}()
39
+		}
32 40
 		c.Lock()
33 41
 		defer c.Unlock()
34 42
 		c.Wait()
... ...
@@ -113,6 +113,14 @@ func (daemon *Daemon) containerStart(container *container.Container) (err error)
113 113
 			}
114 114
 			container.ToDisk()
115 115
 			daemon.Cleanup(container)
116
+			// if containers AutoRemove flag is set, remove it after clean up
117
+			if container.HostConfig.AutoRemove {
118
+				container.Unlock()
119
+				if err := daemon.ContainerRm(container.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {
120
+					logrus.Errorf("can't remove container %s: %v", container.ID, err)
121
+				}
122
+				container.Lock()
123
+			}
116 124
 		}
117 125
 	}()
118 126
 
... ...
@@ -198,10 +206,4 @@ func (daemon *Daemon) Cleanup(container *container.Container) {
198 198
 		}
199 199
 	}
200 200
 	container.CancelAttachContext()
201
-
202
-	// if containers AutoRemove flag is set, remove it after clean up
203
-	if container.HostConfig.AutoRemove {
204
-		// If containers lock is not released, goroutine will guarantee no block
205
-		go daemon.ContainerRm(container.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true})
206
-	}
207 201
 }