Browse code

Fix race in native driver on activeContainers usage

Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)

Alexandr Morozov authored on 2014/05/29 21:40:42
Showing 2 changed files
... ...
@@ -47,9 +47,11 @@ func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container
47 47
 		return nil, err
48 48
 	}
49 49
 	cmds := make(map[string]*exec.Cmd)
50
+	d.Lock()
50 51
 	for k, v := range d.activeContainers {
51 52
 		cmds[k] = v.cmd
52 53
 	}
54
+	d.Unlock()
53 55
 	if err := configuration.ParseConfiguration(container, cmds, c.Config["native"]); err != nil {
54 56
 		return nil, err
55 57
 	}
... ...
@@ -86,7 +88,9 @@ func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.
86 86
 	}
87 87
 
88 88
 	if c.Network.ContainerID != "" {
89
+		d.Lock()
89 90
 		active := d.activeContainers[c.Network.ContainerID]
91
+		d.Unlock()
90 92
 		if active == nil || active.cmd.Process == nil {
91 93
 			return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
92 94
 		}
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"os/exec"
9 9
 	"path/filepath"
10 10
 	"strings"
11
+	"sync"
11 12
 	"syscall"
12 13
 
13 14
 	"github.com/dotcloud/docker/daemon/execdriver"
... ...
@@ -62,6 +63,7 @@ type driver struct {
62 62
 	root             string
63 63
 	initPath         string
64 64
 	activeContainers map[string]*activeContainer
65
+	sync.Mutex
65 66
 }
66 67
 
67 68
 func NewDriver(root, initPath string) (*driver, error) {
... ...
@@ -87,10 +89,12 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
87 87
 	if err != nil {
88 88
 		return -1, err
89 89
 	}
90
+	d.Lock()
90 91
 	d.activeContainers[c.ID] = &activeContainer{
91 92
 		container: container,
92 93
 		cmd:       &c.Cmd,
93 94
 	}
95
+	d.Unlock()
94 96
 
95 97
 	var (
96 98
 		dataPath = filepath.Join(d.root, c.ID)
... ...
@@ -186,7 +190,9 @@ func (d *driver) Name() string {
186 186
 }
187 187
 
188 188
 func (d *driver) GetPidsForContainer(id string) ([]int, error) {
189
+	d.Lock()
189 190
 	active := d.activeContainers[id]
191
+	d.Unlock()
190 192
 
191 193
 	if active == nil {
192 194
 		return nil, fmt.Errorf("active container for %s does not exist", id)
... ...
@@ -212,7 +218,9 @@ func (d *driver) createContainerRoot(id string) error {
212 212
 }
213 213
 
214 214
 func (d *driver) removeContainerRoot(id string) error {
215
+	d.Lock()
215 216
 	delete(d.activeContainers, id)
217
+	d.Unlock()
216 218
 
217 219
 	return os.RemoveAll(filepath.Join(d.root, id))
218 220
 }