Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
bpo-42130: Fix for explicit suppressing of cancellations in wait_for()#28149
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
9c2942a7ff2c04cdaa9c8342222f01d9884af96f101e5233d743177ed7c096580f027b4eb5de2a8f5c1f5de6b795a79cf211e12143a35126df15ac17374366c694173cb59377File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1009,16 +1009,63 @@ def gen(): | ||
| self.assertEqual(res, "ok") | ||
| def test_wait_for_cancellation_race_condition(self): | ||
| def gen(): | ||
| yield 0.1 | ||
| yield 0.1 | ||
| yield 0.1 | ||
| yield 0.1 | ||
| loop = self.new_test_loop(gen) | ||
| fut = self.new_future(loop) | ||
| # Test that if task is cancelled at the same time the future | ||
| # completes, the result is still returned. | ||
| loop.call_later(0.1, fut.set_result, "ok") | ||
| task = loop.create_task(asyncio.wait_for(fut, timeout=1)) | ||
| loop.call_later(0.1, task.cancel) | ||
| res = loop.run_until_complete(task) | ||
| self.assertEqual(res, "ok") | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting this test back in, with a comment. It does manage to trigger the race condition aaliddel mentioned. | ||
| def test_wait_for_suppress_cancellation(self): | ||
| def gen(): | ||
| yield | ||
| yield 0.1 | ||
| yield 0.1 | ||
| yield 0.1 | ||
| async def inner(): | ||
| with contextlib.suppress(asyncio.CancelledError): | ||
| try: | ||
| await asyncio.sleep(1) | ||
| return 1 | ||
| except asyncio.CancelledError: | ||
| return "ok" | ||
| async def main(): | ||
| result = await asyncio.wait_for(inner(), timeout=.01) | ||
| assert result == 1 | ||
| loop = self.new_test_loop(gen) | ||
| fut = self.new_future(loop) | ||
| task = loop.create_task(asyncio.wait_for(inner(), timeout=1)) | ||
| loop.call_later(0.1, task.cancel) | ||
| # Cancellation is suppressed in inner(), so should still return here. | ||
| res = loop.run_until_complete(task) | ||
| self.assertEqual(res, "ok") | ||
| def test_wait_for_cancellation_inner_race_condition(self): | ||
| def gen(): | ||
| yield | ||
| yield 0.1 | ||
| yield 0 | ||
| yield 0 | ||
| async def inner(): | ||
| # Test that if fut completes at same time as timeout, the result | ||
| # is still returned. | ||
| return await asyncio.wait_for(fut, timeout=1) | ||
| asyncio.run(main()) | ||
| loop = self.new_test_loop(gen) | ||
| fut = self.new_future(loop) | ||
| loop.call_later(0.1, fut.set_result, "ok") | ||
| res = loop.run_until_complete(asyncio.wait_for(inner(), timeout=0.1)) | ||
| self.assertEqual(res, "ok") | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Converted my last test to use | ||
| def test_wait_for_waits_for_task_cancellation(self): | ||
| loop = asyncio.new_event_loop() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix issue where ``asyncio.wait_for()`` is cancelled when the coro | ||
| actually suppresses the cancellation. |
Uh oh!
There was an error while loading. Please reload this page.
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.
This ensures that a cancellation suppressed in the inner future, also suppresses it in
wait_for()as one would expect (see test_wait_for_suppress_cancellation).