From fbad4da51f25db6ee522592105e8c787065fec8b Mon Sep 17 00:00:00 2001 From: Abhinandan Prativadi Date: Mon, 24 Jul 2017 11:50:02 -0700 Subject: [PATCH] Ensuring endpoint resources are freed even on delete failures Came across a code path where we might not be releasing ip address assigned to an endpoint if we have a failure with deleteEndpoint. Even if there is a failure it is better to release the resource rather than holding them. This might lead to issues where ip never gets released even though the container has exited and the only way of recovery is a reload. Signed-off-by: Abhinandan Prativadi --- endpoint.go | 2 +- sandbox.go | 16 +++++----------- sandbox_store.go | 2 +- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/endpoint.go b/endpoint.go index 111b747352..4af4f492be 100644 --- a/endpoint.go +++ b/endpoint.go @@ -822,7 +822,7 @@ func (ep *endpoint) Delete(force bool) error { } } - if err = n.getController().deleteFromStore(ep); err != nil { + if err = n.getController().deleteFromStore(ep); err != nil && !force { return err } diff --git a/sandbox.go b/sandbox.go index 9454c5c286..e0f693a1ac 100644 --- a/sandbox.go +++ b/sandbox.go @@ -182,10 +182,10 @@ func (sb *sandbox) Statistics() (map[string]*types.InterfaceStatistics, error) { } func (sb *sandbox) Delete() error { - return sb.delete(false) + return sb.delete() } -func (sb *sandbox) delete(force bool) error { +func (sb *sandbox) delete() error { sb.Lock() if sb.inDelete { sb.Unlock() @@ -208,10 +208,10 @@ func (sb *sandbox) delete(force bool) error { retain := false for _, ep := range sb.getConnectedEndpoints() { // gw network endpoint detach and removal are automatic - if ep.endpointInGWNetwork() && !force { + if ep.endpointInGWNetwork() { continue } - // Retain the sanbdox if we can't obtain the network from store. + // Retain the sandbox if we can't obtain the network from store. if _, err := c.getNetworkFromStore(ep.getNetwork().ID()); err != nil { if c.isDistributedControl() { retain = true @@ -220,13 +220,7 @@ func (sb *sandbox) delete(force bool) error { continue } - if !force { - if err := ep.Leave(sb); err != nil { - logrus.Warnf("Failed detaching sandbox %s from endpoint %s: %v\n", sb.ID(), ep.ID(), err) - } - } - - if err := ep.Delete(force); err != nil { + if err := ep.Delete(true); err != nil { logrus.Warnf("Failed deleting endpoint %s: %v\n", ep.ID(), err) } } diff --git a/sandbox_store.go b/sandbox_store.go index 38b2bd7e8b..e46a6a0d32 100644 --- a/sandbox_store.go +++ b/sandbox_store.go @@ -279,7 +279,7 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) { if _, ok := activeSandboxes[sb.ID()]; !ok { logrus.Infof("Removing stale sandbox %s (%s)", sb.id, sb.containerID) - if err := sb.delete(true); err != nil { + if err := sb.delete(); err != nil { logrus.Errorf("Failed to delete sandbox %s while trying to cleanup: %v", sb.id, err) } continue