Skip to content

Commit

Permalink
Merge pull request moby#5343 from tonistiigi/http-digest-mismatch
Browse files Browse the repository at this point in the history
http: avoid possible digest mismatch error
  • Loading branch information
tonistiigi authored Oct 7, 2024
2 parents 605c469 + 3b35fc3 commit 8c313f9
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions source/http/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/source"
srctypes "github.com/moby/buildkit/source/types"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/tracing"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
Expand Down Expand Up @@ -192,7 +193,12 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, g session.Group, inde
// if metaDigest := getMetaDigest(si); metaDigest == hs.formatCacheKey("") {
if etag := md.getETag(); etag != "" {
if dgst := md.getHTTPChecksum(); dgst != "" {
m[etag] = md
// check that ref still exists
ref, err := hs.cache.Get(ctx, md.ID(), nil)
if err == nil {
m[etag] = md
defer ref.Release(context.WithoutCancel(ctx))
}
}
}
// }
Expand Down Expand Up @@ -235,6 +241,7 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, g session.Group, inde
hs.refID = md.ID()
dgst := md.getHTTPChecksum()
if dgst != "" {
hs.cacheKey = dgst
modTime := md.getHTTPModTime()
resp.Body.Close()
return hs.formatCacheKey(getFileName(hs.src.URL, hs.src.Filename, resp), dgst, modTime).String(), dgst.String(), nil, true, nil
Expand Down Expand Up @@ -275,8 +282,10 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, g session.Group, inde
if dgst == "" {
return "", "", nil, false, errors.Errorf("invalid metadata change")
}
hs.cacheKey = dgst
modTime := md.getHTTPModTime()
resp.Body.Close()

return hs.formatCacheKey(getFileName(hs.src.URL, hs.src.Filename, resp), dgst, modTime).String(), dgst.String(), nil, true, nil
}

Expand Down Expand Up @@ -421,7 +430,9 @@ func (hs *httpSourceHandler) save(ctx context.Context, resp *http.Response, s se
func (hs *httpSourceHandler) Snapshot(ctx context.Context, g session.Group) (cache.ImmutableRef, error) {
if hs.refID != "" {
ref, err := hs.cache.Get(ctx, hs.refID, nil)
if err == nil {
if err != nil {
bklog.G(ctx).WithError(err).Warnf("failed to get HTTP snapshot for ref %s (%s)", hs.refID, hs.src.URL)
} else {
return ref, nil
}
}
Expand Down

0 comments on commit 8c313f9

Please sign in to comment.