-
Notifications
You must be signed in to change notification settings - Fork 159
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
Error of "attached to a different loop" appears in 0.11.0 but not in 0.10.0 #154
Comments
@krizex Can you provide the shortest possible example that reproduces this? |
@simonfagerholm Thanks for you quick response! Updated the testcase in my original post, please check. |
tests/integration/test_aiopg_connector.py::test_execute_query_simultaneous fails with pytest-asyncio 0.11.0. See pytest-dev/pytest-asyncio#154.
We're getting hit by the same issue. We need to stick to 0.10.0 for now. |
tests/integration/test_aiopg_connector.py::test_execute_query_simultaneous fails with pytest-asyncio 0.11.0. See pytest-dev/pytest-asyncio#154.
Got the same issue with 0.11.0... |
I tested a little bit, it seems the By including Also the loop provided by
Would that work as a work around? |
The example testcase is just for reproducing the issue. My real use case is not that simple and use At this moment I will stick to v0.10.0 until the issue been fixed. |
Is it possible to make your fixture dependant on the |
It will be not only about tests to rewrite. Because of that you somehow need to pass |
No, as long as the |
No, the fixture is constructed by calling some low level module and the low level module call BTW, it is a backward compatibility issue so we have to fix it in the library side. |
As you said, why not setup the |
I agree that it should be fixed, I am just suggesting a work around that if it solves the issue can be implemented. Also you are free to create a fix and a PR for this issue, if you think you have a solution. I'm not a maintainer of this project, just your friendly neighborhood sp...programmer, that took some time out of my schedule to help you with a problem |
@simonfagerholm Thanks for your suggestion and I can confirm that adding |
@krizex Great, thanks for the help with confirming! I will see if I can create a fix in the hooks later today |
To apply your suggestion to the example code, just add the
|
As an aside, I think I'm seeing this issue cause an odd problem with aiohttp. This code: import aiohttp
from pytest import mark, fixture
@fixture
async def session():
async with aiohttp.ClientSession() as s:
yield s
@mark.asyncio
async def test_session(session):
async with session.get("http://www.example.org/"):
pass Causes this error
In 0.11 but not in 0.10. Seems to caused by the event loops being different - i.e. I think this is the same issue. |
It is the same issue, the task is not attached to the loop the timeout context manager is run from. |
Just going through these issues real quick. Note that at a certain point, the asyncio ecosystem moved away from passing loops explicitly between components. (Maybe around Python 3.7 when The event loop is basically a global variable. (It's a little more complex than that, but let's go with it.) If you're using the When you have a fixture like
the problem is - it depends on the event loop too, it's just hidden from you since it's basically a global variable, like we said. The order in which pytest initializes unrelated fixtures is, as far as I know, unspecified (so from the perspective of end users, basically random). The correct fix is to let pytest know the I guess the question for us is can we save this boilerplate for our users. |
One problem with depending on the |
I had random hangs upon updating too. I just added |
We should probably document this somewhere. |
@Tinche I think I agree with you, going by ZEN 2: "Explicit is better than implicit" the fixture should explicitly declare that it depends on the What I don't currently understand is why the
But the example has none of these. |
Wow, I just realised: pytest-asyncio appends the
Tested very quickly and it seems to solve it for the example. @Tinche What do you think about changing this? I also think that if you specify it yourself we maybe should change the order so it's still first or give a warning. |
It seems this was introduced in #122, where the
Ironically this could mean that #124 is already solved, while causing problems for the people in this thread.
into:
|
@simonfagerholm Interesting, good research! This looks backwards compatible to me, can you put together a PR? |
@Tinche Sure, I'll do it tonight |
I started doing the changes, but this test throws me for a loop (sorry for the pun ;)):
Of course this fails with the new implementation, as the It was added in #64, but a lot has changed since then so I'll continue digging. Discussion about the problem is continued in PR #156 |
@simonfagerholm I confirm, your changes help in our case 👍 |
pytest-asyncio 0.11.0 uses different loops for fixtures and the actual test and also closes event loop in some cases. Downgrade to 0.10.0 until it's fixed. pytest-dev/pytest-asyncio#154 pytest-dev/pytest-asyncio#157
My testcase get the error of "attached to a different loop" in 0.11.0 but works fine in 0.10.0
The detail is I have the following code in my testcase:
Is there any related change in 0.11.0?
A simple testcase for reproducing:
The text was updated successfully, but these errors were encountered: