From ee1326aa5a73c1907622b41b9570832739817d2c Mon Sep 17 00:00:00 2001 From: Flavio Crisciani Date: Fri, 16 Jun 2017 16:54:18 -0700 Subject: [PATCH] Avoid race on network cleanup Use the locker to avoid the race between the network deletion and new endpoints being created Signed-off-by: Flavio Crisciani --- network.go | 8 +++++++- service_common.go | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/network.go b/network.go index eabb55b59b..0412d2e586 100644 --- a/network.go +++ b/network.go @@ -941,6 +941,9 @@ func (n *network) delete(force bool) error { id := n.id n.Unlock() + c.networkLocker.Lock(id) + defer c.networkLocker.Unlock(id) + n, err := c.getNetworkFromStore(id) if err != nil { return &UnknownNetworkError{name: name, id: id} @@ -1078,6 +1081,9 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi ep := &endpoint{name: name, generic: make(map[string]interface{}), iface: &endpointInterface{}} ep.id = stringid.GenerateRandomID() + n.ctrlr.networkLocker.Lock(n.id) + defer n.ctrlr.networkLocker.Unlock(n.id) + // Initialize ep.network with a possibly stale copy of n. We need this to get network from // store. But once we get it from store we will have the most uptodate copy possibly. ep.network = n @@ -1876,7 +1882,7 @@ func (n *network) ResolveName(req string, ipType int) ([]net.IP, bool) { } if ok && len(ipSet) > 0 { - // this maps is to avoid IP duplicates, this can happen during a transition period where 2 services are using the same IP + // this map is to avoid IP duplicates, this can happen during a transition period where 2 services are using the same IP noDup := make(map[string]bool) var ipLocal []net.IP for _, ip := range ipSet { diff --git a/service_common.go b/service_common.go index 88bcd9ddda..57ba07cb75 100644 --- a/service_common.go +++ b/service_common.go @@ -15,7 +15,7 @@ func (c *controller) addEndpointNameResolution(svcName, svcID, nID, eID, contain return err } - logrus.Debugf("addEndpointNameResolution %s %s add_service:%t", eID, svcName, addService) + logrus.Debugf("addEndpointNameResolution %s %s add_service:%t sAliases:%v tAliases:%v", eID, svcName, addService, serviceAliases, taskAliases) // Add container resolution mappings c.addContainerNameResolution(nID, eID, containerName, taskAliases, ip, method) @@ -68,7 +68,7 @@ func (c *controller) deleteEndpointNameResolution(svcName, svcID, nID, eID, cont return err } - logrus.Debugf("deleteEndpointNameResolution %s %s rm_service:%t suppress:%t", eID, svcName, rmService, multipleEntries) + logrus.Debugf("deleteEndpointNameResolution %s %s rm_service:%t suppress:%t sAliases:%v tAliases:%v", eID, svcName, rmService, multipleEntries, serviceAliases, taskAliases) // Delete container resolution mappings c.delContainerNameResolution(nID, eID, containerName, taskAliases, ip, method)