Skip to content

Commit

Permalink
fix cancellation error not being detected and erroneously cached
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi committed Jun 23, 2022
1 parent a6a114a commit e727469
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion frontend/gateway/gateway.go
Expand Up @@ -278,7 +278,7 @@ func (gf *gatewayFrontend) Solve(ctx context.Context, llbBridge frontend.Fronten
err = w.Executor().Run(ctx, "", mountWithSession(rootFS, session.NewGroup(sid)), mnts, executor.ProcessInfo{Meta: meta, Stdin: lbf.Stdin, Stdout: lbf.Stdout, Stderr: os.Stderr}, nil)

if err != nil {
if errdefs.IsCanceled(err) && lbf.isErrServerClosed {
if errdefs.IsCanceled(ctx, err) && lbf.isErrServerClosed {
err = errors.Errorf("frontend grpc server closed unexpectedly")
}
// An existing error (set via Return rpc) takes
Expand Down
18 changes: 16 additions & 2 deletions solver/errdefs/context.go
Expand Up @@ -3,11 +3,25 @@ package errdefs
import (
"context"
"errors"
"strings"

"github.com/moby/buildkit/util/grpcerrors"
"google.golang.org/grpc/codes"
)

func IsCanceled(err error) bool {
return errors.Is(err, context.Canceled) || grpcerrors.Code(err) == codes.Canceled
func IsCanceled(ctx context.Context, err error) bool {
if errors.Is(err, context.Canceled) || grpcerrors.Code(err) == codes.Canceled {
return true
}
// grpc does not set cancel correctly when stream gets cancelled and then Recv is called
if ctx.Err() == context.Canceled {
// when this error comes from containerd it is not typed at all, just concatenated string
if strings.Contains(err.Error(), "EOF") {
return true
}
if strings.Contains(err.Error(), context.Canceled.Error()) {
return true
}
}
return false
}
7 changes: 3 additions & 4 deletions solver/jobs.go
Expand Up @@ -3,7 +3,6 @@ package solver
import (
"context"
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -704,7 +703,7 @@ func (s *sharedOp) CalcSlowCache(ctx context.Context, index Index, p PreprocessF
if err != nil {
select {
case <-ctx.Done():
if strings.Contains(err.Error(), context.Canceled.Error()) {
if errdefs.IsCanceled(ctx, err) {
complete = false
releaseError(err)
err = errors.Wrap(ctx.Err(), err.Error())
Expand Down Expand Up @@ -770,7 +769,7 @@ func (s *sharedOp) CacheMap(ctx context.Context, index int) (resp *cacheMapResp,
if err != nil {
select {
case <-ctx.Done():
if strings.Contains(err.Error(), context.Canceled.Error()) {
if errdefs.IsCanceled(ctx, err) {
complete = false
releaseError(err)
err = errors.Wrap(ctx.Err(), err.Error())
Expand Down Expand Up @@ -846,7 +845,7 @@ func (s *sharedOp) Exec(ctx context.Context, inputs []Result) (outputs []Result,
if err != nil {
select {
case <-ctx.Done():
if strings.Contains(err.Error(), context.Canceled.Error()) {
if errdefs.IsCanceled(ctx, err) {
complete = false
releaseError(err)
err = errors.Wrap(ctx.Err(), err.Error())
Expand Down
3 changes: 1 addition & 2 deletions solver/llbsolver/bridge.go
Expand Up @@ -3,7 +3,6 @@ package llbsolver
import (
"context"
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -290,7 +289,7 @@ func (rp *resultProxy) Result(ctx context.Context) (res solver.CachedResult, err
if err != nil {
select {
case <-ctx.Done():
if strings.Contains(err.Error(), context.Canceled.Error()) {
if errdefs.IsCanceled(ctx, err) {
return v, err
}
default:
Expand Down
2 changes: 1 addition & 1 deletion source/containerimage/pull.go
Expand Up @@ -272,7 +272,7 @@ func (p *puller) CacheKey(ctx context.Context, g session.Group, index int) (cach
return nil, p.cacheKeyErr
}
defer func() {
if !errdefs.IsCanceled(err) {
if !errdefs.IsCanceled(ctx, err) {
p.cacheKeyErr = err
}
}()
Expand Down

0 comments on commit e727469

Please sign in to comment.