Skip to content

Allow Connection.get_connection_from_secrets to accept and propagate caller session #76553

Open

Description

@meel-ayush

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

  1. Add *, session: Session | None = None keyword-only parameter to Connection.get_connection_from_secrets() in airflow-core/src/airflow/models/connection.py.
  2. Check accepts_kwarg(secrets_backend.get_connection, "session") and pass session when calling call_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

Activity

  1. added
    kind:bug
    needs-triage
    on Nov 5, 2026
  2. amoghrajesh commented on Nov 6, 2026

    @amoghrajesh
    Contributor

    Thanks. This is an improvement, not a bug, so it won't go into 3.4. Feel free to open a PR against main.

  3. added
    kind:feature
    and removed
    kind:bug
    needs-triage
    on Nov 6, 2026
  4. added this to the Airflow 3.4.1 milestone on Nov 6, 2026
  5. uranusjr commented on Nov 6, 2026

    @uranusjr
    Member

    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.

  6. meel-ayush commented on Nov 6, 2026

    @meel-ayush
    ContributorAuthor

    Makes sense. Opened #76563 against main to mirror the Variable interface. Can be backported later whenever convenient.

  7. meel-ayush commented on Nov 6, 2026

    @meel-ayush
    ContributorAuthor

    @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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:feature

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions