-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-33562: Check for asyncio global setting changes in tests #6958
New issue
8000Have 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
bpo-33562: Check for asyncio global setting changes in tests #6958
Conversation
I think at this point I don't know quite how to fix |
@asvetlov Andrew, please take a look if you have time. |
@brettcannon I've looked at the Mac VSTS failures for test_async. FWIW we have seen flaky test on JupyterHub when using mock with async. Usually it is due to an intermittent timing issue or race condition. https://github.com/python/cpython/blob/master/Lib/asyncio/futures.py#L37 I think that On another note, we have found that increasing the async timeout when running tests on the mac also helps. Example test failure:
|
I hope the master is fixed now. |
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 like the general idea, but I have complains about the current implementation.
asyncio.set_event_loop_policy(policy) | ||
|
||
def get_asyncio_get_event_loop(self): | ||
return asyncio.get_event_loop() |
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 function has a side effect: if there is no current event loop, it does create a new one. So I don't think that it's safe to call it. I don't think that we have an API to get the event loop without side effect.
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.
Then basically the whole PR is pointless as every single thing here fetches the event loop somehow to check its state. (Which is fine, but we have to be aware that we the asyncio API is slightly deficient such that we can't do this).
) | ||
|
||
def get_asyncio_get_event_loop_policy(self): | ||
return asyncio.get_event_loop_policy() |
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 function creates a new policy if there is no current policy. The function has a side effect, so I don't think that it's ok to call it from here.
@@ -322,6 +322,7 @@ def test_async_gen_api_01(self): | |||
class AsyncGenAsyncioTest(unittest.TestCase): | |||
|
|||
def setUp(self): | |||
self.addCleanup(asyncio.set_event_loop, asyncio.get_event_loop()) |
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.
Why do we need this?
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.
Because the tests were wiping out the event loop and were triggering the mutated environment check for the event loop.
@@ -11,14 +11,15 @@ def _async_test(func): | |||
"""Decorator to turn an async function into a test case.""" | |||
@functools.wraps(func) | |||
def wrapper(*args, **kwargs): | |||
old_loop = asyncio.get_event_loop() |
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.
Since asyncio.get_event_loop() API is weird, maybe an context manager helper should be added to test.support?
When you're done making the requested changes, leave the comment: |
"I think that future.result is not thread-safe" The whole class is not thread-safe: |
So it sounds like I should close this PR until there's a way to query if a global event loop has been created yet? Does that seem reasonable? |
Actually, I just realized that I'm complicating this. I can simply check if |
Python 3.7 added asyncio.get_running_loop() which doesn't create an event loop if there is no current loop. Maybe if you use this one, it will be fine? |
|
I have another PR that I'm just waiting on the final test run to finish that should take care of everything to everyone's satisfaction. |
I've opened GH-7328 to supercede this PR. |
Tests that need to be updated: