Skip to content
This repository was archived by the owner on May 8, 2024. It is now read-only.

Commit 28f4c3d

Browse files
authored
Don't hide cancellation reason (#363)
* Don't hide cancellation reason Otherwise, we assume a task has timeout when in reality it could've been externally interrupted. * revert log * Less changes * Removed whitespace
1 parent 5af6378 commit 28f4c3d

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

internal/executor/executor.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type StepResult struct {
5252

5353
var (
5454
ErrStepExit = errors.New("executor step requested to terminate execution")
55+
ErrTimedOut = errors.New("timed out")
5556
)
5657

5758
func NewExecutor(
@@ -215,7 +216,7 @@ func (executor *Executor) RunBuild(ctx context.Context) {
215216
// Normal timeout-bounded context
216217
timeout := time.Duration(response.TimeoutInSeconds) * time.Second
217218

218-
timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, timeout)
219+
timeoutCtx, timeoutCtxCancel := context.WithTimeoutCause(ctx, timeout, ErrTimedOut)
219220
defer timeoutCtxCancel()
220221

221222
// Like timeout-bounded context, but extended by 5 minutes
@@ -500,7 +501,7 @@ func (executor *Executor) performStep(ctx context.Context, currentStep *api.Comm
500501
signaledToExit = ws.Signaled()
501502
}
502503
}
503-
if err == TimeOutError {
504+
if errors.Is(err, ErrTimedOut) {
504505
signaledToExit = false
505506
}
506507
case *api.Command_BackgroundScriptInstruction:

internal/executor/shell.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"os/exec"
1414
"os/signal"
15+
"strings"
1516
"syscall"
1617
"time"
1718
)
@@ -23,8 +24,6 @@ type ShellOutputWriter struct {
2324
handler ShellOutputHandler
2425
}
2526

26-
var TimeOutError = errors.New("timed out")
27-
2827
func (writer ShellOutputWriter) Write(bytes []byte) (int, error) {
2928
return writer.handler(bytes)
3029
}
@@ -61,15 +60,16 @@ func ShellCommandsAndWait(
6160

6261
select {
6362
case <-ctx.Done():
64-
handler([]byte("\nTimed out!"))
63+
errorMessage := fmt.Sprintf("%v!", context.Cause(ctx))
64+
handler([]byte("\n" + strings.ToUpper(errorMessage[:1]) + errorMessage[1:]))
6565

6666
processdumper.Dump()
6767

6868
if err = sc.kill(); err != nil {
6969
handler([]byte(fmt.Sprintf("\nFailed to kill a timed out shell session: %s", err)))
7070
}
7171

72-
return cmd, TimeOutError
72+
return cmd, context.Cause(ctx)
7373
case <-done:
7474
var forcePiperClosure bool
7575

internal/executor/shell_subunix_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// the shell spawned in ShellCommandsAndGetOutput() has been placed into, thus killing
1717
// it's children processes.
1818
func TestProcessGroupTermination(t *testing.T) {
19-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
19+
ctx, cancel := context.WithTimeoutCause(context.Background(), 1*time.Second, ErrTimedOut)
2020
defer cancel()
2121

2222
success, output := ShellCommandsAndGetOutput(ctx, []string{"sleep 86400 & echo target PID is $! ; sleep 60"}, nil)

internal/executor/shell_unix_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func Test_ShellCommands_CustomWorkingDir_Unix(t *testing.T) {
112112
}
113113

114114
func Test_ShellCommands_Timeout_Unix(t *testing.T) {
115-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
115+
ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, ErrTimedOut)
116116
defer cancel()
117117

118118
_, output := ShellCommandsAndGetOutput(ctx, []string{"sleep 60"}, nil)
@@ -124,7 +124,7 @@ func Test_ShellCommands_Timeout_Unix(t *testing.T) {
124124
}
125125

126126
func TestChildrenProcessesAreCancelled(t *testing.T) {
127-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
127+
ctx, cancel := context.WithTimeoutCause(context.Background(), time.Second*5, ErrTimedOut)
128128
defer cancel()
129129

130130
success, output := ShellCommandsAndGetOutput(ctx, []string{"sleep 60 & sleep 10"}, nil)

internal/executor/shell_windows_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestMain(m *testing.M) {
5353
// TestProcessGroupTermination ensures that we terminate all processes we've automatically
5454
// tainted by assigning a job object to a shell spawned in ShellCommandsAndGetOutput().
5555
func TestJobObjectTermination(t *testing.T) {
56-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
56+
ctx, cancel := context.WithTimeoutCause(context.Background(), 10*time.Second, ErrTimedOut)
5757
defer cancel()
5858

5959
success, output := ShellCommandsAndGetOutput(ctx, []string{os.Args[0]},

0 commit comments

Comments
 (0)