From 826f6f07031abc4dea6f71ed69d33a4e0789ee11 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Thu, 16 Jun 2016 16:08:15 -0700 Subject: [PATCH] Fix removing containers on leaving from pending state Signed-off-by: Tonis Tiigi --- daemon/cluster/cluster.go | 9 ++++---- integration-cli/docker_api_swarm_test.go | 26 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/daemon/cluster/cluster.go b/daemon/cluster/cluster.go index b8e56432a3..1b2d20d5cd 100644 --- a/daemon/cluster/cluster.go +++ b/daemon/cluster/cluster.go @@ -379,10 +379,11 @@ func (c *Cluster) Leave(force bool) error { if err := node.Stop(ctx); err != nil && !strings.Contains(err.Error(), "context canceled") { return err } - nodeID := node.NodeID() - for _, id := range c.config.Backend.ListContainersForNode(nodeID) { - if err := c.config.Backend.ContainerRm(id, &apitypes.ContainerRmConfig{ForceRemove: true}); err != nil { - logrus.Errorf("error removing %v: %v", id, err) + if nodeID := node.NodeID(); nodeID != "" { + for _, id := range c.config.Backend.ListContainersForNode(nodeID) { + if err := c.config.Backend.ContainerRm(id, &apitypes.ContainerRmConfig{ForceRemove: true}); err != nil { + logrus.Errorf("error removing %v: %v", id, err) + } } } c.Lock() diff --git a/integration-cli/docker_api_swarm_test.go b/integration-cli/docker_api_swarm_test.go index d76c6ac0e4..fee1119682 100644 --- a/integration-cli/docker_api_swarm_test.go +++ b/integration-cli/docker_api_swarm_test.go @@ -550,6 +550,32 @@ func (s *DockerSwarmSuite) TestApiSwarmLeaveRemovesContainer(c *check.C) { c.Assert(id, checker.HasPrefix, strings.TrimSpace(id2)) } +// #23629 +func (s *DockerSwarmSuite) TestApiSwarmLeaveOnPendingJoin(c *check.C) { + s.AddDaemon(c, true, true) + d2 := s.AddDaemon(c, false, false) + + id, err := d2.Cmd("run", "-d", "busybox", "top") + c.Assert(err, checker.IsNil) + id = strings.TrimSpace(id) + + go d2.Join("nosuchhost:1234", "", "", false) // will block on pending state + + time.Sleep(1 * time.Second) + + info, err := d2.info() + c.Assert(err, checker.IsNil) + c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStatePending) + + c.Assert(d2.Leave(true), checker.IsNil) + + waitAndAssert(c, defaultReconciliationTimeout, d2.checkActiveContainerCount, checker.Equals, 1) + + id2, err := d2.Cmd("ps", "-q") + c.Assert(err, checker.IsNil) + c.Assert(id, checker.HasPrefix, strings.TrimSpace(id2)) +} + func (s *DockerSwarmSuite) TestApiSwarmManagerRestore(c *check.C) { d1 := s.AddDaemon(c, true, true)