Skip to content

fix: leak of aiodns.DNSResolver when ClientSession is closed #11150

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

Merged
merged 3 commits into from
Jun 6, 2025

Conversation

Tasssadar
Copy link
Contributor

@Tasssadar Tasssadar commented Jun 6, 2025

Resolvers were never closed when TCPConnector is closed. Thus, _DNSResolverManager.release_resolver is never called, keeping the resolver referenced from _loop_data, which prevents pycares from garbage collecting it.

Regression introduced in 3.12.0 in #10897

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Jun 6, 2025
@Tasssadar Tasssadar force-pushed the fix_resolver_leak branch from 362d170 to 11763e2 Compare June 6, 2025 10:49
@bdraco bdraco added backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot labels Jun 6, 2025
Copy link

codspeed-hq bot commented Jun 6, 2025

CodSpeed Performance Report

Merging #11150 will degrade performances by 10.1%

Comparing Tasssadar:fix_resolver_leak (129fb90) with master (0544f11)

Summary

❌ 1 (👁 1) regressions
✅ 58 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
👁 test_one_hundred_simple_get_requests_no_session[pyloop] 78.4 ms 87.2 ms -10.1%

Resolvers were never closed when TCPConnector is closed.
Thus, _DNSResolverManager.release_resolver is never called,
keeping the resolver referenced from _loop_data, which prevents
pycares from garbage collecting it.

Regression introduced in 3.12.0 in aio-libs#10897
@Tasssadar Tasssadar force-pushed the fix_resolver_leak branch from 11763e2 to 77cec38 Compare June 6, 2025 10:59
Copy link

codecov bot commented Jun 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.85%. Comparing base (0544f11) to head (129fb90).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11150      +/-   ##
==========================================
- Coverage   98.85%   98.85%   -0.01%     
==========================================
  Files         131      131              
  Lines       42712    42737      +25     
  Branches     2307     2308       +1     
==========================================
+ Hits        42225    42248      +23     
- Misses        337      340       +3     
+ Partials      150      149       -1     
Flag Coverage Δ
CI-GHA 98.73% <100.00%> (-0.01%) ⬇️
OS-Linux 98.46% <100.00%> (-0.01%) ⬇️
OS-Windows 96.78% <100.00%> (-0.01%) ⬇️
OS-macOS 97.69% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 97.45% <100.00%> (-0.01%) ⬇️
Py-3.10.17 97.95% <100.00%> (-0.01%) ⬇️
Py-3.11.12 98.11% <100.00%> (-0.02%) ⬇️
Py-3.11.9 97.63% <100.00%> (-0.01%) ⬇️
Py-3.12.10 98.50% <100.00%> (-0.02%) ⬇️
Py-3.13.3 98.48% <100.00%> (-0.02%) ⬇️
Py-3.9.13 97.34% <100.00%> (-0.01%) ⬇️
Py-3.9.22 97.82% <100.00%> (-0.02%) ⬇️
Py-pypy7.3.16 93.95% <100.00%> (-2.22%) ⬇️
VM-macos 97.69% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.46% <100.00%> (-0.01%) ⬇️
VM-windows 96.78% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bdraco
Copy link
Member

bdraco commented Jun 6, 2025

Thanks for the PR. I'll clear the perf regression once the CI passes. Its going to be expected with this fix.

@bdraco bdraco enabled auto-merge (squash) June 6, 2025 11:20
@bdraco bdraco disabled auto-merge June 6, 2025 11:26
@bdraco
Copy link
Member

bdraco commented Jun 6, 2025

close/reopen to try to get the docs runner to work

@bdraco bdraco closed this Jun 6, 2025
@bdraco bdraco reopened this Jun 6, 2025
@bdraco bdraco enabled auto-merge (squash) June 6, 2025 11:30
@bdraco bdraco merged commit 996ad00 into aio-libs:master Jun 6, 2025
64 of 73 checks passed
Copy link
Contributor

patchback bot commented Jun 6, 2025

Backport to 3.12: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 996ad00 on top of patchback/backports/3.12/996ad00037d9e729aa58b757099dd2fea5e7dcc9/pr-11150

Backporting merged PR #11150 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.12/996ad00037d9e729aa58b757099dd2fea5e7dcc9/pr-11150 upstream/3.12
  4. Now, cherry-pick PR fix: leak of aiodns.DNSResolver when ClientSession is closed #11150 contents into that branch:
    $ git cherry-pick -x 996ad00037d9e729aa58b757099dd2fea5e7dcc9
    If it'll yell at you with something like fatal: Commit 996ad00037d9e729aa58b757099dd2fea5e7dcc9 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 996ad00037d9e729aa58b757099dd2fea5e7dcc9
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR fix: leak of aiodns.DNSResolver when ClientSession is closed #11150 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.12/996ad00037d9e729aa58b757099dd2fea5e7dcc9/pr-11150
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

Copy link
Contributor

patchback bot commented Jun 6, 2025

Backport to 3.13: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 996ad00 on top of patchback/backports/3.13/996ad00037d9e729aa58b757099dd2fea5e7dcc9/pr-11150

Backporting merged PR #11150 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.13/996ad00037d9e729aa58b757099dd2fea5e7dcc9/pr-11150 upstream/3.13
  4. Now, cherry-pick PR fix: leak of aiodns.DNSResolver when ClientSession is closed #11150 contents into that branch:
    $ git cherry-pick -x 996ad00037d9e729aa58b757099dd2fea5e7dcc9
    If it'll yell at you with something like fatal: Commit 996ad00037d9e729aa58b757099dd2fea5e7dcc9 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 996ad00037d9e729aa58b757099dd2fea5e7dcc9
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR fix: leak of aiodns.DNSResolver when ClientSession is closed #11150 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.13/996ad00037d9e729aa58b757099dd2fea5e7dcc9/pr-11150
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@bdraco
Copy link
Member

bdraco commented Jun 6, 2025

@Tasssadar Please let me know if you have any trouble with the backport. I can do another release tomorrow.

Tasssadar added a commit to Tasssadar/aiohttp that referenced this pull request Jun 6, 2025
Tasssadar added a commit to Tasssadar/aiohttp that referenced this pull request Jun 6, 2025
…olver when ClientSession is closed (aio-libs#11150)

Co-authored-by: J. Nick Koston <[email protected]>
(cherry picked from commit 996ad00)
Tasssadar added a commit to Tasssadar/aiohttp that referenced this pull request Jun 6, 2025
…olver when ClientSession is closed (aio-libs#11150)

Co-authored-by: J. Nick Koston <[email protected]>
(cherry picked from commit 996ad00)
Tasssadar added a commit to Tasssadar/aiohttp that referenced this pull request Jun 6, 2025
…olver when ClientSession is closed

Co-authored-by: J. Nick Koston <[email protected]>
(cherry picked from commit 996ad00)
bdraco added a commit that referenced this pull request Jun 7, 2025
…en ClientSession is closed (#11151)

Co-authored-by: J. Nick Koston <[email protected]>
bdraco added a commit that referenced this pull request Jun 7, 2025
…en ClientSession is closed (#11152)

Co-authored-by: J. Nick Koston <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot 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