GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-124309: fix staggered race on eager tasks by graingert · Pull Request #124847 · python/cpython

Conversation

@graingert

@graingert graingert commented Oct 1, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Co-Authored-By: Peter Bierma [email protected]

  • Issue: Happy Eyeballs crashes with Eager Task Factory #124309

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
graingert and others added 2 commits October 1, 2024 14:39
@graingert @ZeroIntensity
restore tests from reverted commits
c03e43f
Co-Authored-By: Peter Bierma <[email protected]>
@graingert
in asyncio.staggered wait for the current task to resume before start…
c936c0f
…ing work on child tasks
@graingert graingert changed the title restore tests from reverted commits gh-124309: fix staggered race on eager tasks Oct 1, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Oct 1, 2024
Happy Eyeballs crashes with Eager Task Factory #124309
Closed
@blurb-it
📜🤖 Added by blurb_it.
2c928f6
@ZeroIntensity ZeroIntensity mentioned this pull request Oct 1, 2024
Using eager task factory with aiohttp 3.10.0 and cpython 3.12+ raise an AssertionError in stdlib asyncio.staggered (fixed in aiohappyeyeballs 2.4.3+) aio-libs/aiohttp#8599
Closed
1 task done
@graingert
graingert marked this pull request as ready for review October 1, 2024 15:36
@graingert
graingert requested review from 1st1, asvetlov, gvanrossum, kumaraditya303 and willingc as code owners October 1, 2024 15:36
@bedevere-app bedevere-app Bot added the awaiting review label Oct 1, 2024
@ZeroIntensity ZeroIntensity added the DO-NOT-MERGE label Oct 1, 2024
@ZeroIntensity

ZeroIntensity commented Oct 1, 2024

Copy link
Copy Markdown
Member

I'm marking this as DO-NOT-MERGE for the time being. aiohttp is planning on trying this sometime this week.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Oct 1, 2024

Copy link
Copy Markdown
Member

aiohttp is planning on trying this sometime this week

Hopefully you will test with a range of aiohttp versions -- the previous version of this PR seemed to work on some but not on others.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Oct 1, 2024

Copy link
Copy Markdown
Member

Yeah, that's the idea. We might not even need this PR if their new implementation turns out to work perfectly.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Oct 1, 2024

Copy link
Copy Markdown
Member

Don't we still need to do something to make eager tasks work? Or does that problem only occur in combination with aiohttp? Presumably there's at least one previous aiohttp version where the current main (or 3.12) branch doesn't work with aiohttp (or I don't understand the issue).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Oct 1, 2024

Copy link
Copy Markdown
Member

Our implementation (on main and 3.12) doesn't work with eager task factories, and most aiohttp versions (apart from the latest, IIUC, because they made their own) rely on our implementation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Oct 2, 2024

Copy link
Copy Markdown
Member

Our implementation (on main and 3.12) doesn't work with eager task factories, and most aiohttp versions (apart from the latest, IIUC, because they made their own) rely on our implementation.

So we definitely have to change something right?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Oct 2, 2024

Copy link
Copy Markdown
Member

Yup, I'm just cautious because I don't want to break prior aiohttp versions on accident.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bdraco
bdraco reviewed Oct 2, 2024
View reviewed changes
Comment thread Lib/asyncio/staggered.py
ok_to_start = locks.Event()
first_task = loop.create_task(run_one_coro(ok_to_start, None))
running_tasks.append(first_task)
ok_to_start.set()

@bdraco bdraco Oct 2, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

For the first one, should the event be preset so it doesn't negate the benefits of an eager task factory?

Or maybe make the event optional for the first one since we want it to start right away?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@graingert graingert Oct 2, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

no it's still needed because all the coro_fn can return immediately without awaiting anything resulting in none of the tasks being appended to running_tasks

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@graingert graingert Oct 2, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

it's not terribly important to maintain the benefits of an eager task factory here as it's supposed to be used for happy_eyeballs over the internet where there is always going to be some delay

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@bdraco bdraco Oct 2, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I was only thinking about the first one since there is a chance the first ip could connect right away (not sure if it can happen synchronously or not)

In aiohttp we don't know if the host is an internet or lan or local host so everything ends up going through this path.

It's also possible the user connecting to localhost has working ipv4 but broken IPv6 and 127.0.0.1 will connect right away and ::1 never will or vise versa

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@graingert graingert Oct 2, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Even connecting to a localhost socket raises BlockingIOError and asyncio needs to wait for it to be writable

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bdraco reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bdraco

bdraco commented Oct 2, 2024

Copy link
Copy Markdown
Contributor

Thanks for the PR. I can test in production later this week

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@graingert

graingert commented Oct 2, 2024

Copy link
Copy Markdown
Contributor Author

we should copy and adapt the aiohttp tests into cpython

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@graingert graingert closed this Oct 2, 2024
@graingert

graingert commented Oct 2, 2024

Copy link
Copy Markdown
Contributor Author

sorry I pressed the wrong button

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@graingert graingert reopened this Oct 2, 2024
@bdraco

bdraco commented Oct 2, 2024

Copy link
Copy Markdown
Contributor

Feel free to copy and adapt any tests in aiohappyeyeballs and consider their relicensed as needed for cpython

ZeroIntensity reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Oct 2, 2024

Copy link
Copy Markdown
Member

I can get to porting aiohttp's pytest stuff over to us sometime later this week.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bdraco

bdraco commented Oct 4, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The new implementation we have in aiohappyeyeballs seems to be going well. No new issues for aiohappyeyeballs filed, Home Assistant release went well, and my testing went well. I think I’m ready to start testing this PR today

I’ll start by running aiohappyeyeballs test suite against it and if it passes try to write a meaningful benchmark to compare the implementations

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Oct 8, 2024

Copy link
Copy Markdown
Member

Ok, good to know.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@1st1

1st1 commented Oct 9, 2024

Copy link
Copy Markdown
Member

I didn't test it in production yet because of the comments I posted above: It looks like there is a race where tasks can be garbage collected before they return a result:

FWIW it's a different systemic issue that we need to fix asap (and backport to 3.13)

#121264 (review)

IMO this PR shouldn't solve that specific problem and focus on a smaller change.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bdraco

bdraco commented Oct 9, 2024

Copy link
Copy Markdown
Contributor
@@ -139,3 +139,9 @@ async def staggered_race(coro_fns, delay, *, loop=None):
         # Make sure no tasks are left running if we leave this function
         for t in running_tasks:
             t.cancel()
+            try:
+                await t
+            except (SystemExit, KeyboardInterrupt):
+                raise
+            except BaseException:
+                pass

That fixes the warning but still the aiohappyeyeballs cancellation swallow check test is failing

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@graingert

graingert commented Oct 9, 2024

Copy link
Copy Markdown
Contributor Author
@@ -139,3 +139,9 @@ async def staggered_race(coro_fns, delay, *, loop=None):
         # Make sure no tasks are left running if we leave this function
         for t in running_tasks:
             t.cancel()
+            try:
+                await t
+            except (SystemExit, KeyboardInterrupt):
+                raise
+            except BaseException:
+                pass

That fixes the warning but still the aiohappyeyeballs cancellation swallow check test is failing

Yes awaiting a task and suppressing cancellation will do that. You need to use a loop and a future. But we can fix that in another PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Oct 9, 2024

Copy link
Copy Markdown
Member

If aiohttp's implementation has been tested in production (and has all their tests passing), why not just copy theirs over?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@1st1

1st1 commented Oct 9, 2024

Copy link
Copy Markdown
Member

@ZeroIntensity

If aiohttp's implementation has been tested in production (and has all their tests passing), why not just copy theirs over?

Can you give a link to the final diff of their fix? I can't find it in the few links to different issues I clicked through this discussion.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Oct 9, 2024

Copy link
Copy Markdown
Member

Here

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@1st1

1st1 commented Oct 9, 2024

Copy link
Copy Markdown
Member

Hm, tbqh I think the approach in this PR is more straightforward. Unless you have a specific concern I'd go with it.

@graingert feel free to go ahead and merge.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@1st1

1st1 commented Oct 9, 2024

Copy link
Copy Markdown
Member

We do need to backport this though.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@graingert graingert removed the DO-NOT-MERGE label Oct 10, 2024
@graingert

graingert commented Oct 10, 2024

Copy link
Copy Markdown
Contributor Author

Hm, tbqh I think the approach in this PR is more straightforward. Unless you have a specific concern I'd go with it.

@graingert feel free to go ahead and merge.

I can't merge, I'll need a core dev to do it for me

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Oct 10, 2024

Copy link
Copy Markdown
Member

Hm, tbqh I think the approach in this PR is more straightforward. Unless you have a specific concern I'd go with it.

I'm worried about the failing tests on aiohttp's end, but I think Thomas said that he'll address that in another PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity approved these changes Oct 10, 2024
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'll go ahead and approve this to ease stomachs about merging :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ZeroIntensity ZeroIntensity added needs backport to 3.12 only security fixes needs backport to 3.13 only security fixes labels Oct 10, 2024
@1st1

1st1 commented Oct 11, 2024

Copy link
Copy Markdown
Member

I can't merge, I'll need a core dev to do it for me

Apologies, I'll go ahead and click the button then.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@1st1
1st1 merged commit 979c0df into python:main Oct 11, 2024
@miss-islington-app

miss-islington-app Bot commented Oct 11, 2024

Copy link
Copy Markdown

Thanks @graingert for the PR, and @1st1 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 11, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 11, 2024
@graingert @ZeroIntensity @miss-islington
pythongh-124309: fix staggered race on eager tasks (pythonGH-124847)
471cbe1
This patch is entirely by Thomas and Peter

(cherry picked from commit 979c0df)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 11, 2024
@graingert @ZeroIntensity @miss-islington
pythongh-124309: fix staggered race on eager tasks (pythonGH-124847)
626ea11
This patch is entirely by Thomas and Peter

(cherry picked from commit 979c0df)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
@bedevere-app

bedevere-app Bot commented Oct 11, 2024

Copy link
Copy Markdown

GH-125339 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Oct 11, 2024
@bedevere-app

bedevere-app Bot commented Oct 11, 2024

Copy link
Copy Markdown

GH-125340 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Oct 11, 2024
1st1 pushed a commit that referenced this pull request Oct 12, 2024
@miss-islington @graingert @ZeroIntensity
[3.13] gh-124309: fix staggered race on eager tasks (GH-124847) (#125339
b4c504d
)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
1st1 pushed a commit that referenced this pull request Oct 12, 2024
@miss-islington @graingert @ZeroIntensity
[3.12] gh-124309: fix staggered race on eager tasks (GH-124847) (#125340
affffc7
)

gh-124309: fix staggered race on eager tasks (GH-124847)

This patch is entirely by Thomas and Peter

(cherry picked from commit 979c0df)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
@twisteroidambassador twisteroidambassador mentioned this pull request Nov 15, 2024
Probably breaks with eager task factory twisteroidambassador/async_stagger#11
Closed
bdraco added a commit to aio-libs/aiohappyeyeballs that referenced this pull request Mar 3, 2025
@bdraco
fix: update staggered race implementation to use latest cpython version
d248314
related aiohttp issue aio-libs/aiohttp#10506

In #101 we replaced the staggered race implementation since the cpython version had races
that were not fixed at the time. cpython has since updated the implementation to fix
additional races. Our current implementation still has problems with cancellation
and cpython has fixed that in python/cpython#128475
and python/cpython#124847

This PR ports the latest cpython implementation
@bdraco bdraco mentioned this pull request Mar 3, 2025
fix: update staggered race implementation to use latest cpython version aio-libs/aiohappyeyeballs#142
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@1st1 1st1 1st1 approved these changes
@ZeroIntensity ZeroIntensity ZeroIntensity approved these changes
@asvetlov asvetlov Awaiting requested review from asvetlov asvetlov is a code owner
@gvanrossum gvanrossum Awaiting requested review from gvanrossum
@kumaraditya303 kumaraditya303 Awaiting requested review from kumaraditya303 kumaraditya303 is a code owner
@willingc willingc Awaiting requested review from willingc willingc is a code owner
+1 more reviewer
@bdraco bdraco bdraco left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@graingert @ZeroIntensity @gvanrossum @bdraco @1st1
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.