Skip to content

[3.4.0] KubernetesPodOperator task_state_store upsert waits seconds on dag_run row locks held by the scheduler loop (MySQL) #77493

Open

Description

@vladosh84

Under which category would you file this issue?

Airflow Core

Apache Airflow version

3.4.0

What happened and how to reproduce it?

In 3.4.0, KubernetesPodOperator writes the submitted pod's identity to task_state_store
(_persist_pod_identity_to_task_state_store). The worker calls
PUT /execution/store/ti/{ti_id}/pod_identifier, and the API server runs a MySQL upsert
(INSERT ... ON DUPLICATE KEY UPDATE, from airflow/state/metastore.py).

task_state_store.dag_run_id has a foreign key to dag_run.id. On InnoDB the FK check takes a shared
lock on the parent dag_run row. The scheduler loop takes exclusive locks on the dag_run rows it
processes (SELECT ... FOR UPDATE SKIP LOCKED, then UPDATE dag_run SET last_scheduling_decision ...)
and holds them until it commits at the end of the iteration. Any KPO task of a DAG run the scheduler is
working on waits for that commit.

The upsert itself examines 0 rows, and all of its time is lock wait. From
performance_schema/slow-log digests over one hour on our production database:

Statement Calls Total query time Total lock time Avg
INSERT INTO task_state_store ... ON DUPLICATE KEY UPDATE 7,952 22,375 s 22,388 s 2.8 s
All other statements (5.33M calls) about 3,200 s about 90 s about 7 rows examined per query

So task_state_store is about 99.6% of all lock time on the metadata DB. Over a full day the hourly
average stayed between 2.9 and 4.1 s per upsert, at about 7,800 upserts per hour.

sys.innodb_lock_waits during a burst: 20 waiters on dag_run PRIMARY, all blocked by one connection
holding X,REC_NOT_GAP. That connection belonged to the scheduler, with a 17 s old transaction and the
connection in Sleep. Its statement history was SELECT ... FROM dag_run ... →
UPDATE dag_run SET last_scheduling_decision = ... → (idle) → COMMIT. The scheduler's queries
finish in milliseconds, but the row locks are held while the scheduler does in-process work between
them until the loop commits.

Effects:

  • With the default [workers] execution_api_timeout (5 s), the PUT timed out on the worker
    (ReadTimeout), and KPO task tries failed.
  • In bursts the API server's DB connection pool ran out, because connections were parked in lock waits.
    Execution API calls then failed after the pool timeout with QueuePool limit of size 5 overflow 10 reached, connection timed out (HTTP 500), including some that waited 50 s
    (innodb_lock_wait_timeout).
  • Scheduler loop p99 went from about 7 s on 3.3.2 to 13–18 s on 3.4.0. The mean was unchanged at about
    0.8 s.

The table size of dag_run/task_instance is not the cause: the FK check is a primary-key lookup, and
the scheduler's own queries examine a handful of rows each. The wait is bounded by how long the
scheduler keeps its transaction open.

How to reproduce

  1. Airflow 3.4.0, MySQL 8.4, CeleryExecutor, apache-airflow-providers-cncf-kubernetes 10.25.0.
  2. Have many active DAG runs (hundreds) with KubernetesPodOperator tasks, so the scheduler loop takes
    seconds.
  3. Watch performance_schema.data_lock_waits / sys.innodb_lock_waits: KPO upserts into
    task_state_store wait on dag_run PRIMARY rows held by the scheduler's connection.

What you think should happen instead?

Writing task state from a running task should not have to wait for the scheduler's transaction on the
same DAG run. Possible directions:

  • drop the FK from task_state_store.dag_run_id to dag_run.id (or don't enforce it at the DB level)
    and clean up orphans in airflow db clean;
  • key task_state_store on the task instance instead of the DAG run, so the parent row is not one the
    scheduler locks;
  • shorten the scheduler's hold on dag_run rows (commit before the non-DB work in the loop);
  • let KubernetesPodOperator skip persisting the pod identifier when it isn't needed.

Operating System

Debian 13 (trixie), Python 3.14.10

Deployment

Official Apache Airflow Helm Chart

Apache Airflow Provider(s)

cncf-kubernetes, mysql

Versions of Apache Airflow Providers

apache-airflow-providers-cncf-kubernetes==10.25.0
apache-airflow-providers-mysql==6.7.1
apache-airflow-providers-common-sql==2.4.0
apache-airflow-providers-celery==3.26.0

Official Helm Chart version

1.22.0 (latest released)

Kubernetes Version

1.35.9

Helm Chart configuration

Used as a subchart. CeleryExecutor, 2 schedulers, external MySQL 8.4.9 (Azure Database for MySQL Flexible Server, InnoDB, REPEATABLE READ, innodb_autoinc_lock_mode = 2). Default [workers] execution_api_timeout (5 s) when the failures were seen.

Docker Image customizations

Custom image on Debian 13 with apache-airflow 3.4.0 / task-sdk 1.4.0, SQLAlchemy 2.0.55, mysqlclient 2.3.0, celery 5.6.3, kubernetes (Python client) 36.0.3.

Anything else?

Mitigations we applied:

  • [workers] execution_api_timeout = 30 stops the worker-side timeouts. It doesn't reduce the waits.
  • [scheduler] max_dagruns_per_loop_to_schedule = 5 (default 20). Comparing the hour after the change
    with the hour before, average lock wait per upsert went from 2.8 s to 2.0 s (22,388 s over 7,952
    calls → 15,223 s over 7,440), and scheduler loop p99 from 15.3 s to 13.3 s. It helps, but upserts
    still wait about 2 s.

Code at 3.4.0:

  • providers/cncf/kubernetes/.../operators/pod.py: _persist_pod_identity_to_task_state_store
  • airflow-core/src/airflow/state/metastore.py: MySQL upsert (on_duplicate_key_update)

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Nov 23, 2026
  2. boring-cyborg commented on Nov 23, 2026

    @boring-cyborg

    Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

  3. yakovlef commented on Nov 23, 2026

    @yakovlef
    Contributor

    hey, looked into this. it's mysql only because with_row_locks(key_share=True) is FOR NO KEY UPDATE on postgres, which doesn't block the FK check, but plain FOR UPDATE on mysql, so the insert waits for the scheduler commit.

    the FK only does ON DELETE CASCADE now, _store_task_state already checks the DagRun and 404s if it's gone (#72907). so I'd drop task_state_store_dag_run_fkey in a migration and delete the rows explicitly where dag runs get deleted (api, db clean).

    happy to do the PR if that sounds ok

  4. eladkal commented on Nov 30, 2026

    @eladkal
    Contributor

    @yakovlef if you have a fix you can just start a PR. no need to wait for a special approval

  5. added
    area:db-migrations
    affected_version:3.4
    and removed
    needs-triage
    on Nov 30, 2026
  6. yakovlef commented on Nov 30, 2026

    @yakovlef
    Contributor

    thanks, got it, won't wait next time. I've opened PR #77815

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected_version:3.4area:db-migrationskind:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions