-
Notifications
You must be signed in to change notification settings - Fork 137
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
contextvars setup when mixing sync and async pytest fixtures in async test #614
Comments
There are two ways you can fix this:
Let me know if that works for you, and then we can think about how to improve the documentation. |
Full disclosure: I work in the same team as @heqile, on the affected projects Both of the proposed solutions are annoying for us, because the synchronous fixtures that we use to set the context variables are located in a pytest plugin that we wrote, and these fixtures are used in both sync and async projects:
Unless you tell me that you have already ruled out any fix for this issue in anyio, I will take some time in the next few days to investigate the possibility of a fix in anyio. |
I haven't touched the pytest plugin in months, but if you have a fix that doesn't break anything, I'm willing to consider it. |
I refreshed my memory a bit and it goes something like this: the pytest plugin (actually the backend's runner implementation) creates a runner task which receives requests through a memory object stream. It stays in the background for as long as there are any async fixtures/tests active. But because it works in a long-running task, synchronous fixtures changing context variables won't affect the task. If you have an alternative solution which also lets one use session/package/module scoped async fixtures, then sure, I'm interested in hearing that proposal. |
Thank you for the explanations, they were very useful. I have looked into how the plugin works, and my big idea for a fix would be to run all the synchronous fixtures and tests in the background task as soon as the anyio test runner is in use. If I were to suggest that behavior change in a PR, would you like me to put that behind a pytest configuration switch? Also, I would like to ask: is it supported to have a synchronous test that requires an asynchronous fixture? |
I'm cautiously optimistic about this plan. Layering the scopes in their specific tasks sounds like a good idea. I don't know how it will translate to code, but I'm looking forward to seeing it. |
I am going to put back the comment that I previously deleted, because you are replying to it here (I was in the process of opening a new issue for the contextvar isolation, but I guess I have your approval to look into how to do it). Thank you!
|
@agronholm: Could you just tell me, is it supported to have a test defined by a synchronous function that depends on a fixture implemented in an async function? (I have not been able to make that work, but maybe I am missing something) |
I don't think that has ever worked under any async test runner, including AnyIO's. I also don't know how that could possibly work without involving multithreading (which would introduce its own set of issues). |
Just to clarify, what I meant was that async fixtures that need to be actively running would be the biggest problem. But say you have an async fixture that just uses an async API to get some data, that could conceivably work. |
Btw @jhominal, I pinged you earlier on the |
I have been quite disconnected from gitter and a lot of other tools for a while, we can discuss it on gitter or I can send you my email address so that we can discuss that over there. |
I just sent you a PM on Gitter. |
Things to check first
I have searched the existing issues and didn't find my bug already reported there
I have checked that my bug is still present in the latest release
AnyIO version
4.0.0
Python version
3.8
What happened?
In the tests, we need to set the
ContextVar
to some special values by using pytest fixtures. After updating tov4.0.0
, some of our tests broke, as if thesync
fixture who setupContextVar
does not work.I do notice the changes in
v4.0.0
:In fact, when setting a context variable in a synchronous fixture, the result of setting that context variable is not visible in an async test when using anyio's test runner if an async fixture has been run before the synchronous fixture. (see the code below to reproduce).
But the case that a
ContextVar
setup insync
fixture and runs after certainasync
fixture should also be a common use case, (especially when you want to re-use the setup fixutre in othernon-async
project). And it does work inv3.7.1
, so I'd like to consider it as a bug.(correct me, if I'm wrong :) )
How can we reproduce the bug?
Thanks @jhominal for reform the tests
The text was updated successfully, but these errors were encountered: