You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "RishabhBajaj97 (via GitHub)" <gi...@apache.org> on 2026/10/13 13:31:20 UTC
[PR] Roll back session between retries in Trigger.clean_unused [airflow]
RishabhBajaj97 opened a new pull request, #74650:
URL: https://github.com/apache/airflow/pull/74650
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?
- [X] Yes — Claude Code (Opus 4.7)
Generated-by: Claude Code (Opus 4.7) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Roll back session between retries in Trigger.clean_unused [airflow]
Posted by "luc-pimentel (via GitHub)" <gi...@apache.org>.
luc-pimentel commented on PR #74650:
URL: https://github.com/apache/airflow/pull/74650#issuecomment-6251891060
#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 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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Roll back session between retries in Trigger.clean_unused [airflow]
Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #74650:
URL: https://github.com/apache/airflow/pull/74650#issuecomment-6146706792
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](https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
Here are some useful points:
- Pay attention to the quality of your code (ruff, mypy and type annotations). Our [prek-hooks]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-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](https://github.com/apache/airflow/blob/main/airflow-core/docs/howto/custom-operator.rst) Consider adding an example Dag that shows how users should use it.
- Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) 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](https://www.apache.org/foundation/policies/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]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
- 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
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org