uranusjr commented on code in PR #77489:
URL: https://github.com/apache/airflow/pull/77489#discussion_r4691952469
##########
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:
A task that returns just before the deadline (with its `return_value` XCom
already pushed inside `Execute`) can still fail if the timer fires before the
`select` runs. Check the cause inside the goroutine and send it with the result
instead. Low impact, but easy to fix.
##########
airflow-core/docs/authoring-and-scheduling/language-sdks/go.rst:
##########
@@ -477,6 +477,12 @@ Its first parameter is an ``airflow.Context``, which gives
it what Airflow provi
that runs under ``context.WithoutCancel(actx)``. A helper that takes a plain
``context.Context`` gets the
``airflow.Context`` back with ``airflow.FromContext``.
+When the task runs past its ``execution_timeout``, ``Done()`` fires with
``sdk.ErrTaskTimeout`` as the cause, so
+``errors.Is(context.Cause(actx), sdk.ErrTaskTimeout)`` tells a timeout from
Airflow stopping the task. The task then
+has ``[core] killed_task_cleanup_time`` to clean up and return. It fails with
the timeout whatever it returns, and
+retries if it has retries. A value it returns after the timeout is not pushed
as an XCom. A task that has not
+returned by then is reported as timed out and its process is stopped.
Review Comment:
Use a ref, similar to
https://github.com/apache/airflow/pull/76103#discussion_r4676718562
##########
go-sdk/pkg/execution/task_runner.go:
##########
@@ -129,6 +130,25 @@ func RunTask(
return failTask(details.TIContext.ShouldRetry, err)
}
+ run := func(ctx context.Context) any {
+ return runTask(ctx, task, details, client, logger)
+ }
+ if timeoutSeconds == nil {
+ return run(ctx)
+ }
+ return runWithExecutionTimeout(
+ ctx, *timeoutSeconds, details.TIContext.ShouldRetry, logger,
run,
+ )
+}
+
+// runTask runs the task and returns its terminal body.
Review Comment:
```suggestion
```
##########
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:
Every other sentinel in this package has no prefix; if following the
convention, this should be `TaskTimeout`.
##########
go-sdk/pkg/execution/task_runner.go:
##########
@@ -129,6 +130,25 @@ func RunTask(
return failTask(details.TIContext.ShouldRetry, err)
}
+ run := func(ctx context.Context) any {
+ return runTask(ctx, task, details, client, logger)
+ }
+ if timeoutSeconds == nil {
+ return run(ctx)
+ }
+ return runWithExecutionTimeout(
+ ctx, *timeoutSeconds, details.TIContext.ShouldRetry, logger,
run,
+ )
Review Comment:
This has a similar problem I also mentioned in
https://github.com/apache/airflow/pull/76103#discussion_r4676640191 (same
feature for TypeScript). Put the timeout behind a type, with a null object for
"no timeout", so `RunTask` has no branch.
##########
go-sdk/pkg/execution/trigger_runner_test.go:
##########
@@ -386,18 +386,21 @@ func TestTriggerDagRunPollHonorsTheStatesOfTheTask(t
*testing.T) {
assert.Equal(t, 4, len(supervisor.types())-2-1, "four polls after the
link, trigger and run_id")
}
+// sleepUntilCancelled sleeps for d, or until ctx is cancelled.
Review Comment:
```suggestion
```
--
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]