ChickenBenny commented on code in PR #77489:
URL: https://github.com/apache/airflow/pull/77489#discussion_r4756557964


##########
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)

Review Comment:
   Done in b631ad3de7. `RunTask` now derives the task's context and defers 
`cancel(nil)`, with or without a timeout, and `resolveExecutionTimeout` returns 
`(bundle.ExecutionTimeout, error)` again.
   
   It takes one more argument than your snippet: the parent context, renamed 
`shutdown` in bd16324133. At the timeout, `Run` cancels the task's context 
itself. After that it can only see the supervisor's SIGTERM by waiting on the 
parent, because the task's context is already done.
   
   `TestRunTaskCancelsTheTaskContextWhenItReturns` checks that the task's 
context is cancelled after a task that finishes in time.



-- 
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]

Reply via email to