Skip to content

Fix: Unclosed aiohttp ClientSession and TCPConnector in DatabricksRunNowOperator (deferrable=True) #52119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

SalikramPaudel
Copy link
Contributor

@SalikramPaudel SalikramPaudel commented Jun 23, 2025

Closes: #51910

What this PR does

Fixes unclosed aiohttp.ClientSession and TCPConnector warnings when using DatabricksRunNowOperator with deferrable=True in Airflow 3.0.2 and Databricks Provider 7.4.0.

Background

As described in #51910, the following errors appear during deferrable task execution:

Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x...>

Unclosed connector
connector: <aiohttp.connector.TCPConnector object at 0x...>

These indicate improper async resource cleanup during trigger polling.

Fix

  • Ensures aiohttp.ClientSession and TCPConnector are properly closed
  • Applies best practices for async resource lifecycle management in the trigger

Status

  • Initial fix implemented
  • Unit tests in progress
  • Manual testing in progress

Opening as a Draft PR to allow early feedback.

Copy link

boring-cyborg bot commented Jun 23, 2025

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 pre-commits 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 Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment 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 for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • 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: [email protected]
    Slack: https://s.apache.org/airflow-slack

@albertwangnz
Copy link

Awesome, @SalikramPaudel. Thank you for the fix. Cannot wait for upgrading to this version :-)

@SalikramPaudel SalikramPaudel marked this pull request as ready for review June 25, 2025 17:02
@SalikramPaudel SalikramPaudel changed the title [WIP] Fix: Unclosed aiohttp ClientSession and TCPConnector in DatabricksRunNowOperator (deferrable=True) Fix: Unclosed aiohttp ClientSession and TCPConnector in DatabricksRunNowOperator (deferrable=True) Jun 25, 2025
@SalikramPaudel
Copy link
Contributor Author

@potiuk ,
I've submitted this PR as part of the effort to resolve issue #51910

Whenever you have a moment, please take a look and share your feedback. I'd appreciate your review. Thanks in advance for your time.

@potiuk
Copy link
Member

potiuk commented Jun 26, 2025

cc: @pankajkoti => since you are working on this operator's Airflow 3 compatibility, maybe you can take a look as well ?

@pankajkoti pankajkoti force-pushed the fix/unclosed-aiohttp-session-azure-databricks branch from 6fba668 to ea2ead1 Compare June 27, 2025 07:03
Copy link
Member

@pankajkoti pankajkoti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for your great first contribution @SalikramPaudel 👏🏽

SalikramPaudel and others added 4 commits June 27, 2025 13:01
…redentials

When using `DatabricksRunNowOperator` with `deferrable=True` and Azure authentication,
unclosed aiohttp sessions were reported for both `ClientSecretCredential` and `ManagedIdentityCredential`.

This fix ensures that:
- `ClientSecretCredential` is used within an `async with` block to auto-close the session safely.
- `ManagedIdentityCredential` is also used with `async with` (supported in azure-identity >= 1.7.0b2).

This prevents runtime warnings like:
- "Unclosed client session"
- "Unclosed connector"

The fix improves resource handling and aligns with Azure SDK best practices.
…manager flow

Production now wraps `azure.identity.aio.ClientSecretCredential` in
`async with`, so patching only `__init__`/`get_token` left the real
`__aenter__` to explode with “AttributeError: _client”.
The tests are refactored to:

* Patch the credential class itself instead of individual methods.
* Return a fully mocked object that implements
  `__aenter__`, `__aexit__`, and `get_token`, preserving existing
  assertions on constructor kwargs and token scope.
* Apply the same change to both
  `TestDatabricksHookAsyncAadTokenOtherClouds` and
  `TestDatabricksHookAsyncAadTokenSpOutside`.

No production code is affected; the update silences the failing async
test suites introduced by the resource-cleanup PR.
@pankajkoti pankajkoti force-pushed the fix/unclosed-aiohttp-session-azure-databricks branch from ea2ead1 to 1d1b4e4 Compare June 27, 2025 07:31
@pankajkoti pankajkoti merged commit 3c1568a into apache:main Jun 27, 2025
18 checks passed
Copy link

boring-cyborg bot commented Jun 27, 2025

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

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

Successfully merging this pull request may close these issues.

Unclosed aiohttp ClientSession and TCPConnector in DatabricksRunNowOperator with deferrable=True (Airflow 3.0.2, Databricks Provider 7.4.0)
4 participants