Skip to content

Remove airflow.utils.session references in task-sdk #54710

Open

Description

@amoghrajesh

Body

Current Usage:
task-sdk/src/airflow/sdk/definitions/dag.py

Committer

  • I acknowledge that I am a maintainer/committer of the Apache Airflow project.

Activity

  1. added this to the Airflow 3.1.0 milestone on Aug 20, 2025
  2. added
    kind:meta
    on Aug 20, 2025
  3. aayushbisen commented on Aug 21, 2025

    @aayushbisen
    Contributor

    Hey @amoghrajesh I would like to work on this issue.

  4. potiuk commented on Aug 21, 2025

    @potiuk
    Member

    Feel free.

  5. aayushbisen commented on Aug 22, 2025

    @aayushbisen
    Contributor

    Where should the session utility be moved from the current folder? I am thinking like

    # Before:
    from airflow.utils.session import create_session
    
    # After:
    from airflow.sdk.utils.session import create_session
  6. aayushbisen commented on Aug 26, 2025

    @aayushbisen
    Contributor

    Existing use of session util

    The _run_task() helper function was creating its own database session using:

    from airflow.utils.session import create_session
    
    # Later in the code:
    with create_session() as session:
        ti.state = State.SCHEDULED
        session.add(ti)

    This created an unwanted dependency on airflow core utilities from within the task-sdk.

    Proposed Solution

    Refactored the session management to reuse the existing session from the parent test() method instead of creating a new one:

    1. Modified function signature: Added optional session=None parameter to _run_task()
    2. Removed problematic import: Eliminated from airflow.utils.session import create_session
    3. Updated session usage: Replaced context manager with direct session usage when provided
    4. Updated function call: Pass the existing session from the test() method

    Before:

    def _run_task(*, ti, task, run_triggerer=False):
        # ...
        if ti.state == State.DEFERRED and isinstance(msg, DeferTask) and run_triggerer:
            from airflow.utils.session import create_session
            # ...
            with create_session() as session:
                ti.state = State.SCHEDULED
                session.add(ti)

    After:

    def _run_task(*, ti, task, run_triggerer=False, session=None):
        # ...
        if ti.state == State.DEFERRED and isinstance(msg, DeferTask) and run_triggerer:
            # ...
            if session is not None:
                ti.state = State.SCHEDULED
                session.add(ti)

    @amoghrajesh let me know if this looks okay

  7. self-assigned this
    on Dec 1, 2025
  8. potiuk commented on Mar 2, 2026

    @potiuk
    Member

    @aayushbisen We are unassigning you from this issue as part of our updated assignment policy.

    This is not meant to discourage your contribution — quite the opposite! You are still very welcome to work on this issue and submit a PR for it. Simply comment that you are working on it and open a PR when ready.

    We found that formal assignments were not working well, as they often prevented others from contributing when the assignee was not actively working on the issue.

  9. amoghrajesh commented on Jun 15, 2026

    @amoghrajesh
    ContributorAuthor

    Wasn't committed for Airflow 3.3, moving to Airflow 3.4 as best effort work.

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

    area:corekind:meta

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions