Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,15 @@ class TIRunContext(BaseModel):
``None`` for regular tasks and for stub tasks that declare no parameters.
"""

execution_timeout: float | None = None
"""
The task's ``execution_timeout`` in seconds, for tasks run by a language SDK.

Covers ``@task.stub`` tasks and native language-SDK Dag tasks. Their runtime never sends
``SetExecutionTimeout``, so the supervisor arms its deadline from this value. ``None`` for Python
tasks, which report their own timeout, and for language-SDK tasks without one.
"""

multi_team: bool = False
"""
Whether the deployment runs in multi-team mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
from airflow.api_fastapi.execution_api.services.task_instances import (
client_supports_arg_bindings,
get_arg_bindings,
get_language_sdk_execution_timeout,
)
from airflow.api_fastapi.execution_api.versions.v2026_10_30 import IdentifyArchivedTaskStateUpdates
from airflow.configuration import conf
Expand Down Expand Up @@ -406,6 +407,12 @@ def ti_run(
},
)

# Stub and native language-SDK Dag tasks need the timeout from the server, not a parsed Dag.
if (
execution_timeout := get_language_sdk_execution_timeout(dag_bag, ti, session=session)
) is not None:
context.execution_timeout = execution_timeout

# Only set if they are non-null
if ti.next_method:
context.next_method = ti.next_method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from sqlalchemy.orm import Session

from airflow.models.dagbag import DBDagBag
from airflow.serialization.serialized_objects import SerializedOperator


def client_supports_arg_bindings() -> bool:
Expand All @@ -44,18 +45,41 @@ def client_supports_arg_bindings() -> bool:
return AddArgBindingsToTIRunContext.is_applied


def get_arg_bindings(dag_bag: DBDagBag, ti: Any, *, session: Session) -> list | None:
def get_stub_task(dag_bag: DBDagBag, ti: Any, *, session: Session) -> SerializedOperator | None:
"""
Extract the stub task's TaskFlow arg spec from its Dag version.
Return the TI's task from its Dag version if it runs in a language SDK, else ``None``.

Mapped (``.expand()``) stubs never capture a parse-time spec, so they resolve to
``None`` here and keep the legacy ignored-args behavior; per-map-index delivery
lands in a follow-up.
Both ``@task.stub`` tasks and native language-SDK Dag tasks serialize with ``is_stub=True``.
"""
if ti.dag_version_id is None:
return None
if (dag := dag_bag.get_dag(ti.dag_version_id, session=session)) is None:
return None
if (task := dag.task_dict.get(ti.task_id)) is None or not task.is_stub:
return None
return task


def get_arg_bindings(dag_bag: DBDagBag, ti: Any, *, session: Session) -> list | None:
"""
Extract the stub task's TaskFlow arg spec from its Dag version.

Mapped (``.expand()``) stubs never capture a parse-time spec, so they resolve to
``None`` here and keep the legacy ignored-args behavior; per-map-index delivery
lands in a follow-up.
"""
if (task := get_stub_task(dag_bag, ti, session=session)) is None:
return None
return task.arg_bindings


def get_language_sdk_execution_timeout(dag_bag: DBDagBag, ti: Any, *, session: Session) -> float | None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the first lines here are the same lookup as get_arg_bindings. Maybe a small helper that returns the stub task, so both can use it?

"""
Return ``execution_timeout`` in seconds for tasks run by a language SDK.

These tasks need the timeout from the server; a Python task parses the Dag file and reports its own.
Return ``None`` for other tasks and when no timeout is set, including a zero timeout.
"""
if (task := get_stub_task(dag_bag, ti, session=session)) is None or not task.execution_timeout:
return None
return task.execution_timeout.total_seconds()
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
AddArgBindingsToTIRunContext,
AddCallbackRunEndpoint,
AddDagRunNoteUpdateEndpoint,
AddExecutionTimeoutToTIRunContext,
AddFirstTaskRescheduleStartDateField,
AddLoopContext,
AddMultiTeamToTIRunContext,
Expand All @@ -77,6 +78,7 @@
AddArgBindingsToTIRunContext,
AddCallbackRunEndpoint,
AddDagRunNoteUpdateEndpoint,
AddExecutionTimeoutToTIRunContext,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to decide which release this goes into before merging. 2026-10-30 already shipped in 3.4.0b1 and its published supervisor schema has no execution_timeout, while main now adds new changes at 2027-02-28 (#74370). So either this is backported to v3-4-test before 3.4.0, or it moves to 2027-02-28 here and in the supervisor schema.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Moving to 2027-02-28 is my preference, but on the supervisor-schema side it has a knock-on effect I'd like to check first.

A new 2027-02-28 supervisor-schema version moves schema.json's api_version, and the sync-go-sdk-schemas / sync-ts-sdk-schemas hooks keep the Go and TS vendored copies identical to it. So both SDKs would also have to declare supervisor schema 2027-02-28 (SupervisorSchemaVersion, capabilities.yaml, the generated TS constant), which means SDK builds from main would need an Airflow release newer than 3.4.

Is that acceptable, or would you rather keep 2026-10-30 and backport to v3-4-test before 3.4.0? I have the 2027-02-28 move ready locally and can push either way. The other three comments are addressed in 5454b4f, and the PR body now spells out the SIGTERM timing and the native Dag task coverage.


Drafted-by: Claude Code (Opus 5.5) (no human review before posting)

AddLoopContext,
AddTerminalStateRetryReasonField,
AddMultiTeamToTIRunContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ def remove_arg_bindings_field(response: ResponseInfo) -> None: # type: ignore[m
response.body.pop("arg_bindings", None)


class AddExecutionTimeoutToTIRunContext(VersionChange):
"""Add ``execution_timeout`` so the supervisor enforces it for stub and native language-SDK Dag tasks."""

description = __doc__

instructions_to_migrate_to_previous_version = (
schema(TIRunContext).field("execution_timeout").didnt_exist,
)

@convert_response_to_previous_version_for(TIRunContext) # type: ignore[arg-type]
def remove_execution_timeout_field(response: ResponseInfo) -> None: # type: ignore[misc]
"""Strip ``execution_timeout`` from the run context for older clients."""
response.body.pop("execution_timeout", None)


class AddCallbackRunEndpoint(VersionChange):
"""Add the callbacks/{callback_id}/run endpoint a worker uses to exchange its single-use callback token."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,44 @@ def transform(country: str, extracted: dict, limit: int = 10): ...
assert response.status_code == 200
assert "arg_bindings" not in response.json()

def test_ti_run_returns_execution_timeout_for_stub_task_only(self, client, dag_maker):
"""A stub task's execution_timeout is returned in seconds; other tasks report their own."""
with dag_maker("test_stub_execution_timeout_dag", serialized=True):

@task.stub(execution_timeout=timedelta(seconds=90))
def with_timeout(): ...

@task.stub
def without_timeout(): ...

@task.stub(execution_timeout=timedelta(0))
def zero_timeout(): ...

@task(execution_timeout=timedelta(seconds=10))
def python_task(): ...

with_timeout()
without_timeout()
zero_timeout()
python_task()

dr = dag_maker.create_dagrun()
tis = {ti.task_id: ti for ti in dr.get_task_instances()}
for ti in tis.values():
ti.set_state(State.QUEUED)
dag_maker.session.flush()

response = client.patch(
f"/execution/task-instances/{tis['with_timeout'].id}/run", json=self.RUN_PAYLOAD
)
assert response.status_code == 200
assert response.json()["execution_timeout"] == 90.0

for task_id in ("without_timeout", "zero_timeout", "python_task"):
response = client.patch(f"/execution/task-instances/{tis[task_id].id}/run", json=self.RUN_PAYLOAD)
assert response.status_code == 200
assert "execution_timeout" not in response.json()

@mock.patch(
"airflow.api_fastapi.execution_api.routes.task_instances.get_arg_bindings",
autospec=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import annotations

from datetime import timedelta
from uuid import uuid4

import pytest
Expand Down Expand Up @@ -287,6 +288,39 @@ def test_head_version_includes_arg_bindings(self, client, stub_ti):
]


class TestExecutionTimeoutBackwardCompat:
def setup_method(self):
clear_db_runs()

def teardown_method(self):
clear_db_runs()

@pytest.fixture
def stub_ti(self, dag_maker):
with dag_maker("test_execution_timeout_compat_dag", serialized=True):

@task.stub(execution_timeout=timedelta(seconds=90))
def sleepy(): ...

sleepy()

dr = dag_maker.create_dagrun()
(ti,) = dr.get_task_instances()
ti.set_state(State.QUEUED)
dag_maker.session.flush()
return ti

def test_old_version_strips_execution_timeout_even_when_set(self, old_ver_client, stub_ti):
response = old_ver_client.patch(f"/execution/task-instances/{stub_ti.id}/run", json=RUN_PATCH_BODY)
assert response.status_code == 200
assert "execution_timeout" not in response.json()

def test_head_version_includes_execution_timeout(self, client, stub_ti):
response = client.patch(f"/execution/task-instances/{stub_ti.id}/run", json=RUN_PATCH_BODY)
assert response.status_code == 200
assert response.json()["execution_timeout"] == 90.0


@pytest.fixture
def archived_attempt(client, exec_app, monkeypatch, create_task_instance, session):
"""A running attempt, authenticated with a signed token, that has since been retried."""
Expand Down
35 changes: 19 additions & 16 deletions go-sdk/pkg/execution/genmodels/models.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions go-sdk/schema/supervisor-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5325,6 +5325,18 @@
"default": null,
"title": "Arg Bindings"
},
"execution_timeout": {
"anyOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"default": null,
"title": "Execution Timeout"
},
"multi_team": {
"default": false,
"title": "Multi Team",
Expand Down
1 change: 1 addition & 0 deletions task-sdk/src/airflow/sdk/api/datamodels/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,4 +998,5 @@ class TIRunContext(BaseModel):
should_retry: Annotated[bool | None, Field(title="Should Retry")] = False
start_date: Annotated[AwareDatetime | None, Field(title="Start Date")] = None
arg_bindings: Annotated[list[TaskArgBinding] | None, Field(title="Arg Bindings")] = None
execution_timeout: Annotated[float | None, Field(title="Execution Timeout")] = None
multi_team: Annotated[bool | None, Field(title="Multi Team")] = False
12 changes: 12 additions & 0 deletions task-sdk/src/airflow/sdk/execution_time/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5325,6 +5325,18 @@
"default": null,
"title": "Arg Bindings"
},
"execution_timeout": {
"anyOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"default": null,
"title": "Execution Timeout"
},
"multi_team": {
"default": false,
"title": "Multi Team",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def get_bundle() -> VersionBundle:
from airflow.sdk.execution_time.schema.versions.v2026_10_30 import (
AddArgBindingsToSupervisorTIRunContext,
AddDagDefinitionsToDagFileParsingResult,
AddExecutionTimeoutToSupervisorTIRunContext,
AddFirstTaskRescheduleStartDateToSupervisorTIRunContext,
AddLoopRuntime,
AddRegionCoordinatesToSupervisorTaskInstance,
Expand All @@ -54,6 +55,7 @@ def get_bundle() -> VersionBundle:
AddLoopRuntime,
AddRetryReasonToTaskState,
AddDagDefinitionsToDagFileParsingResult,
AddExecutionTimeoutToSupervisorTIRunContext,
AddRegionCoordinatesToSupervisorTaskInstance,
AddFirstTaskRescheduleStartDateToSupervisorTIRunContext,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,20 @@ class AddFirstTaskRescheduleStartDateToSupervisorTIRunContext(VersionChange):
instructions_to_migrate_to_previous_version = (
schema(TIRunContext).field("first_task_reschedule_start_date").didnt_exist,
)


class AddExecutionTimeoutToSupervisorTIRunContext(VersionChange):
"""
Add the ``execution_timeout`` field carrying a language-SDK task's timeout in seconds.

Language-SDK tasks are ``@task.stub`` tasks and native language-SDK Dag tasks.

The supervisor-schema mirror of the execution API's ``AddExecutionTimeoutToTIRunContext``,
named apart so the two migrations are not confused.
"""

description = __doc__

instructions_to_migrate_to_previous_version = (
schema(TIRunContext).field("execution_timeout").didnt_exist,
)
Loading