Skip to content

Conversation

@Ruke3663
Copy link

What do these changes do?

These changes fix a bug in aiohttp.ClientSession that caused "Unclosed connector" warnings when a user-provided connector was used.
The session now correctly distinguishes between internally created and externally provided connectors, ensuring it only closes connectors that it owns.

Are there changes in behavior for the user?

Yes, there is a positive change in behavior.
Users who provide their own TCPConnector instance to a ClientSession will no longer see
ResourceWarning: Unclosed connector warnings when the session is closed.

The lifecycle of the external connector remains entirely under the user’s control, as expected.
The behavior for internally created connectors is unchanged.

Is it a substantial burden for the maintainers to support this?

No, this change should not be a burden to maintain.
It corrects the logic to align with the library’s intended design and documentation.

The fix is:

  • Small and localized to the ClientSession’s __init__ and close methods.
  • Improves predictability and robustness.
  • Relies on existing flags and introduces no new complexity.

This should be easy to support long-term.

Related issue number

"2e6: With explicit connector reuse, aiohttp.ClientSession fails to close connector when using async context"

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Oct 20, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 20, 2025

CodSpeed Performance Report

Merging #11696 will improve performances by 12.65%

Comparing Ruke3663:solution/2e6eba86-5af3-4080-9a66-c77ca8c82117-v1 (c60da99) with master (42fc48a)1

Summary

⚡ 1 improvement
✅ 58 untouched

Benchmarks breakdown

Benchmark BASE HEAD Change
test_one_hundred_simple_get_requests_no_session[pyloop] 87.5 ms 77.7 ms +12.65%

Footnotes

  1. No successful run was found on master (0a840da) during the generation of this report, so 42fc48a was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Ruke3663 Ruke3663 changed the title Solution/2e6eba86 5af3 4080 9a66 c77ca8c82117 v1 BugFix in aiohttp.ClientSession that caused "Unclosed connector" warnings Oct 20, 2025
)

if connector is None:
connector_owner = True
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this is the only line that is potentially related to the title of this PR? Though surely only fixing an issue where someone incorrectly passes connector_owner=False without a connector (in which case we should probably error on the incorrect arguments, rather than just override it).

@Dreamsorcerer
Copy link
Member

We'll need a regression test that reproduces the reported issue too.

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

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants