Abdel39 opened a new pull request, #77386:
URL: https://github.com/apache/airflow/pull/77386
#64052 made the Celery worker ignore a redelivered message for a task that
is still running: the duplicate's `PATCH /task-instances/{id}/run` gets a 409,
the SDK raises `TaskAlreadyRunningError`, and `execute_workload` raises
`Ignore()`.
That only works while the token in the message is still valid. The token is
a `workload` token minted when the task is queued, valid for `[scheduler]
task_queued_timeout` (600s by default), and it is never refreshed in the
message (the running process swaps it for an execution token on `/run`).
`JWTBearer` rejects an expired token with a 403 before the route runs, so a
copy redelivered more than `task_queued_timeout` after queueing never reaches
the 409 check:
1. A worker loses its broker connection. With `acks_late`, kombu restores
the unacked message with `redelivered=True`, and the running task keeps going.
2. Another worker receives the copy and calls `/run`. It gets 403 `Invalid
auth token`.
3. `ServerResponseError` propagates out of `execute_workload`, so Celery
stores FAILURE under the Celery task id the executor polls.
4. The scheduler sees `failed` for a TI that is `running`, logs a state
mismatch, and fails the attempt that is still running. When the original
finishes, its terminal-state call gets a 404.
The visibility-timeout case named in the existing comment always takes this
path with Redis, since the provider defaults `visibility_timeout` to 86400s.
We see this on Airflow 3.3.2 with provider 3.24.0 and a Redis broker: each
broker connection drop fails every long-running task whose message was unacked
at the time, and the originals then finish successfully.
This treats a redelivered message whose `/run` call got a 403 as a duplicate
and raises `Ignore()`, on both the 3.3+ and the pre-3.3 path. A message that is
not redelivered still fails on a 403, as before.
If the original worker is really gone, ignoring the copy does not leave the
TI stuck: a running TI is caught by `task_instance_heartbeat_timeout`, and a TI
that never started (a prefetched message restored before it ran) is still
queued and is caught by `task_queued_timeout`, which is also when its token
expired.
Alternatives considered:
- Raising `task_queued_timeout` so the token outlives a redelivery keeps the
409 guard working, but it would have to cover the longest task runtime, and it
also delays detection of tasks stuck in the queue.
- Checking TI state before validating the token's expiry would let expired
tokens read TI state.
related: #64052
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes (please specify the tool below)
Generated-by: Claude Code Opus 5.5 following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
---
* Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information. Note: commit author/co-author name and email in commits
become permanently public when merged.
* For fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License
Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment:
`{pr_number}.significant.rst`, in
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
You can add this file in a follow-up commit after the PR is created so you
know the PR number.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]