solver: fix nil result handling

Before this, you could return worker ref results from ops that have nil
refs but once they were attempted to be used, various nil exceptions
would get hit. Now, those cases should be handled.

Signed-off-by: Erik Sipsma <erik@sipsma.dev>
master
Erik Sipsma 2021-11-26 18:34:40 -08:00
parent 8c1e411d01
commit 1835ef5118
6 changed files with 21 additions and 4 deletions

View File

@ -136,7 +136,7 @@ func (e *exporter) ExportTo(ctx context.Context, t CacheExporterTarget, opt Cach
return nil, err
}
res.Release(context.TODO())
if remote == nil {
if remote == nil && len(remotes) > 0 {
remote, remotes = remotes[0], remotes[1:] // pop the first element
}
if opt.CompressionOpt != nil {

View File

@ -77,7 +77,9 @@ func (d *diffOp) Exec(ctx context.Context, g session.Group, inputs []solver.Resu
return nil, errors.Errorf("invalid lower reference for diff op %T", lowerInp.Sys())
}
lowerRef = wref.ImmutableRef
lowerRefID = wref.ImmutableRef.ID()
if lowerRef != nil {
lowerRefID = wref.ImmutableRef.ID()
}
} else {
return nil, errors.New("invalid nil lower input for diff op")
}
@ -93,7 +95,9 @@ func (d *diffOp) Exec(ctx context.Context, g session.Group, inputs []solver.Resu
return nil, errors.Errorf("invalid upper reference for diff op %T", upperInp.Sys())
}
upperRef = wref.ImmutableRef
upperRefID = wref.ImmutableRef.ID()
if upperRef != nil {
upperRefID = wref.ImmutableRef.ID()
}
} else {
return nil, errors.New("invalid nil upper input for diff op")
}

View File

@ -70,6 +70,9 @@ func (m *mergeOp) Exec(ctx context.Context, g session.Group, inputs []solver.Res
if !ok {
return nil, errors.Errorf("invalid reference for merge %T", inp.Sys())
}
if wref.ImmutableRef == nil {
continue
}
refs[index] = wref.ImmutableRef
ids[index] = wref.ImmutableRef.ID()
index++

View File

@ -221,6 +221,11 @@ func (w *Worker) LoadRef(ctx context.Context, id string, hidden bool) (cache.Imm
if hidden {
opts = append(opts, cache.NoUpdateLastUsed)
}
if id == "" {
// results can have nil refs if they are optimized out to be equal to scratch,
// i.e. Diff(A,A) == scratch
return nil, nil
}
ref, err := w.CacheMgr.Get(ctx, id, opts...)
var needsRemoteProviders cache.NeedsRemoteProvidersError

View File

@ -75,7 +75,9 @@ func (s *cacheResultStorage) LoadRemotes(ctx context.Context, res solver.CacheRe
if err != nil {
return nil, err
}
defer ref.Release(context.TODO())
if ref != nil {
defer ref.Release(context.TODO())
}
wref := WorkerRef{ref, w}
all := true // load as many compression blobs as possible
if compressionopt == nil {

View File

@ -34,6 +34,9 @@ func (wr *WorkerRef) GetRemotes(ctx context.Context, createIfNeeded bool, compre
}); ok {
return w.GetRemotes(ctx, wr.ImmutableRef, createIfNeeded, compressionopt, all, g)
}
if wr.ImmutableRef == nil {
return nil, nil
}
return wr.ImmutableRef.GetRemotes(ctx, createIfNeeded, compressionopt, all, g)
}