Skip to content

Commit

Permalink
fix wait entrypoint cancellation error log output.
Browse files Browse the repository at this point in the history
context canceled should be treated as normal completion

Signed-off-by: chengjoey <[email protected]>
  • Loading branch information
chengjoey authored and tekton-robot committed Oct 25, 2023
1 parent c377744 commit 97184c3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func main() {
log.Print(err.Error())
os.Exit(1)
case entrypoint.ContextError:
if errors.Is(err, entrypoint.ErrContextCanceled) {
if entrypoint.IsContextCanceledError(err) {
log.Print("Step was cancelled")
// use the SIGKILL signal to distinguish normal exit programs, just like kill -9 PID
os.Exit(int(syscall.SIGKILL))
Expand Down
12 changes: 11 additions & 1 deletion pkg/entrypoint/entrypointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ var (
ErrContextCanceled = ContextError(context.Canceled.Error())
)

// IsContextDeadlineError determine whether the error is context deadline
func IsContextDeadlineError(err error) bool {
return errors.Is(err, ErrContextDeadlineExceeded)
}

// IsContextCanceledError determine whether the error is context canceled
func IsContextCanceledError(err error) bool {
return errors.Is(err, ErrContextCanceled)
}

// Entrypointer holds fields for running commands with redirected
// entrypoints.
type Entrypointer struct {
Expand Down Expand Up @@ -174,7 +184,7 @@ func (e Entrypointer) Go() error {
defer cancel()
// start a goroutine to listen for cancellation file
go func() {
if err := e.waitingCancellation(ctx, cancel); err != nil {
if err := e.waitingCancellation(ctx, cancel); err != nil && (!IsContextCanceledError(err) && !IsContextDeadlineError(err)) {
logger.Error("Error while waiting for cancellation", zap.Error(err))
}
}()
Expand Down
22 changes: 22 additions & 0 deletions pkg/entrypoint/entrypointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,28 @@ func TestEntrypointerStopOnCancel(t *testing.T) {
}
}

func TestIsContextDeadlineError(t *testing.T) {
ctxErr := ContextError(context.DeadlineExceeded.Error())
if !IsContextDeadlineError(ctxErr) {
t.Errorf("expected context deadline error, got %v", ctxErr)
}
normalErr := ContextError("normal error")
if IsContextDeadlineError(normalErr) {
t.Errorf("expected normal error, got %v", normalErr)
}
}

func TestIsContextCanceledError(t *testing.T) {
ctxErr := ContextError(context.Canceled.Error())
if !IsContextCanceledError(ctxErr) {
t.Errorf("expected context canceled error, got %v", ctxErr)
}
normalErr := ContextError("normal error")
if IsContextCanceledError(normalErr) {
t.Errorf("expected normal error, got %v", normalErr)
}
}

type fakeWaiter struct {
sync.Mutex
waited []string
Expand Down

0 comments on commit 97184c3

Please sign in to comment.