This is an automated email from the ASF dual-hosted git repository.
jason810496 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new e04bfefe28a Go SDK: send execution_timeout and use the worker's
default (#76359)
e04bfefe28a is described below
commit e04bfefe28a485ad6322397957cd132bddba9232
Author: ChickenBenny <[email protected]>
AuthorDate: Thu Nov 19 14:52:55 2026 +0800
Go SDK: send execution_timeout and use the worker's default (#76359)
* Apply the worker's default execution_timeout to language SDK tasks
* Go SDK: move the any-value helpers into their own file
* Go SDK: send the task's execution_timeout to the supervisor
* Read the worker's default execution_timeout before the task instance
starts
* Go SDK: handle a zero or negative execution_timeout in
resolveExecutionTimeout
* Go SDK: note that ifaceInt truncates a float
* Go SDK: test the supervisor rejecting SetExecutionTimeout
---
go-sdk/pkg/execution/execution_timeout.go | 55 ++++++++
go-sdk/pkg/execution/execution_timeout_test.go | 156 +++++++++++++++++++++
go-sdk/pkg/execution/iface.go | 86 ++++++++++++
go-sdk/pkg/execution/iface_test.go | 67 +++++++++
go-sdk/pkg/execution/messages.go | 52 -------
go-sdk/pkg/execution/messages_test.go | 24 ----
go-sdk/pkg/execution/task_runner.go | 21 ++-
.../src/airflow/sdk/coordinators/_subprocess.py | 38 ++++-
.../tests/task_sdk/coordinators/test_subprocess.py | 78 +++++++++--
9 files changed, 487 insertions(+), 90 deletions(-)
diff --git a/go-sdk/pkg/execution/execution_timeout.go
b/go-sdk/pkg/execution/execution_timeout.go
new file mode 100644
index 00000000000..c99623491a0
--- /dev/null
+++ b/go-sdk/pkg/execution/execution_timeout.go
@@ -0,0 +1,55 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package execution
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/apache/airflow/go-sdk/pkg/execution/genmodels"
+)
+
+// 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) {
+ if fromContext == nil {
+ return nil, nil
+ }
+ seconds, ok := ifaceFloat(fromContext)
+ switch {
+ case !ok:
+ return nil, fmt.Errorf(
+ "ti_context.execution_timeout has unexpected wire shape
%T",
+ fromContext,
+ )
+ case seconds == 0:
+ return nil, nil
+ case seconds < 0:
+ return nil, fmt.Errorf("execution_timeout of %g seconds has
already elapsed", seconds)
+ }
+ return &seconds, 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
+}
diff --git a/go-sdk/pkg/execution/execution_timeout_test.go
b/go-sdk/pkg/execution/execution_timeout_test.go
new file mode 100644
index 00000000000..5e2ef7b51d2
--- /dev/null
+++ b/go-sdk/pkg/execution/execution_timeout_test.go
@@ -0,0 +1,156 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package execution
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/apache/airflow/go-sdk/internal/bundle"
+ "github.com/apache/airflow/go-sdk/internal/contexttest"
+)
+
+// TestServeSendsExecutionTimeoutEndToEnd pins that the runtime sends the
supervisor the
+// execution_timeout in ti_context before the task runs, and sends nothing
when there is none or it is
+// zero, which means no timeout.
+func TestServeSendsExecutionTimeoutEndToEnd(t *testing.T) {
+ setTimeout := func(seconds float64) []map[string]any {
+ return []map[string]any{{"type": "SetExecutionTimeout",
"timeout_seconds": seconds}}
+ }
+ tests := []struct {
+ name string
+ fromContext any
+ wantRequests []map[string]any
+ }{
+ {name: "whole seconds", fromContext: 30, wantRequests:
setTimeout(30)},
+ {name: "fractional seconds", fromContext: 1.5, wantRequests:
setTimeout(1.5)},
+ {name: "zero", fromContext: 0},
+ {name: "unset"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ ran := false
+ task, err :=
bundle.NewTaskFunction(func(contexttest.Context) error {
+ ran = true
+ return nil
+ })
+ require.NoError(t, err)
+ tiContext := map[string]any{}
+ if tt.fromContext != nil {
+ tiContext["execution_timeout"] = tt.fromContext
+ }
+
+ requests, terminal := serveTask(t, "task", task, nil,
tiContext, answerAll)
+
+ assert.True(t, ran)
+ assert.Equal(t, tt.wantRequests, requests)
+ assert.Equal(t, "SucceedTask", terminal["type"])
+ })
+ }
+}
+
+// TestServeFailsWithoutRunningOnExecutionTimeoutEndToEnd pins that the task
does not run, and
+// nothing is sent to the supervisor, when its execution_timeout has already
elapsed, as for a Python
+// task, or is not a number.
+func TestServeFailsWithoutRunningOnExecutionTimeoutEndToEnd(t *testing.T) {
+ tests := []struct {
+ name string
+ fromContext any
+ wantReason string
+ }{
+ {
+ name: "negative",
+ fromContext: -5,
+ wantReason: "execution_timeout of -5 seconds has
already elapsed",
+ },
+ {
+ name: "not a number",
+ fromContext: "30",
+ wantReason: "ti_context.execution_timeout has
unexpected wire shape string",
+ },
+ }
+ for _, tt := range tests {
+ for _, shouldRetry := range []bool{true, false} {
+ t.Run(fmt.Sprintf("%s/should_retry=%t", tt.name,
shouldRetry), func(t *testing.T) {
+ ran := false
+ task, err :=
bundle.NewTaskFunction(func(contexttest.Context) error {
+ ran = true
+ return nil
+ })
+ require.NoError(t, err)
+ tiContext := map[string]any{
+ "should_retry": shouldRetry,
+ "execution_timeout": tt.fromContext,
+ }
+
+ requests, terminal := serveTask(t, "task",
task, nil, tiContext, answerAll)
+
+ assert.False(t, ran, "the task must not run")
+ assert.Empty(t, requests)
+ if shouldRetry {
+ assert.Equal(t, "RetryTask",
terminal["type"])
+ assert.Contains(t,
terminal["retry_reason"], tt.wantReason)
+ } else {
+ assert.Equal(t, "TaskState",
terminal["type"])
+ assert.Equal(t, "failed",
terminal["state"])
+ }
+ })
+ }
+ }
+}
+
+// TestServeFailsWhenTheSupervisorRejectsExecutionTimeoutEndToEnd pins that
the task does not run
+// when the supervisor rejects SetExecutionTimeout.
+func TestServeFailsWhenTheSupervisorRejectsExecutionTimeoutEndToEnd(t
*testing.T) {
+ for _, shouldRetry := range []bool{true, false} {
+ t.Run(fmt.Sprintf("should_retry=%t", shouldRetry), func(t
*testing.T) {
+ ran := false
+ task, err :=
bundle.NewTaskFunction(func(contexttest.Context) error {
+ ran = true
+ return nil
+ })
+ require.NoError(t, err)
+
+ requests, terminal := serveTask(t, "task", task, nil,
map[string]any{
+ "execution_timeout": 30,
+ "should_retry": shouldRetry,
+ }, func(map[string]any) map[string]any {
+ return map[string]any{
+ "type": "ErrorResponse",
+ "error": "GENERIC_ERROR",
+ "detail": map[string]any{"message":
"boom"},
+ }
+ })
+
+ assert.False(t, ran, "the task must not run")
+ assert.Equal(t, []map[string]any{
+ {"type": "SetExecutionTimeout",
"timeout_seconds": float64(30)},
+ }, requests)
+ if shouldRetry {
+ assert.Equal(t, "RetryTask", terminal["type"])
+ assert.Contains(t, terminal["retry_reason"],
"GENERIC_ERROR")
+ } else {
+ assert.Equal(t, "TaskState", terminal["type"])
+ assert.Equal(t, "failed", terminal["state"])
+ }
+ })
+ }
+}
diff --git a/go-sdk/pkg/execution/iface.go b/go-sdk/pkg/execution/iface.go
new file mode 100644
index 00000000000..7e1e745463e
--- /dev/null
+++ b/go-sdk/pkg/execution/iface.go
@@ -0,0 +1,86 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package execution
+
+// ifaceString returns the string carried by a nullable schema field decoded as
+// any, or "" when the value is nil or not a string.
+func ifaceString(v any) string {
+ s, _ := v.(string)
+ return s
+}
+
+// ifaceStringPtr preserves the null-vs-empty distinction for a nullable string
+// field decoded as any: nil when missing/null, else a pointer to the string.
+// Used for connection credentials, where an explicit "" must stay distinct
from
+// "not set".
+func ifaceStringPtr(v any) *string {
+ s, ok := v.(string)
+ if !ok {
+ return nil
+ }
+ return &s
+}
+
+// ifaceInt converts a numeric value decoded as any into int, truncating a
float,
+// and returns def when the value is nil or non-numeric.
+func ifaceInt(v any, def int) int {
+ if n, ok := ifaceNumber[int](v); ok {
+ return n
+ }
+ return def
+}
+
+// ifaceFloat converts a numeric value decoded as any into float64, reporting
+// false when it is not numeric.
+func ifaceFloat(v any) (float64, bool) {
+ return ifaceNumber[float64](v)
+}
+
+// ifaceNumber converts a numeric value decoded as any into T, reporting false
+// when it is not numeric. msgpack decodes a number into whichever int, uint or
+// float width fits it.
+func ifaceNumber[T int | float64](v any) (T, bool) {
+ switch n := v.(type) {
+ case int:
+ return T(n), true
+ case int8:
+ return T(n), true
+ case int16:
+ return T(n), true
+ case int32:
+ return T(n), true
+ case int64:
+ return T(n), true
+ case uint:
+ return T(n), true
+ case uint8:
+ return T(n), true
+ case uint16:
+ return T(n), true
+ case uint32:
+ return T(n), true
+ case uint64:
+ return T(n), true
+ case float32:
+ return T(n), true
+ case float64:
+ return T(n), true
+ default:
+ return 0, false
+ }
+}
diff --git a/go-sdk/pkg/execution/iface_test.go
b/go-sdk/pkg/execution/iface_test.go
new file mode 100644
index 00000000000..2839bd7b01f
--- /dev/null
+++ b/go-sdk/pkg/execution/iface_test.go
@@ -0,0 +1,67 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package execution
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestIfaceInt(t *testing.T) {
+ for _, v := range []any{
+ int(42), int8(42), int16(42), int32(42), int64(42),
+ uint(42), uint8(42), uint16(42), uint32(42), uint64(42),
+ float32(42), float64(42),
+ } {
+ assert.Equal(t, 42, ifaceInt(v, -1))
+ }
+ assert.Equal(t, -1, ifaceInt(nil, -1))
+ assert.Equal(t, -1, ifaceInt("not a number", -1))
+}
+
+func TestIfaceFloat(t *testing.T) {
+ for _, v := range []any{
+ int(42), int8(42), int16(42), int32(42), int64(42),
+ uint(42), uint8(42), uint16(42), uint32(42), uint64(42),
+ float32(42), float64(42),
+ } {
+ f, ok := ifaceFloat(v)
+ assert.True(t, ok)
+ assert.Equal(t, 42.0, f)
+ }
+ f, ok := ifaceFloat(1.5)
+ assert.True(t, ok)
+ assert.Equal(t, 1.5, f)
+ for _, v := range []any{nil, "not a number"} {
+ _, ok := ifaceFloat(v)
+ assert.False(t, ok)
+ }
+}
+
+func TestIfaceStringHelpers(t *testing.T) {
+ assert.Equal(t, "v", ifaceString("v"))
+ assert.Equal(t, "", ifaceString(nil))
+ assert.Equal(t, "", ifaceString(42))
+
+ assert.Nil(t, ifaceStringPtr(nil))
+ assert.Nil(t, ifaceStringPtr(42))
+ if p := ifaceStringPtr(""); assert.NotNil(t, p) {
+ assert.Equal(t, "", *p) // explicit "" stays distinct from "not
set"
+ }
+}
diff --git a/go-sdk/pkg/execution/messages.go b/go-sdk/pkg/execution/messages.go
index 8fe334311d6..f09591dadc8 100644
--- a/go-sdk/pkg/execution/messages.go
+++ b/go-sdk/pkg/execution/messages.go
@@ -264,55 +264,3 @@ func apiErrorFromFrame(f IncomingFrame) *APIError {
}
return &APIError{Err: string(resp.Error), Detail: detail}
}
-
-// ifaceString returns the string carried by a nullable schema field decoded as
-// any, or "" when the value is nil or not a string.
-func ifaceString(v any) string {
- s, _ := v.(string)
- return s
-}
-
-// ifaceStringPtr preserves the null-vs-empty distinction for a nullable string
-// field decoded as any: nil when missing/null, else a pointer to the string.
-// Used for connection credentials, where an explicit "" must stay distinct
from
-// "not set".
-func ifaceStringPtr(v any) *string {
- s, ok := v.(string)
- if !ok {
- return nil
- }
- return &s
-}
-
-// ifaceInt converts a numeric value decoded as any (msgpack yields various
-// int/uint/float widths) into int, returning def when nil or non-numeric.
-func ifaceInt(v any, def int) int {
- switch n := v.(type) {
- case int:
- return n
- case int8:
- return int(n)
- case int16:
- return int(n)
- case int32:
- return int(n)
- case int64:
- return int(n)
- case uint:
- return int(n)
- case uint8:
- return int(n)
- case uint16:
- return int(n)
- case uint32:
- return int(n)
- case uint64:
- return int(n)
- case float32:
- return int(n)
- case float64:
- return int(n)
- default:
- return def
- }
-}
diff --git a/go-sdk/pkg/execution/messages_test.go
b/go-sdk/pkg/execution/messages_test.go
index 0cb773bd1bd..0c674bae231 100644
--- a/go-sdk/pkg/execution/messages_test.go
+++ b/go-sdk/pkg/execution/messages_test.go
@@ -725,30 +725,6 @@ func TestNormalizeDecodedValueGoInts(t *testing.T) {
assert.Equal(t, int64(500), normalizeDecodedValue(uint(500)))
}
-func TestIfaceInt(t *testing.T) {
- for _, v := range []any{
- int(42), int8(42), int16(42), int32(42), int64(42),
- uint(42), uint8(42), uint16(42), uint32(42), uint64(42),
- float32(42), float64(42),
- } {
- assert.Equal(t, 42, ifaceInt(v, -1))
- }
- assert.Equal(t, -1, ifaceInt(nil, -1))
- assert.Equal(t, -1, ifaceInt("not a number", -1))
-}
-
-func TestIfaceStringHelpers(t *testing.T) {
- assert.Equal(t, "v", ifaceString("v"))
- assert.Equal(t, "", ifaceString(nil))
- assert.Equal(t, "", ifaceString(42))
-
- assert.Nil(t, ifaceStringPtr(nil))
- assert.Nil(t, ifaceStringPtr(42))
- if p := ifaceStringPtr(""); assert.NotNil(t, p) {
- assert.Equal(t, "", *p) // explicit "" stays distinct from "not
set"
- }
-}
-
func TestIsNilRaw(t *testing.T) {
assert.True(t, isNilRaw(nil))
assert.True(t, isNilRaw(msgpack.RawMessage{}))
diff --git a/go-sdk/pkg/execution/task_runner.go
b/go-sdk/pkg/execution/task_runner.go
index 792aff79a73..bc6ea3d40ee 100644
--- a/go-sdk/pkg/execution/task_runner.go
+++ b/go-sdk/pkg/execution/task_runner.go
@@ -34,11 +34,13 @@ import (
// RunTask executes a task based on StartupDetails received from the
supervisor.
//
// It looks up the task in the bundle, creates a CoordinatorClient for SDK
-// calls, deletes the XComs that ti_context.xcom_keys_to_clear lists, executes
-// the task, and returns the terminal body to ship as the final response frame:
+// calls, deletes the XComs that ti_context.xcom_keys_to_clear lists, sends the
+// supervisor the task's execution_timeout, executes the task, and returns the
+// terminal body to ship as the final response frame:
// one of genmodels.SucceedTask, TaskState, RetryTask, or DeferTask. A task
from airflow.TriggerDagRun
// is the only one that defers, and RunTask runs it without a Go function.
When RunTask cannot
-// delete an XCom, or arg_bindings is invalid, the task fails without running.
+// delete an XCom, cannot resolve the execution_timeout, finds it has already
elapsed, or
+// arg_bindings is invalid, the task fails without running.
//
// The supervisor owns the Execution-API state transitions, so the runtime only
// invokes the user's task function and returns its terminal response.
@@ -114,6 +116,19 @@ func RunTask(
}
}
+ timeoutSeconds, err :=
resolveExecutionTimeout(details.TIContext.ExecutionTimeout)
+ if err == nil && timeoutSeconds != nil {
+ err = client.setExecutionTimeout(ctx, *timeoutSeconds)
+ }
+ if err != nil {
+ logger.Error("Unable to apply execution_timeout",
+ "dag_id", details.TI.DagID,
+ "task_id", details.TI.TaskID,
+ "error", err,
+ )
+ return failTask(details.TIContext.ShouldRetry, err)
+ }
+
// The runtime runs a TriggerDagRun task itself. It has no Go function
to bind arguments to.
if trigger, ok := task.(*bundle.TriggerTask); ok {
return runTriggerDagRun(ctx, details, trigger.Spec, client,
logger, defaultTriggerEnv)
diff --git a/task-sdk/src/airflow/sdk/coordinators/_subprocess.py
b/task-sdk/src/airflow/sdk/coordinators/_subprocess.py
index 95173d29809..41952403c2b 100644
--- a/task-sdk/src/airflow/sdk/coordinators/_subprocess.py
+++ b/task-sdk/src/airflow/sdk/coordinators/_subprocess.py
@@ -67,7 +67,7 @@ from airflow.sdk.coordinators._xcom_backend import (
runs_in_helper,
send_unless_exited,
)
-from airflow.sdk.exceptions import ErrorType
+from airflow.sdk.exceptions import AirflowConfigException, ErrorType
from airflow.sdk.execution_time.bundles import initialize_pinned_bundle
from airflow.sdk.execution_time.comms import (
DeleteXCom,
@@ -495,6 +495,7 @@ class _PopenActivitySubprocess(ActivitySubprocess):
_comm_server: socket.socket
_logs_server: socket.socket
+ _default_execution_timeout: float | None = None
@classmethod
def start( # type: ignore[override]
@@ -509,6 +510,7 @@ class _PopenActivitySubprocess(ActivitySubprocess):
subprocess_schema_version: str | None = None,
subprocess_logs_to_stdout: bool = False,
startup_timeout: float = 10.0,
+ default_execution_timeout: float | None = None,
**kwargs,
) -> Self:
process_log = logger or structlog.get_logger(logger_name="task").bind()
@@ -559,6 +561,7 @@ class _PopenActivitySubprocess(ActivitySubprocess):
stdin=socks[comm_server],
subprocess_schema_version=subprocess_schema_version,
subprocess_logs_to_stdout=subprocess_logs_to_stdout,
+ default_execution_timeout=default_execution_timeout,
# The supervisor signals the group; must agree with
process_group=0 above.
new_process_group=True,
comm_server=comm_server,
@@ -616,8 +619,16 @@ class _PopenActivitySubprocess(ActivitySubprocess):
Like ``XCom.delete``, this reads, deletes, then purges each XCom. A
failure fails this try, as it
does for a Python task.
+
+ Also fills in the worker's default ``execution_timeout`` when the task
sets none.
"""
- self._execution_timeout_seconds = ti_context.execution_timeout
+ # ti_context.execution_timeout is what the API server read from the
serialized Dag. A task that
+ # sets none gets the worker's default, as a Python task does when the
worker parses its Dag.
+ # A zero means no timeout, so it does not fall back.
+ execution_timeout = ti_context.execution_timeout
+ if execution_timeout is None:
+ execution_timeout = self._default_execution_timeout
+ self._execution_timeout_seconds = execution_timeout
for key in ti_context.xcom_keys_to_clear or ():
self.process_log.debug("Clearing XCom with key", key=key)
try:
@@ -657,7 +668,9 @@ class _PopenActivitySubprocess(ActivitySubprocess):
# The supervisor has already deleted the listed XComs, so it sends the
runtime an empty list.
# If a runtime also deletes these XComs itself, the empty list keeps
the runtime from deleting
# them a second time.
- return ti_context.model_copy(update={"xcom_keys_to_clear": []})
+ return ti_context.model_copy(
+ update={"xcom_keys_to_clear": [], "execution_timeout":
execution_timeout}
+ )
def _fail_before_start(self, ti_context: TIRunContext, reason: str) ->
None:
end_date = datetime.now(tz=UTC)
@@ -953,6 +966,23 @@ def _is_file_in_bundle(bundle: BaseDagBundle, rel_path:
str) -> bool:
return False
+def _get_default_execution_timeout() -> float | None:
+ """
+ Return the worker's ``[core] default_task_execution_timeout`` in seconds,
or None when it is unset.
+
+ Read before the task instance is started, so a value that is not a number
fails the task with
+ the reason in its task log.
+
+ :raises TaskLaunchError: when the value is not a number. Retrying on the
same worker reads the
+ same value, so this does not retry.
+ """
+ try:
+ default = conf.gettimedelta("core", "default_task_execution_timeout")
+ except AirflowConfigException as e:
+ raise TaskLaunchError(str(e), retryable=False) from e
+ return None if default is None else default.total_seconds()
+
+
def _require_supported_schema_version(version: str) -> None:
"""
Check that the supervisor of this worker's Task SDK knows *version*.
@@ -1206,6 +1236,7 @@ class SubprocessCoordinator(BaseCoordinator):
with _raise_as_launch_error(
f"Cannot prepare the runtime of task {what.task_id!r} in Dag
{what.dag_id!r}"
):
+ default_execution_timeout = _get_default_execution_timeout()
is_native = self._is_native_dag_file(rel_path,
bundle_info.name)
if is_native:
bundle = _resolve_bundle(bundle_info, task_logger)
@@ -1235,6 +1266,7 @@ class SubprocessCoordinator(BaseCoordinator):
command=command,
subprocess_schema_version=subprocess_schema_version,
startup_timeout=self.task_startup_timeout,
+ default_execution_timeout=default_execution_timeout,
)
exit_code = process.wait()
return self.ExecutionResult(exit_code, process.final_state)
diff --git a/task-sdk/tests/task_sdk/coordinators/test_subprocess.py
b/task-sdk/tests/task_sdk/coordinators/test_subprocess.py
index f8a2aecb6fd..c2d4ecdb28a 100644
--- a/task-sdk/tests/task_sdk/coordinators/test_subprocess.py
+++ b/task-sdk/tests/task_sdk/coordinators/test_subprocess.py
@@ -66,7 +66,7 @@ from airflow.sdk.coordinators._xcom_backend import (
XComBackendResult,
_HookRunner,
)
-from airflow.sdk.exceptions import ErrorType
+from airflow.sdk.exceptions import AirflowConfigException, ErrorType
from airflow.sdk.execution_time.comms import (
DeleteXCom,
GetXCom,
@@ -783,6 +783,31 @@ class TestSubprocessCoordinatorExecuteTask:
assert popen_calls, "subprocess.Popen was not called"
return popen_calls[0], cls_kwargs.get("subprocess_schema_version")
+ @pytest.mark.parametrize(
+ ("worker_default", "expected"),
+ [
+ pytest.param("", None, id="unset"),
+ pytest.param("10", 10.0, id="seconds"),
+ pytest.param("1_000", 1000.0, id="as-python-reads-it"),
+ ],
+ )
+ @patch.object(_PopenActivitySubprocess, "start", autospec=True)
+ def test_passes_the_worker_default_execution_timeout(
+ self, mock_start, mock_client, worker_default, expected
+ ):
+ coordinator = _StubSubprocessCoordinator(command=["/runtime"])
+
+ with conf_vars({("core", "default_task_execution_timeout"):
worker_default}):
+ coordinator.execute_task(
+ what=_make_ti(),
+ dag_rel_path="dag.py",
+ bundle_info=BundleInfo(name="dags"),
+ client=mock_client,
+ subprocess_logs_to_stdout=False,
+ )
+
+ assert mock_start.call_args.kwargs["default_execution_timeout"] ==
expected
+
def test_command_prefix_preserved(self, mock_client):
cmd, _ = self._captured_popen_cmd(mock_client,
command=["/path/to/runtime", "arg1"])
assert cmd[:2] == ["/path/to/runtime", "arg1"]
@@ -1349,13 +1374,17 @@ class TestPopenActivitySubprocessProcessGroup:
class TestPopenActivitySubprocessExecutionTimeout:
@pytest.mark.parametrize(
- ("execution_timeout", "elapsed", "killed"),
+ ("execution_timeout", "worker_default", "elapsed", "sent", "killed"),
[
- pytest.param(30.0, 35, True, id="with-timeout"),
- pytest.param(None, 35, False, id="without-timeout"),
- pytest.param(0.0, 35, False, id="zero-means-no-timeout"),
- pytest.param(-30.0, 3, False, id="negative-keeps-grace-period"),
- pytest.param(-30.0, 6, True, id="negative-has-elapsed"),
+ pytest.param(30.0, None, 35, 30.0, True, id="with-timeout"),
+ pytest.param(None, None, 35, None, False, id="without-timeout"),
+ pytest.param(0.0, None, 35, 0.0, False,
id="zero-means-no-timeout"),
+ pytest.param(-30.0, None, 3, -30.0, False,
id="negative-keeps-grace-period"),
+ pytest.param(-30.0, None, 6, -30.0, True,
id="negative-has-elapsed"),
+ pytest.param(None, 10.0, 14, 10.0, False,
id="worker-default-keeps-grace-period"),
+ pytest.param(None, 10.0, 16, 10.0, True,
id="worker-default-has-elapsed"),
+ pytest.param(30.0, 10.0, 16, 30.0, False,
id="run-context-over-worker-default"),
+ pytest.param(0.0, 10.0, 35, 0.0, False,
id="zero-does-not-fall-back"),
],
)
@patch("airflow.sdk.execution_time.supervisor.KILLED_TASK_CLEANUP_TIME",
5.0)
@@ -1363,7 +1392,15 @@ class TestPopenActivitySubprocessExecutionTimeout:
@patch.object(_PopenActivitySubprocess, "send_msg", autospec=True)
@patch("time.monotonic", autospec=True, return_value=1.0)
def test_arms_execution_timeout_from_run_context(
- self, mock_monotonic, mock_send_msg, mock_kill, execution_timeout,
elapsed, killed
+ self,
+ mock_monotonic,
+ mock_send_msg,
+ mock_kill,
+ execution_timeout,
+ worker_default,
+ elapsed,
+ sent,
+ killed,
):
client = MagicMock()
client.task_instances.start.return_value =
TIRunContext.model_construct(
@@ -1378,6 +1415,7 @@ class TestPopenActivitySubprocessExecutionTimeout:
client=client,
comm_server=MagicMock(spec=socket.socket),
logs_server=MagicMock(spec=socket.socket),
+ default_execution_timeout=worker_default,
)
proc._on_child_started(
@@ -1387,6 +1425,8 @@ class TestPopenActivitySubprocessExecutionTimeout:
sentry_integration="",
)
+ startup = mock_send_msg.call_args.args[1]
+ assert startup.ti_context.execution_timeout == sent
mock_monotonic.return_value += elapsed
proc._handle_execution_timeout_if_needed()
assert mock_kill.called is killed
@@ -2299,6 +2339,28 @@ class TestExecuteTaskLaunchFailure:
assert raised.value.retryable is False
mock_client.task_instances.start.assert_not_called()
+ @pytest.mark.usefixtures("artifact_bundle")
+ @conf_vars({("core", "default_task_execution_timeout"): "abc"})
+ @patch.object(_PopenActivitySubprocess, "start", autospec=True)
+ def
test_fails_without_a_retry_for_a_worker_default_execution_timeout_that_is_not_a_number(
+ self, mock_start, mock_client
+ ):
+ coordinator = _StubSubprocessCoordinator(command=["/runtime"])
+
+ with pytest.raises(TaskLaunchError,
match="default_task_execution_timeout") as raised:
+ coordinator.execute_task(
+ what=_make_ti(),
+ dag_rel_path="dag.py",
+ bundle_info=BundleInfo(name="dags"),
+ client=mock_client,
+ subprocess_logs_to_stdout=False,
+ )
+
+ assert raised.value.retryable is False
+ assert isinstance(raised.value.__cause__, AirflowConfigException)
+ mock_start.assert_not_called()
+ mock_client.task_instances.start.assert_not_called()
+
@pytest.mark.usefixtures("artifact_bundle")
@pytest.mark.parametrize(
("failing", "expected"),