Skip to content

fix: handle concurrent XCom writes with savepoint + UPDATE fallback to avoid 409#69959

Open
nagasrisai wants to merge 4 commits into
apache:mainfrom
nagasrisai:fix/xcom-execution-api-concurrent-409
Open

fix: handle concurrent XCom writes with savepoint + UPDATE fallback to avoid 409#69959
nagasrisai wants to merge 4 commits into
apache:mainfrom
nagasrisai:fix/xcom-execution-api-concurrent-409

Conversation

@nagasrisai

Copy link
Copy Markdown
Contributor

Closes #69956

Summary

When two task runners (concurrent execution or task retry) write the same XCom key at roughly the same time, both call XComModel.set which does a DELETE followed by an INSERT. Both DELETEs succeed (they each see the record that was just written by the peer or see nothing), then both try to INSERT the same primary key. The second INSERT hits the unique constraint and the Execution API converts the IntegrityError to an HTTP 409, causing the task to fail.

Fix

Wrap the XComModel.set call in a savepoint (session.begin_nested()) so that a constraint violation only rolls back the XCom write and not any earlier work in the same request (e.g. the task_map merge). The IntegrityError is caught and handled with an explicit UPDATE that overwrites the existing entry with the latest value, which is the correct semantic for a retry.

try:
    with session.begin_nested():
        XComModel.set(...)
except IntegrityError:
    # concurrent write already committed; overwrite with latest value
    session.execute(update(XComModel).where(...).values(value=value, dag_result=dag_result))

Changes

  • airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py
    • Add update to the sqlalchemy import
    • Add IntegrityError import from sqlalchemy.exc
    • Wrap XComModel.set in session.begin_nested() and catch IntegrityError with an UPDATE fallback

nagasrisai added a commit to nagasrisai/airflow that referenced this pull request Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent/retried Execution API XCom writes fail with 409 due to duplicate primary key

1 participant