Repository navigation
Description
Under which category would you file this issue?
Airflow Core
Apache Airflow version
3.4.0rc3 (and main)
What happened and how to reproduce it?
Description
Following up on #71968 and #76547, Connection.get_connection_from_secrets() currently does not accept or forward an optional session parameter to secrets backends.
When internal components or extension points holding an open database transaction read a connection via Connection.get_connection_from_secrets(), they cannot pass their active session. As a result, MetastoreBackend.get_connection() always falls back to opening a separate session via create_session(scoped=False).
This causes an extra connection checkout from the SQLAlchemy pool on every lookup, adding latency and connection overhead in high-throughput loops. Additionally, transactional callers cannot read newly added uncommitted connections within the same transaction.
Affected Code
In airflow-core/src/airflow/models/connection.py:522-583:
@classmethod
def get_connection_from_secrets(cls, conn_id: str, team_name: str | None = None) -> Connection:
...
for secrets_backend in ensure_secrets_loaded():
try:
conn = call_secrets_backend_method(
secrets_backend.get_connection, team_name=team_name, conn_id=conn_id
)By comparison, Variable.get_variable_from_secrets() in airflow-core/src/airflow/models/variable.py:494-536 already accepts *, session: Session | None = None and forwards it to MetastoreBackend.get_variable().
Steps to Reproduce
Calling Connection.get_connection_from_secrets("my_conn", session=session) raises:
TypeError: Connection.get_connection_from_secrets() got an unexpected keyword argument 'session'
Expected Behavior
Connection.get_connection_from_secrets() should accept an optional session: Session | None = None keyword argument and forward it to MetastoreBackend.get_connection() when supported.
Proposed Fix
- Add
*, session: Session | None = Nonekeyword-only parameter toConnection.get_connection_from_secrets()inairflow-core/src/airflow/models/connection.py. - Check
accepts_kwarg(secrets_backend.get_connection, "session")and passsessionwhen callingcall_secrets_backend_method().
I have verified this behavior and am working on a PR with unit tests to add this session propagation.
Are you willing to submit PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
amoghrajesh commentedon Nov 6, 2026
Thanks. This is an improvement, not a bug, so it won't go into 3.4. Feel free to open a PR against main.
uranusjr commentedon Nov 6, 2026
I agree this is not a bug.
3.4.0 added the same for
Variable.get_variable_from_secrets, so this should probably be backported once implemented to match the interface. But all in all this shouldn’t block 3.4.0.meel-ayush commentedon Nov 6, 2026
Makes sense. Opened #76563 against main to mirror the Variable interface. Can be backported later whenever convenient.
meel-ayush commentedon Nov 6, 2026
@uranusjr @amoghrajesh i have the implementation ready but i have reached the open PR limit.
Whenever you or the team have a moment, could you please take a look at my existing open PRs? Once one of those is merged or resolved, I'll be able to mark this new PR as ready for review.
Thanks for your time!