Retry transient database errors in the metastore secrets backend - #71307
Open
1fanwang wants to merge 1 commit into
Open
Retry transient database errors in the metastore secrets backend#713071fanwang wants to merge 1 commit into
1fanwang wants to merge 1 commit into
Conversation
A momentary metadata-database failure during a connection or variable lookup is currently indistinguishable from the secret not existing. Both get_connection_from_secrets and get_variable_from_secrets swallow every exception a backend raises, log it at debug level, and fall through to the next backend, so a dropped connection, a failover, or a deadlock surfaces to the caller as "the conn_id isn't defined" or a 404 rather than as a retryable server error. MetastoreBackend is the one backend in the default chain that talks to the metadata database, and Airflow already has retry_db_transaction for exactly this class of failure. Signed-off-by: 1fanwang <1fannnw@gmail.com>
1fanwang
force-pushed
the
metastore-secret-retry
branch
from
August 7, 2026 18:17
dba6ec3 to
b3702de
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MetastoreBackendreads connections and variables from the metadata database with no retry. Every caller that wraps it (Connection.get_connection_from_secrets,Variable.get_variable_from_secrets, and the Task SDK's server-context lookup) catchesException, logs it at debug level, and falls through to the next backend. A transient database failure therefore reaches the caller as the secret does not exist.Why it matters
The Execution API answers 404 for a connection or variable that is present in the database. 404 is permanent, so the Task SDK does not retry and the task fails with
The conn_id 'x' isn't defined. The real cause is visible only at debug level.Two amplifiers. Under
[secrets] use_cache = Truethe resultingNoneis cached forcache_ttl_seconds(seevariable.py:498, which notes "we save None as well"), so one blip poisons the key. The triggerer, Dag processor, and callback supervisor call this backend in-process, so deferred tasks hit the same bogus not-found.pool_pre_pingdoes not cover this. It recycles a stale pooled connection, but the failure here lands on the reconnect itself.The fix
Apply the existing
retry_db_transactionunder@provide_session, so retries run within one session with a rollback between attempts. That is the stacking already used inrenderedtifields.py:241,dagwarning.py:78, andmanager.py:701. Both lookups are reads, the budget is the existing[database] max_db_retries, and a missing secret still returnsNoneon the first attempt.Testing Done
Real Postgres, real psycopg2, real TCP-level failure. Airflow connects through a local forwarder;
blip()drops the open connections and refuses exactly the next connection attempt, then serves normally. One failover-shaped blip, so the outcome is deterministic rather than a race against the backoff.Setup
docker run -d --name af-e2e-pg -e POSTGRES_PASSWORD=airflow \ -e POSTGRES_USER=airflow -e POSTGRES_DB=airflow -p 55432:5432 postgres:16 export AIRFLOW_HOME=/tmp/af-e2e-metastore export AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow:airflow@127.0.0.1:55433/airflow python repro.py --setup # airflow db migrate, then seed e2e_conn and e2e_var python repro.pyrepro.pyBefore, on
origin/mainThe swallowed driver error (debug level)
After, on this branch
Three consecutive runs each way:
Regression tests in
airflow-core/tests/unit/always/test_secrets_metastore.pydrive a real session against a real database and fail on unpatched source.