ChickenBenny commented on code in PR #77489:
URL: https://github.com/apache/airflow/pull/77489#discussion_r4696928592
##########
go-sdk/sdk/errors.go:
##########
@@ -44,3 +44,8 @@ var XComNotFound = errors.New("xcom not found")
//
// See the “Get“ method of [TaskStateStore] for an example
var TaskStateNotFound = errors.New("task state not found")
+
+// ErrTaskTimeout is the cause of a task's context when the task runs past its
execution_timeout.
+// Check errors.Is(context.Cause(ctx), ErrTaskTimeout) to tell a timeout from
a shutdown of the task,
+// for example to decide what to clean up. A task that times out fails,
whatever it returns.
+var ErrTaskTimeout = errors.New("task timed out")
Review Comment:
Renamed in db51194cfd.
##########
go-sdk/pkg/execution/execution_timeout.go:
##########
@@ -47,9 +52,64 @@ func resolveExecutionTimeout(fromContext any) (*float64,
error) {
}
// setExecutionTimeout tells the supervisor the task's execution_timeout and
restarts its clock from
-// now. The runtime does not stop the task itself: killed_task_cleanup_time
after the timeout, the
-// supervisor sends SIGTERM, which cancels the task's ctx.
+// when the supervisor receives it. The supervisor sends SIGTERM
killed_task_cleanup_time after the
+// timeout, which ends the cleanup time runWithExecutionTimeout gives the task.
func (c *CoordinatorClient) setExecutionTimeout(ctx context.Context, seconds
float64) error {
_, err := c.comm.Communicate(ctx,
genmodels.SetExecutionTimeout{TimeoutSeconds: seconds})
return err
}
+
+// runWithExecutionTimeout runs the task and returns its terminal body,
failing the task when it
+// runs past the timeout. Call it after setExecutionTimeout returns, so that
its clock starts after
+// the supervisor's, as in the Python task runner.
+//
+// At the timeout, the task's ctx is cancelled with sdk.ErrTaskTimeout as its
cause. The task then
+// has until it returns, or until the supervisor's SIGTERM cancels stop, to
clean up. Either way the
+// task fails with the timeout, so a task that returns nil after the timeout
does not succeed. A
+// task that ignores its ctx keeps running in its goroutine until the runtime
process exits after
+// reporting the timeout.
+func runWithExecutionTimeout(
+ stop context.Context,
+ seconds float64,
+ shouldRetry bool,
+ logger *slog.Logger,
+ run func(ctx context.Context) any,
+) any {
+ timeoutErr := fmt.Errorf("%w after %g seconds", sdk.ErrTaskTimeout,
seconds)
+ ctx, cancel := context.WithTimeoutCause(stop,
secondsToDuration(seconds), timeoutErr)
+ defer cancel()
+
+ done := make(chan any, 1)
+ go func() { done <- run(ctx) }()
+
+ select {
+ case result := <-done:
+ if !errors.Is(context.Cause(ctx), sdk.ErrTaskTimeout) {
Review Comment:
Fixed in c6d5f8d87b. The goroutine now cancels the context itself as soon as
the task returns, then reads the cause and sends it with the result. Whichever
cancels the context first sets the cause, so the timer can no longer change it
after the task has returned.
This is a timing race, so I didn't find a deterministic test for it. The
existing timeout tests still cover both outcomes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]