Browse code

Fix race on reading endpoint data

Race is with its cleanup.

Signed-off-by: Alexander Morozov <lk4d4@docker.com>

Alexander Morozov authored on 2016/01/09 06:41:28
Showing 1 changed files
... ...
@@ -12,6 +12,7 @@ import (
12 12
 	"github.com/docker/docker/pkg/graphdb"
13 13
 	"github.com/docker/engine-api/types"
14 14
 	"github.com/docker/engine-api/types/filters"
15
+	networktypes "github.com/docker/engine-api/types/network"
15 16
 	"github.com/docker/go-connections/nat"
16 17
 )
17 18
 
... ...
@@ -351,7 +352,30 @@ func (daemon *Daemon) transformContainer(container *container.Container, ctx *li
351 351
 	newC.Created = container.Created.Unix()
352 352
 	newC.Status = container.State.String()
353 353
 	newC.HostConfig.NetworkMode = string(container.HostConfig.NetworkMode)
354
-	newC.NetworkSettings = &types.SummaryNetworkSettings{container.NetworkSettings.Networks}
354
+	// copy networks to avoid races
355
+	networks := make(map[string]*networktypes.EndpointSettings)
356
+	for name, network := range container.NetworkSettings.Networks {
357
+		if network == nil {
358
+			continue
359
+		}
360
+		networks[name] = &networktypes.EndpointSettings{
361
+			EndpointID:          network.EndpointID,
362
+			Gateway:             network.Gateway,
363
+			IPAddress:           network.IPAddress,
364
+			IPPrefixLen:         network.IPPrefixLen,
365
+			IPv6Gateway:         network.IPv6Gateway,
366
+			GlobalIPv6Address:   network.GlobalIPv6Address,
367
+			GlobalIPv6PrefixLen: network.GlobalIPv6PrefixLen,
368
+			MacAddress:          network.MacAddress,
369
+		}
370
+		if network.IPAMConfig != nil {
371
+			networks[name].IPAMConfig = &networktypes.EndpointIPAMConfig{
372
+				IPv4Address: network.IPAMConfig.IPv4Address,
373
+				IPv6Address: network.IPAMConfig.IPv6Address,
374
+			}
375
+		}
376
+	}
377
+	newC.NetworkSettings = &types.SummaryNetworkSettings{Networks: networks}
355 378
 
356 379
 	newC.Ports = []types.Port{}
357 380
 	for port, bindings := range container.NetworkSettings.Ports {