Skip to content

Commit 786d74c

Browse files
committed
[cache] Fix descriptor duplication in getAvailableBlobs
The walkBlob function was called with a function having a side effect: appending a descriptor to a list. However, the function could be called multiple times for the same descriptor (once in `walkBlob` and once in `walkBlobVariantsOnly` in that case) leading to duplicate entries in the final list. Instead, we will check each time we want to add an element, that it's not already in the list using slice.ContainsFunc. This should be fine from a performance perspective as the list is small (at most 2 compressions in the vast majority of cases). Signed-off-by: Baptiste Girard-Carrabin <[email protected]>
1 parent 141a4a6 commit 786d74c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

cache/remote.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,16 @@ func getAvailableBlobs(ctx context.Context, cs content.Store, chain *solver.Remo
114114
}
115115
var descs []ocispecs.Descriptor
116116
if err := walkBlob(ctx, cs, target, func(desc ocispecs.Descriptor) bool {
117-
descs = append(descs, desc)
117+
// Nothing prevents this function from being called multiple times for the same descriptor.
118+
// So we need to make sure we don't add the same descriptor again.
119+
// Looping over the list is preferable:
120+
// 1. to avoid using a map, which don't preserve the order of descriptors,
121+
// 2. descs will have a length the number of compression variants for a blob, which is usually very small
122+
if !slices.ContainsFunc(descs, func(d ocispecs.Descriptor) bool {
123+
return d.Digest == desc.Digest
124+
}) {
125+
descs = append(descs, desc)
126+
}
118127
return true
119128
}); err != nil {
120129
bklog.G(ctx).WithError(err).Warn("failed to walk variant blob") // is not a critical error at this moment.

0 commit comments

Comments
 (0)