From 1d9546fc62c559dbcbb3dbdce40318fb7c4d67a2 Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Wed, 26 Jul 2017 16:43:10 -0700 Subject: [PATCH] container: Fix Delete on nonexistent container Delete needs to release names related to a container even if that container isn't present in the db. However, slightly overzealous error checking causes the transaction to get rolled back. Ignore the error from Delete on the container itself, since it may not be present. Signed-off-by: Aaron Lehmann --- container/view.go | 6 +++--- container/view_test.go | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/container/view.go b/container/view.go index e865e4d5df..4b6c70259b 100644 --- a/container/view.go +++ b/container/view.go @@ -168,9 +168,9 @@ func (db *memDB) Delete(c *Container) error { txn.Delete(memdbNamesTable, nameAssociation{name: name}) } - if err := txn.Delete(memdbContainersTable, NewBaseContainer(c.ID, c.Root)); err != nil { - return err - } + // Ignore error - the container may not actually exist in the + // db, but we still need to clean up associated names. + txn.Delete(memdbContainersTable, NewBaseContainer(c.ID, c.Root)) return nil }) } diff --git a/container/view_test.go b/container/view_test.go index 09ba343830..4535a8fade 100644 --- a/container/view_test.go +++ b/container/view_test.go @@ -150,4 +150,12 @@ func TestNames(t *testing.T) { view = db.Snapshot() assert.Equal(t, map[string][]string{"containerid1": {"name1", "name3", "name4"}, "containerid4": {"name2"}}, view.GetAllNames()) + + // Release containerid1's names with Delete even though no container exists + assert.NoError(t, db.Delete(&Container{ID: "containerid1"})) + + // Reusing one of those names should work + assert.NoError(t, db.ReserveName("name1", "containerid4")) + view = db.Snapshot() + assert.Equal(t, map[string][]string{"containerid4": {"name1", "name2"}}, view.GetAllNames()) }