Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion workspace/task/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (r *Runner) Run(ctx context.Context) {
task.mu.Unlock()

err := task.Action(ctx)
if err == nil && ctx.Err() != nil {
err = ctx.Err()
}

task.mu.Lock()
task.FinishedAt = time.Now()
Expand All @@ -66,4 +69,4 @@ func (r *Runner) Run(ctx context.Context) {
}(t)
}
wg.Wait()
}
}
22 changes: 21 additions & 1 deletion workspace/task/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,24 @@ func TestRunner_Run_Cancel(t *testing.T) {
if err := t1.GetErr(); err != context.Canceled {
t.Errorf("expected t1 error to be context.Canceled, got %v", err)
}
}
}

func TestRunner_Run_CancelDuringAction(t *testing.T) {
runner := NewRunner()
ctx, cancel := context.WithCancel(context.Background())

task := NewTask("cancel-during-action", func(ctx context.Context) error {
cancel()
return nil
})

runner.AddTask(task)
runner.Run(ctx)

if state := task.GetState(); state != StateFailed {
t.Errorf("expected task to fail after context cancellation, got %s", state)
}
if err := task.GetErr(); err != context.Canceled {
t.Errorf("expected task error to be context.Canceled, got %v", err)
}
}