Skip to content
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

Refactor connection waiters to be cancellation safe #9671

Merged
merged 72 commits into from
Nov 5, 2024
Merged

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Nov 4, 2024

A deadlock that could occur while attempting to get a new connection slot after a timeout.

The connector was not cancellation-safe.

fixes #9670

will need to backport #9600 to 3.10 as well

Copy link

codspeed-hq bot commented Nov 4, 2024

CodSpeed Performance Report

Merging #9671 will not alter performance

Comparing race_safe_waiter (72c1cba) with master (5b654d5)

Summary

✅ 13 untouched benchmarks

aiohttp/connector.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.65%. Comparing base (5b654d5) to head (72c1cba).
Report is 1 commits behind head on master.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #9671    +/-   ##
========================================
  Coverage   98.65%   98.65%            
========================================
  Files         116      116            
  Lines       35482    35594   +112     
  Branches     4211     4221    +10     
========================================
+ Hits        35003    35116   +113     
  Misses        321      321            
+ Partials      158      157     -1     
Flag Coverage Δ
CI-GHA 98.54% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.21% <100.00%> (+<0.01%) ⬆️
OS-Windows 96.11% <100.00%> (+0.01%) ⬆️
OS-macOS 97.42% <100.00%> (+0.02%) ⬆️
Py-3.10.11 97.28% <99.56%> (+0.01%) ⬆️
Py-3.10.15 97.70% <99.56%> (+0.01%) ⬆️
Py-3.11.10 97.76% <100.00%> (+0.01%) ⬆️
Py-3.11.9 97.35% <100.00%> (+<0.01%) ⬆️
Py-3.12.7 98.24% <100.00%> (+<0.01%) ⬆️
Py-3.13.0 98.23% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 97.18% <100.00%> (+0.01%) ⬆️
Py-3.9.20 97.61% <100.00%> (+0.01%) ⬆️
Py-pypy7.3.16 97.23% <100.00%> (+0.01%) ⬆️
VM-macos 97.42% <100.00%> (+0.02%) ⬆️
VM-ubuntu 98.21% <100.00%> (+<0.01%) ⬆️
VM-windows 96.11% <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.

aiohttp/connector.py Outdated Show resolved Hide resolved
@bdraco
Copy link
Member Author

bdraco commented Nov 4, 2024

#9670 No longer reproducible with these changes .. but now we can go over the limit.

Previously it was over-enforcing. Now its under-enforcing

@bdraco
Copy link
Member Author

bdraco commented Nov 4, 2024

The core problem is we have three sources of truth for limits and there is no synchronization between them

self._acquired, self._acquired_per_host, self._waiters

@bdraco
Copy link
Member Author

bdraco commented Nov 4, 2024

Any await after calling self._available_connections invalidates it because it could change after that

@bdraco
Copy link
Member Author

bdraco commented Nov 4, 2024

This is turning out to be really hard to fix with the current design

tests/test_connector.py Fixed Show fixed Hide fixed
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Nov 5, 2024
CHANGES/9671.bugfix.rst Outdated Show resolved Hide resolved
@bdraco bdraco marked this pull request as ready for review November 5, 2024 15:06
aiohttp/connector.py Outdated Show resolved Hide resolved
@bdraco
Copy link
Member Author

bdraco commented Nov 5, 2024

Screenshot 2024-11-05 at 9 14 48 AM

Still a tiny perf regression in _get from calling it more often

@bdraco
Copy link
Member Author

bdraco commented Nov 5, 2024

Testing looks good, and it looks like we aren't regressing performance now

@bdraco bdraco merged commit 37d9fe6 into master Nov 5, 2024
40 of 41 checks passed
@bdraco bdraco deleted the race_safe_waiter branch November 5, 2024 16:12
Copy link
Contributor

patchback bot commented Nov 5, 2024

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

❌ Failed to cleanly apply 37d9fe6 on top of patchback/backports/3.10/37d9fe68387e3672604ff5b183f48538bca34f16/pr-9671

Backporting merged PR #9671 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.10/37d9fe68387e3672604ff5b183f48538bca34f16/pr-9671 upstream/3.10
  4. Now, cherry-pick PR Refactor connection waiters to be cancellation safe #9671 contents into that branch:
    $ git cherry-pick -x 37d9fe68387e3672604ff5b183f48538bca34f16
    If it'll yell at you with something like fatal: Commit 37d9fe68387e3672604ff5b183f48538bca34f16 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 37d9fe68387e3672604ff5b183f48538bca34f16
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Refactor connection waiters to be cancellation safe #9671 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.10/37d9fe68387e3672604ff5b183f48538bca34f16/pr-9671
  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 Nov 5, 2024

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

❌ Failed to cleanly apply 37d9fe6 on top of patchback/backports/3.11/37d9fe68387e3672604ff5b183f48538bca34f16/pr-9671

Backporting merged PR #9671 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.11/37d9fe68387e3672604ff5b183f48538bca34f16/pr-9671 upstream/3.11
  4. Now, cherry-pick PR Refactor connection waiters to be cancellation safe #9671 contents into that branch:
    $ git cherry-pick -x 37d9fe68387e3672604ff5b183f48538bca34f16
    If it'll yell at you with something like fatal: Commit 37d9fe68387e3672604ff5b183f48538bca34f16 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 37d9fe68387e3672604ff5b183f48538bca34f16
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Refactor connection waiters to be cancellation safe #9671 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.11/37d9fe68387e3672604ff5b183f48538bca34f16/pr-9671
  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 added a commit that referenced this pull request Nov 5, 2024
bdraco added a commit that referenced this pull request Nov 5, 2024
bdraco added a commit that referenced this pull request Nov 5, 2024
bdraco added a commit that referenced this pull request Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.11 Trigger automatic backporting to the 3.11 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.

deadlock with TCPConnector limit after timeout
2 participants