Skip to content

Roll back session between retries in Trigger.clean_unused - #74650

Open
RishabhBajaj97 wants to merge 2 commits into
apache:mainfrom
RishabhBajaj97:fix-triggerer-session-rollback
Open

RishabhBajaj97 wants to merge 2 commits into
apache:mainfrom
RishabhBajaj97:fix-triggerer-session-rollback

Conversation

@RishabhBajaj97

Copy link
Copy Markdown

When the metadata database has a transient disconnect (RDS/Aurora failover, idle-connection drop by the NLB, routine Postgres maintenance), the UPDATE task_instance inside Trigger.clean_unused raises DBAPIError. The run_with_db_retries() wrapper is supposed to absorb this, but it only matches DBAPIError / StaleDataError — and after a DBAPIError, the SQLAlchemy session is left in a pending-rollback state. The next retry attempt reuses that invalidated session and raises PendingRollbackError, which the retry policy does not match. The exception propagates out of clean_unused, the triggerer process exits, and every in-flight deferred trigger it was polling has to be reclaimed by another triggerer, losing its asyncio progress.

The project already has the right pattern in retry_db_transaction (airflow/utils/retries.py), which catches DBAPIError / StaleDataError inside the retry attempt, calls session.rollback(), and re-raises so Tenacity drives the next retry on a clean session. retry_db_transaction can't be applied directly because its docstring says it must not be used with @provide_session (and clean_unused already is), so this PR mirrors the pattern inline.

Validated:

  • New regression test test_clean_unused_rolls_back_session_on_transient_db_error passes with the fix (1 passed in 10.20s) and fails without it (AssertionError: Expected 'rollback' to have been called once. Called 0 times.).
  • Full airflow-core/tests/unit/models/test_trigger.py suite: 49 passed, no regressions.
  • ruff format + ruff check clean.
  • mypy airflow-core/src/airflow/models/trigger.py run inside Breeze: Success: no issues found in 1 source file (host-side mypy prek hook can't set up its venv on this machine due to a glibc mismatch unrelated to the diff).
  • Fast prek hooks (prek run --from-ref main --stage pre-commit) all pass for the matched files; mypy-airflow-core + mypy-devel-common skipped locally and verified via Breeze as noted above.

Related: #70961 is an in-review PR that batches clean_unused; its restructure to with create_session(scoped=False) per retry attempt would incidentally close this issue as a side effect of the rework. This PR is a smaller, targeted fix in case the batching PR takes longer to land.

closes: #74614


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 4.7)

Generated-by: Claude Code (Opus 4.7) following the guidelines

A transient metadata-database disconnect raises DBAPIError and leaves the
SQLAlchemy session in a pending-rollback state. The Tenacity retry wrapper
inside Trigger.clean_unused then reuses the same session on the next attempt
and raises PendingRollbackError, which the retry policy does not match, so
the triggerer process exits. The in-flight deferred triggers it was polling
must be reclaimed by another triggerer, interrupting their asyncio state and
restarting any mid-poll progress.

Mirror the pattern already used by retry_db_transaction: on DBAPIError or
StaleDataError inside the retry attempt, call session.rollback() before
re-raising so Tenacity drives the next retry on a clean session.
@boring-cyborg

boring-cyborg Bot commented Oct 13, 2026

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example Dag that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@luc-pimentel

Copy link
Copy Markdown
Contributor

#70961 merged today and rewrote Trigger.clean_unused. Each retry attempt now runs in its own session from create_session, which rolls back on any exception, so #74614 is already covered on main. That is also why this branch conflicts, and after a rebase there is nothing left to roll back.

Should this one be closed, with a note on #74614 that #70961 fixed it?

I ran a test in Breeze on SQLite that closes the DBAPI connection under the task_instance UPDATE: PendingRollbackError at this PR's base, passes at this head, passes on current main without this change.


Drafted-by: Claude Code 2.1.302; reviewed by @luc-pimentel before posting

This branch has not been deployed

No deployments
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.

Triggerer restarts after a transient metadata database disconnect

2 participants