Browse code

Fix races in set/get currentInterfaces in networkdriver

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

Alexandr Morozov authored on 2014/05/29 21:28:06
Showing 1 changed files
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"log"
7 7
 	"net"
8 8
 	"strings"
9
+	"sync"
9 10
 
10 11
 	"github.com/dotcloud/docker/daemon/networkdriver"
11 12
 	"github.com/dotcloud/docker/daemon/networkdriver/ipallocator"
... ...
@@ -28,6 +29,24 @@ type networkInterface struct {
28 28
 	PortMappings []net.Addr // there are mappings to the host interfaces
29 29
 }
30 30
 
31
+type ifaces struct {
32
+	c map[string]*networkInterface
33
+	sync.Mutex
34
+}
35
+
36
+func (i *ifaces) Set(key string, n *networkInterface) {
37
+	i.Lock()
38
+	i.c[key] = n
39
+	i.Unlock()
40
+}
41
+
42
+func (i *ifaces) Get(key string) *networkInterface {
43
+	i.Lock()
44
+	res := i.c[key]
45
+	i.Unlock()
46
+	return res
47
+}
48
+
31 49
 var (
32 50
 	addrs = []string{
33 51
 		// Here we don't follow the convention of using the 1st IP of the range for the gateway.
... ...
@@ -53,7 +72,7 @@ var (
53 53
 	bridgeNetwork *net.IPNet
54 54
 
55 55
 	defaultBindingIP  = net.ParseIP("0.0.0.0")
56
-	currentInterfaces = make(map[string]*networkInterface)
56
+	currentInterfaces = ifaces{c: make(map[string]*networkInterface)}
57 57
 )
58 58
 
59 59
 func InitDriver(job *engine.Job) engine.Status {
... ...
@@ -321,9 +340,9 @@ func Allocate(job *engine.Job) engine.Status {
321 321
 	size, _ := bridgeNetwork.Mask.Size()
322 322
 	out.SetInt("IPPrefixLen", size)
323 323
 
324
-	currentInterfaces[id] = &networkInterface{
324
+	currentInterfaces.Set(id, &networkInterface{
325 325
 		IP: *ip,
326
-	}
326
+	})
327 327
 
328 328
 	out.WriteTo(job.Stdout)
329 329
 
... ...
@@ -334,7 +353,7 @@ func Allocate(job *engine.Job) engine.Status {
334 334
 func Release(job *engine.Job) engine.Status {
335 335
 	var (
336 336
 		id                 = job.Args[0]
337
-		containerInterface = currentInterfaces[id]
337
+		containerInterface = currentInterfaces.Get(id)
338 338
 		ip                 net.IP
339 339
 		port               int
340 340
 		proto              string
... ...
@@ -383,7 +402,7 @@ func AllocatePort(job *engine.Job) engine.Status {
383 383
 		origHostPort  = job.GetenvInt("HostPort")
384 384
 		containerPort = job.GetenvInt("ContainerPort")
385 385
 		proto         = job.Getenv("Proto")
386
-		network       = currentInterfaces[id]
386
+		network       = currentInterfaces.Get(id)
387 387
 	)
388 388
 
389 389
 	if hostIP != "" {