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

Fix timer handle churn in websocket heartbeat #8608

Merged
merged 35 commits into from
Aug 7, 2024
Merged

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Aug 5, 2024

What do these changes do?

Each message would cancel and reset the timer handler which meant asyncio would have to rebuild the whole schedule heap frequently because so many timer handlers were being created and than cancelled.

When asyncio timer handle cancels reach the threadhold the whole heap has to be rebuilt
https://github.com/python/cpython/blob/1422500d020bd199b26357fc387f8b79b82226cd/Lib/asyncio/base_events.py#L1968 which is extremely inefficent.

To solve this, when a timer handle is already running, instead of cancelling it, we let it fire and reschedule if heartbeat would be sent to early.

The websocket ping/pong code was missing some coverage in this area so there are some new tests as well.

Are there changes in behavior for the user?

no

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

no

Each message would cancel and reset the timer handler which
meant asyncio would have to rebuild the whole schedule heap
frequently because so many timer handlers were being created
and than cancelled.

When asyncio timer handle cancels reach the threadhold the
whole heap has to be rebuilt
https://github.com/python/cpython/blob/1422500d020bd199b26357fc387f8b79b82226cd/Lib/asyncio/base_events.py#L1968
which is extremely inefficent.

To solve this, when a timer handle is already running, instead
of cancelling it, we let it fire and reschedule if heartbeat
would be sent to early.
aiohttp/client_ws.py Outdated Show resolved Hide resolved
aiohttp/web_ws.py Outdated Show resolved Hide resolved
@bdraco bdraco added backport-3.10 Trigger automatic backporting to the 3.10 release branch by Patchback robot backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot labels Aug 5, 2024
Copy link

codecov bot commented Aug 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.87%. Comparing base (b2691f2) to head (f9c6027).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8608      +/-   ##
==========================================
- Coverage   97.90%   97.87%   -0.04%     
==========================================
  Files         107      107              
  Lines       33515    33669     +154     
  Branches     3941     3952      +11     
==========================================
+ Hits        32813    32952     +139     
- Misses        518      533      +15     
  Partials      184      184              
Flag Coverage Δ
CI-GHA 97.79% <100.00%> (-0.03%) ⬇️
OS-Linux 97.45% <100.00%> (-0.03%) ⬇️
OS-Windows 95.87% <100.00%> (+0.01%) ⬆️
OS-macOS 97.17% <98.48%> (+0.02%) ⬆️
Py-3.10.11 97.32% <98.48%> (+0.02%) ⬆️
Py-3.10.14 97.26% <100.00%> (+0.03%) ⬆️
Py-3.11.9 97.49% <100.00%> (+0.03%) ⬆️
Py-3.12.4 97.62% <100.00%> (+0.03%) ⬆️
Py-3.8.10 95.64% <100.00%> (+0.01%) ⬆️
Py-3.8.18 97.16% <100.00%> (+0.03%) ⬆️
Py-3.9.13 97.31% <98.48%> (+0.02%) ⬆️
Py-3.9.19 97.25% <100.00%> (+0.03%) ⬆️
Py-pypy7.3.16 ?
VM-macos 97.17% <98.48%> (+0.02%) ⬆️
VM-ubuntu 97.45% <100.00%> (-0.03%) ⬇️
VM-windows 95.87% <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 Author

bdraco commented Aug 5, 2024

We don't have any tests for when the heartbeat would get rescheduled. Need to write some.

@bdraco
Copy link
Member Author

bdraco commented Aug 5, 2024

95.2% reduction in websocket client timer handler churn. That's even with the downstream libs that already have their own ping/pong implementation.

I'm happy with this. I'll let it bake overnight and than write some tests tomorrow if I don't have any more fires from my day job to put out.

@bdraco
Copy link
Member Author

bdraco commented Aug 5, 2024

95.1% reduction on the other instance I tested this on

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Aug 6, 2024
CHANGES/8608.misc.rst Outdated Show resolved Hide resolved
CHANGES/8608.misc.rst Outdated Show resolved Hide resolved
@bdraco bdraco closed this Aug 6, 2024
@bdraco bdraco reopened this Aug 6, 2024
@bdraco bdraco marked this pull request as ready for review August 6, 2024 16:10
aiohttp/helpers.py Outdated Show resolved Hide resolved
Copy link
Member

@Dreamsorcerer Dreamsorcerer left a comment

Choose a reason for hiding this comment

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

Looks good to me. I'm about to update these test files with type checking though, so would be great if you can update after those changes.

Co-authored-by: Sam Bull <git@sambull.org>
@bdraco
Copy link
Member Author

bdraco commented Aug 6, 2024

Thanks!

@bdraco bdraco merged commit c4acabc into master Aug 7, 2024
37 of 38 checks passed
@bdraco bdraco deleted the timer_handle_churn branch August 7, 2024 21:58
Copy link
Contributor

patchback bot commented Aug 7, 2024

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

❌ Failed to cleanly apply c4acabc on top of patchback/backports/3.10/c4acabc836ab969e95199aa976e85c01df720a27/pr-8608

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

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

❌ Failed to cleanly apply c4acabc on top of patchback/backports/3.11/c4acabc836ab969e95199aa976e85c01df720a27/pr-8608

Backporting merged PR #8608 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/c4acabc836ab969e95199aa976e85c01df720a27/pr-8608 upstream/3.11
  4. Now, cherry-pick PR Fix timer handle churn in websocket heartbeat #8608 contents into that branch:
    $ git cherry-pick -x c4acabc836ab969e95199aa976e85c01df720a27
    If it'll yell at you with something like fatal: Commit c4acabc836ab969e95199aa976e85c01df720a27 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x c4acabc836ab969e95199aa976e85c01df720a27
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Fix timer handle churn in websocket heartbeat #8608 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.11/c4acabc836ab969e95199aa976e85c01df720a27/pr-8608
  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 Aug 7, 2024
Co-authored-by: Sam Bull <git@sambull.org>
(cherry picked from commit c4acabc)
bdraco added a commit that referenced this pull request Aug 7, 2024
Co-authored-by: Sam Bull <git@sambull.org>
(cherry picked from commit c4acabc)
bdraco added a commit that referenced this pull request Aug 7, 2024
… heartbeat (#8639)

Co-authored-by: Sam Bull <git@sambull.org>
(cherry picked from commit c4acabc)
bdraco added a commit that referenced this pull request Aug 7, 2024
… heartbeat (#8640)

Co-authored-by: Sam Bull <git@sambull.org>
(cherry picked from commit c4acabc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.10 Trigger automatic backporting to the 3.10 release branch by Patchback robot 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.

2 participants