You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "prayaagmahajan (via GitHub)" <gi...@apache.org> on 2026/03/10 09:52:08 UTC

[D] Race condition in CeleryExecutor with multiple schedulers causing duplicate TaskInstance execution [airflow]

GitHub user prayaagmahajan created a discussion: Race condition in CeleryExecutor with multiple schedulers causing duplicate TaskInstance execution

**What happened**

We observed a race condition in Airflow 3.1.7 when running with:

  - 3 schedulers
  - 3 Celery nodes
  - Redis broker

A single TaskInstance was executed by **two Celery workers simultaneously**, resulting in inconsistent scheduler state updates.

**Specifically:**

- two different external_executor_id values appeared for the same TaskInstance

- the scheduler received executor events with different try_numbers

- events were processed out of order, causing the task state to be corrupted

**Example observed sequence:**
```
executor event → queued → try_number=1
executor event → failed → try_number=2
executor event → success → try_number=1
```

This resulted in the scheduler logging:

Executor reported state=failed but TaskInstance state=running

and the DAG run ultimately failing.

Scheduler log:
```
Received executor event with state queued for task instance ...
try_number=1

Received executor event with state failed ...
try_number=2

Received executor event with state success ...
try_number=1
```
Error:

```
Executor reported that the task instance finished with state failed,
but the task instance's state attribute is running.
```

Celery worker logs show two separate executions:

```
external_executor_id=fc123
try_number=1
```

and

```
external_executor_id=db234
try_number=2
```


GitHub link: https://github.com/apache/airflow/discussions/63249

----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org


Re: [D] Race condition in CeleryExecutor with multiple schedulers causing duplicate TaskInstance execution [airflow]

Posted by "hezeclark (via GitHub)" <gi...@apache.org>.
GitHub user hezeclark added a comment to the discussion: Race condition in CeleryExecutor with multiple schedulers causing duplicate TaskInstance execution

Duplicate TaskInstance execution with multiple schedulers and CeleryExecutor is a known race condition related to the scheduler heartbeat and task claiming mechanism.

The root causes:

1. Multiple schedulers racing to queue the same TaskInstance — the database-level locking in Airflow 3.x uses SELECT FOR UPDATE SKIP LOCKED but can still have race conditions under high load.

2. Celery at-least-once delivery (TASK_ACKS_LATE) causing redelivery if a worker acks late.

Mitigations:

1. Enable executor-level deduplication — make sure all schedulers share the same database and the scheduler_heartbeat_sec is not too low:
scheduler_heartbeat_sec = 5  # default, increase under load
max_tis_per_query = 512  # reduce to lower contention

2. Use database-level advisory locks — Airflow 3 supports PostgreSQL advisory locks for scheduler coordination:
[scheduler]
use_row_level_locking = True

3. For Celery + Redis: set broker_transport_options correctly:
[celery_broker_transport_options]
visibility_timeout = 86400  # must be > your longest task

4. Consider switching to the Database Executor (Airflow 3.x) for multi-scheduler setups — it uses database-level SKIP LOCKED which is more reliable than Celery for deduplication.

For immediate mitigation: reduce the number of schedulers to 2 and verify all schedulers are on the same database with high availability (primary + read replica is fine, but all schedulers must write to the same primary).

GitHub link: https://github.com/apache/airflow/discussions/63249#discussioncomment-16112341

----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org