From 8f995302217b8634391f8f93addeb3bb3fd7864e Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Tue, 25 Aug 2020 14:46:35 -0500 Subject: [PATCH] make image parent check more robust Follow up on issue #7444 and make the parent checks more robust. We can end up with an incoherent storage when, for instance, a build has been killed. Backport of commit a6f8586 and commit 238abf6. Squashed for easier tracking and referencing. Fixes: #7444 Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1876576 Signed-off-by: Brent Baude Signed-off-by: Valentin Rothberg --- libpod/image/image.go | 8 ++++++++ libpod/image/layer_tree.go | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/libpod/image/image.go b/libpod/image/image.go index 6a87380c01..33d6a1e546 100644 --- a/libpod/image/image.go +++ b/libpod/image/image.go @@ -1242,6 +1242,14 @@ func areParentAndChild(parent, child *imgspecv1.Image) bool { // the child and candidate parent should share all of the // candidate parent's diff IDs, which together would have // controlled which layers were used + + // Both, child and parent, may be nil when the storage is left in an + // incoherent state. Issue #7444 describes such a case when a build + // has been killed. + if child == nil || parent == nil { + return false + } + if len(parent.RootFS.DiffIDs) > len(child.RootFS.DiffIDs) { return false } diff --git a/libpod/image/layer_tree.go b/libpod/image/layer_tree.go index 3699655fde..18101575e6 100644 --- a/libpod/image/layer_tree.go +++ b/libpod/image/layer_tree.go @@ -32,7 +32,9 @@ func (t *layerTree) toOCI(ctx context.Context, i *Image) (*ociv1.Image, error) { oci, exists := t.ociCache[i.ID()] if !exists { oci, err = i.ociv1Image(ctx) - t.ociCache[i.ID()] = oci + if err == nil { + t.ociCache[i.ID()] = oci + } } return oci, err }