Prevent KubernetesExecutor from launching stale workloads - #69762
Prevent KubernetesExecutor from launching stale workloads#69762wolvery wants to merge 8 commits into
Conversation
ce52afb to
84e206c
Compare
27536bf to
66de510
Compare
66de510 to
3a1d3ef
Compare
potiuk
left a comment
There was a problem hiding this comment.
Thanks — the bug is real and well-described. A workload can sit in the executor queue while pod creation is delayed, and in an HA scheduler deployment the task instance can be retried, cleared or re-queued in the meantime, so launching the pod anyway produces a duplicate or orphaned run. Revalidating against the immutable TI id before creating the pod is the right shape, and checking all four of (exists, still QUEUED, try_number, queued_by_job_id) is the correct set — queued_by_job_id in particular is what catches the HA case.
I also checked whether this crosses an architecture boundary and it doesn't: this executor already queries the metadata DB (@provide_session at line 547, select(TaskInstance.state) at 699), so it's consistent with the existing design rather than new access.
Failing open on ImportError and on a non-numeric scheduler_job_id is the right call too — a preflight check should never be the thing that stops tasks running.
One blocking issue with the query itself, inline.
Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting
7427bd4 to
ffdd511
Compare
Fixes #69760
This adds a final DB preflight before KubernetesExecutor creates a worker pod for an Airflow 3 ExecuteTask workload. A workload can sit in the executor queue while Kubernetes pod creation is delayed, and in HA scheduler deployments the task instance can be retried, cleared, or otherwise moved on before the pod is eventually created.
The executor now verifies that the workload still matches the current queued task instance before calling run_next:
If the workload is stale, it is dropped before pod creation and executor bookkeeping for that launch is cleared.
Performance trade-off: this adds one indexed metadata DB query and one database round-trip per pod launch. The query uses the TaskInstance primary key, and the additional round-trip is an intentional correctness cost to prevent stale workloads from creating duplicate or orphaned pods.
Tests added:
Local verification:
Gen-AI disclosure
Generated-by: GPT following the guidelines: https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions