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

Close the socket if there's a failure in start_connection() #10464

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

top-oai
Copy link

@top-oai top-oai commented Feb 14, 2025

What do these changes do?

Over in MagicStack/uvloop#653 in issue is described where after upgrading from aiohttp 3.9.5 -> 3.10.5 (but same thing on 3.11.11), under certain conditions where exceptions are raised at the right/wrong time, continued use of aiohttp can result in File descriptor 2877 is used by transport errors.

I've been able to repro with a small script (gist) that starts/stops/cancels many connections repeatedly. I can repro on at least Ubuntu 24 x86, aiohttp 3.11.11 and uvloop 0.21.0. This fix resolves the errors in my repro script.

I was unable to produce a detailed end-to-end explanation for what's happening here, but I do know that we enter the error state when an exception is thrown while inside of uvloop's create_connection function, here:

https://github.com/MagicStack/uvloop/blob/7bb12a174884b2ec8b3162a08564e5fb8a5c6b39/uvloop/loop.pyx#L2066

In that case, if sock.close() is not called (this pr changes that), uvloop seems to erroneously hold on to a ref to the transport when the underlying file descriptor has been relinquished to the os.

It feels like at its core a bug in uvloop, however this at least works around it in aiohttp.

Are there changes in behavior for the user?

No... Aside from this error not reproducing.

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

Unless I've made a mistake (it is a very small change though), I think this is a pure bug fix.

Related issue number

MagicStack/uvloop#653

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES/ folder
    • name it <issue_or_pr_num>.<type>.rst (e.g. 588.bugfix.rst)

    • if you don't have an issue number, change it to the pull request
      number after creating the PR

      • .bugfix: A bug fix for something the maintainers deemed an
        improper undesired behavior that got corrected to match
        pre-agreed expectations.
      • .feature: A new behavior, public APIs. That sort of stuff.
      • .deprecation: A declaration of future API removals and breaking
        changes in behavior.
      • .breaking: When something public is removed in a breaking way.
        Could be deprecated in an earlier release.
      • .doc: Notable updates to the documentation structure or build
        process.
      • .packaging: Notes for downstreams about unobvious side effects
        and tooling. Changes in the test invocation considerations and
        runtime assumptions.
      • .contrib: Stuff that affects the contributor experience. e.g.
        Running tests, building the docs, setting up the development
        environment.
      • .misc: Changes that are hard to assign to any of the above
        categories.
    • Make sure to use full sentences with correct case and punctuation,
      for example:

      Fixed issue with non-ascii contents in doctest text files
      -- by :user:`contributor-gh-handle`.

      Use the past tense or the present tense a non-imperative mood,
      referring to what's changed compared to the last released version
      of this project.

@top-oai top-oai requested a review from asvetlov as a code owner February 14, 2025 05:20
@bdraco
Copy link
Member

bdraco commented Feb 14, 2025

It looks like the fix for this in uvloop has stalled out

MagicStack/uvloop#645
MagicStack/uvloop#646

This comment was marked as outdated.

@bdraco bdraco added backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot labels Feb 14, 2025
@bdraco
Copy link
Member

bdraco commented Feb 14, 2025

Looks like there is a test that will need to be adjusted as well

@bdraco
Copy link
Member

bdraco commented Feb 14, 2025

Might also be the same issue reported here MagicStack/uvloop#631

@bdraco
Copy link
Member

bdraco commented Feb 14, 2025

The original aiohappyeyeballs issue is 5mo old

@bdraco
Copy link
Member

bdraco commented Feb 14, 2025

I'm really starting to get the feeling uvloop isn't going go to fix this issue anytime soon as there are multiple comments saying they will look at it shortly but it doesn't happen. That's a bit frustrating but it's up to them how they want to prioritize.

Edit: it looks like similar issue MagicStack/uvloop#631 has been sitting for nearly 6 months as well

@bdraco
Copy link
Member

bdraco commented Feb 14, 2025

It looks like one of the connector tests is failing with this change.

@top-oai
Copy link
Author

top-oai commented Feb 14, 2025

Oh, thanks a lot for all the context...

Yeah this sounds good, I'll circle back to look at the test and add a comment and generally polish this up tomorrow.

@Dreamsorcerer
Copy link
Member

I'm really starting to get the feeling uvloop isn't going go to fix this issue anytime soon as there are multiple comments saying they will look at it shortly but it doesn't happen. That's a bit frustrating but it's up to them how they want to prioritize.

It looks like they are just not around at the moment, there appears to be no activity at all since just after that PR was opened. When they get back to the project, it'll probably be the first thing they do...
I'd much rather uvloop is fixed than need to maintain hacks here.

@top-oai top-oai requested a review from webknjaz as a code owner February 16, 2025 01:35
@top-oai
Copy link
Author

top-oai commented Feb 16, 2025

Noticed the argument about whether it's worth it to maintain this complexity or not, polished up the PR anyway (fixed tests, added new test, added changelog) just so that it's actually ready if we do want it.

It looks like they are just not around at the moment, there appears to be no activity at all since just after that PR was opened. When they get back to the project, it'll probably be the first thing they do...
I'd much rather uvloop is fixed than need to maintain hacks here.

Yeah that makes sense. A counterargument here is that this generally hardens aiohttp by making it robust against event loop implementations. Also, even with a correct event loop implementation this would enable the socket destruction to potentially be hastened in this case where we know it's garbage.

In any case this PR would unblock our aiohttp upgrade, since we are already depending on uvloop... but waiting for uvloop to merge the fix on their end certainly works too.

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Feb 16, 2025
Copy link

codecov bot commented Feb 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.69%. Comparing base (e29d989) to head (b3fcd38).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10464      +/-   ##
==========================================
- Coverage   98.70%   98.69%   -0.01%     
==========================================
  Files         122      122              
  Lines       37180    37203      +23     
  Branches     2058     2056       -2     
==========================================
+ Hits        36698    36718      +20     
- Misses        335      338       +3     
  Partials      147      147              
Flag Coverage Δ
CI-GHA 98.58% <100.00%> (-0.01%) ⬇️
OS-Linux 98.25% <100.00%> (-0.01%) ⬇️
OS-Windows 96.18% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.37% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 97.27% <100.00%> (-0.01%) ⬇️
Py-3.10.16 97.82% <100.00%> (+<0.01%) ⬆️
Py-3.11.11 97.90% <100.00%> (-0.01%) ⬇️
Py-3.11.9 97.36% <100.00%> (-0.01%) ⬇️
Py-3.12.8 97.16% <100.00%> (-0.33%) ⬇️
Py-3.12.9 98.34% <100.00%> (+<0.01%) ⬆️
Py-3.13.1 97.15% <100.00%> (-0.32%) ⬇️
Py-3.13.2 98.33% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 97.16% <100.00%> (-0.01%) ⬇️
Py-3.9.21 97.69% <100.00%> (-0.01%) ⬇️
Py-pypy7.3.16 97.29% <100.00%> (-0.01%) ⬇️
VM-macos 97.37% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.25% <100.00%> (-0.01%) ⬇️
VM-windows 96.18% <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

bdraco commented Feb 16, 2025

I'm fine with this change given explicitly closing the socket is still a good practice.

@bdraco
Copy link
Member

bdraco commented Feb 16, 2025

I'd like to get a 👍 or at a comment from @Dreamsorcerer that he doesn't wan to hold this PR before merging.

CHANGES/10464.bugfix.rst Outdated Show resolved Hide resolved
tests/test_connector.py Outdated Show resolved Hide resolved
@Dreamsorcerer
Copy link
Member

I'd like to get a 👍 or at a comment from @Dreamsorcerer that he doesn't wan to hold this PR before merging.

I've made one last attempt to ping the uvloop maintainers. If there's no response in a couple of days, then I guess we should merge this.

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 backport-3.12 Trigger automatic backporting to the 3.12 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.

3 participants