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

implement wait_for using asyncio.timeout() on 3.11 #7571

Merged
merged 7 commits into from
Mar 8, 2023

Conversation

graingert
Copy link
Member

Closes #7570

  • Tests added / passed
  • Passes pre-commit run --all-files

@github-actions
Copy link
Contributor

github-actions bot commented Feb 22, 2023

Unit Test Results

See test report for an extended history of previous test failures. This is useful for diagnosing flaky tests.

       24 files  ±0         24 suites  ±0   10h 29m 33s ⏱️ - 5m 3s
  3 349 tests ±0    3 250 ✔️ +1       99 💤 +1  0  - 2 
39 482 runs  ±0  37 667 ✔️  - 1  1 815 💤 +3  0  - 2 

Results for commit 85dcc7b. ± Comparison against base commit 790d852.

♻️ This comment has been updated with latest results.

@graingert graingert force-pushed the asyncio-wait-for-using-asyncio-timeout branch 3 times, most recently from ce86475 to 31f105c Compare February 23, 2023 14:46
using asyncio.wait_for in utils_test.gen_test.<locals>.async_fn_outer
spawns a new child task which has the extra effect of waiting an
`await asyncio.sleep(0)` after every test before running
utils_test.check_instances. When using `async with asyncio.timeout(t):`
only the `asyncio.current_task()` is used and so this extra sleep is removed.
This extra sleep is enough for the `CommClosedError` to be raised in the
`test_listen_connect(_ws).<locals>.handle_comm` task.

I proved this by checking that both test_listen_connect and
test_listen_connect_wss both pass after restoring `asyncio.wait_for` in
`only utils_test.gen_test.<locals>.async_fn_outer` or adding
`asyncio.sleep(0)` to the end of the tests.
@graingert graingert force-pushed the asyncio-wait-for-using-asyncio-timeout branch from 31f105c to 85dcc7b Compare February 23, 2023 15:04
@graingert graingert marked this pull request as ready for review February 23, 2023 16:34
@graingert graingert requested a review from fjetter February 23, 2023 17:03
Comment on lines +1803 to +1812
if sys.version_info >= (3, 11):

async def wait_for(fut: Awaitable[T], timeout: float) -> T:
async with asyncio.timeout(timeout):
return await fut

else:

async def wait_for(fut: Awaitable[T], timeout: float) -> T:
return await asyncio.wait_for(fut, timeout)
Copy link
Member

Choose a reason for hiding this comment

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

Not a big deal but we also have a compatibility module with things like this

Copy link
Member

@fjetter fjetter left a comment

Choose a reason for hiding this comment

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

LGTM. Let me know if you want to move it over to compatibility or not. I don't have a strong preference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Safe asyncio timeout for python 3.11 and above
2 participants