ChickenBenny commented on code in PR #77489:
URL: https://github.com/apache/airflow/pull/77489#discussion_r4756564999
##########
go-sdk/pkg/execution/execution_timeout.go:
##########
@@ -19,37 +19,133 @@ package execution
import (
"context"
+ "errors"
"fmt"
+ "log/slog"
+ "math"
+ "runtime/debug"
+ "time"
+ "github.com/apache/airflow/go-sdk/internal/bundle"
"github.com/apache/airflow/go-sdk/pkg/execution/genmodels"
+ "github.com/apache/airflow/go-sdk/sdk"
)
-// resolveExecutionTimeout returns the task's execution_timeout in seconds
from ti_context, or nil
-// when the task has no timeout. The coordinator has already filled in the
worker's default.
-// As in the Python task runner, zero means no timeout and a negative value
has already elapsed.
-func resolveExecutionTimeout(fromContext any) (*float64, error) {
+// resolveExecutionTimeout reads ti_context.execution_timeout. As in Python,
zero means no timeout
+// and a negative value has already elapsed. It also returns the task's
context, derived from stop,
+// which the timeout cancels with sdk.TaskTimeout as the cause.
+func resolveExecutionTimeout(
+ stop context.Context,
+ fromContext any,
+ logger *slog.Logger,
+ client *CoordinatorClient,
+) (context.Context, bundle.ExecutionTimeout, error) {
if fromContext == nil {
- return nil, nil
+ return stop, bundle.NoExecutionTimeout, nil
}
seconds, ok := ifaceFloat(fromContext)
switch {
case !ok:
- return nil, fmt.Errorf(
+ return nil, nil, fmt.Errorf(
"ti_context.execution_timeout has unexpected wire shape
%T",
fromContext,
)
case seconds == 0:
- return nil, nil
+ return stop, bundle.NoExecutionTimeout, nil
case seconds < 0:
- return nil, fmt.Errorf("execution_timeout of %g seconds has
already elapsed", seconds)
+ return nil, nil, fmt.Errorf("execution_timeout of %g seconds
has already elapsed", seconds)
}
- return &seconds, nil
+ ctx, cancel := context.WithCancelCause(stop)
+ return ctx, enabledExecutionTimeout{
+ seconds: seconds,
+ logger: logger,
+ client: client,
+ stop: stop,
+ ctx: ctx,
+ cancel: cancel,
+ }, nil
}
-// 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.
-func (c *CoordinatorClient) setExecutionTimeout(ctx context.Context, seconds
float64) error {
- _, err := c.comm.Communicate(ctx,
genmodels.SetExecutionTimeout{TimeoutSeconds: seconds})
- return err
+type enabledExecutionTimeout struct {
+ seconds float64
+ logger *slog.Logger
+ client *CoordinatorClient
+ stop context.Context
+ ctx context.Context
+ cancel context.CancelCauseFunc
+}
+
+// Run arms the supervisor, which sends SIGTERM killed_task_cleanup_time after
the timeout, and
+// does not call fn if the supervisor rejects that. At the timeout it cancels
the task's ctx
+// with sdk.TaskTimeout, then waits until fn returns or the supervisor's
SIGTERM cancels stop.
+// A panic in fn is raised again in the caller's goroutine.
+func (t enabledExecutionTimeout) Run(fn func()) error {
+ _, err := t.client.comm.Communicate(
+ t.stop,
+ genmodels.SetExecutionTimeout{TimeoutSeconds: t.seconds},
+ )
+ if err != nil {
+ return fmt.Errorf("applying execution_timeout: %w", err)
+ }
+
+ timeoutErr := fmt.Errorf("%w after %g seconds", sdk.TaskTimeout,
t.seconds)
+ timer := time.AfterFunc(secondsToDuration(t.seconds), func() {
t.cancel(timeoutErr) })
+ done := make(chan *taskPanic, 1)
+ go func() {
+ defer func() {
+ if r := recover(); r != nil {
+ done <- &taskPanic{value: r, stack:
debug.Stack()}
+ return
+ }
+ done <- nil
+ }()
+ fn()
+ }()
+
+ select {
+ case p := <-done:
+ // Stop fails only when the timer has fired, so the task cannot
time out after this.
+ if timer.Stop() {
+ p.raise()
+ return nil
+ }
+ case <-t.ctx.Done():
+ if !errors.Is(context.Cause(t.ctx), sdk.TaskTimeout) {
+ // A SIGTERM before the timeout: the task's own outcome
stands.
+ timer.Stop()
+ (<-done).raise()
+ return nil
+ }
+ t.logger.Warn("Task reached its execution_timeout; waiting for
it to stop",
+ "timeout_seconds", t.seconds,
+ )
+ select {
+ case <-done:
Review Comment:
Fixed in 91e5a3488c. A panic in `fn` after the timeout is now logged as
`Task panicked after its execution_timeout`, with its value and stack, and the
task still fails as timed out. It covers all three ways it can happen:
- `fn` returns as the timer fires.
- `fn` returns while `Run` waits after the timeout.
- `fn` panics after `Run` has already returned at the supervisor's SIGTERM.
A goroutine waits for it until the process exits.
`TestEnabledExecutionTimeoutLogsAPanicAfterTheTimeout` covers the first two;
which of them it takes depends on the order `Run` sees the timer and the panic.
`TestEnabledExecutionTimeoutLogsAPanicAfterTheSupervisorStop` covers the last
one.
--
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]