Skip to content

Commit e2dbfba

Browse files
committed
Fix issue pytest-dev#112: 'Dynamically calling a fixture causes a runtime error' by failing over to a ThreadPoolExecutor in cases where the expected event loop is already running. Notably this happens when calling 'request.getfixturevalue(argname)' because it dynamically calls a fixture that needs to be setup on an event loop that's already running pytest async tests
1 parent 813423d commit e2dbfba

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

pytest_asyncio/plugin.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""pytest-asyncio implementation."""
22
import asyncio
3+
import concurrent.futures
34
import contextlib
45
import functools
56
import inspect
@@ -94,7 +95,10 @@ async def async_finalizer():
9495

9596
request.addfinalizer(finalizer)
9697

97-
return loop.run_until_complete(setup())
98+
pool = concurrent.futures.ThreadPoolExecutor(1)
99+
loop = asyncio.new_event_loop()
100+
pool.submit(asyncio.set_event_loop, loop).result()
101+
return pool.submit(loop.run_until_complete, setup()).result()
98102

99103
fixturedef.func = wrapper
100104

0 commit comments

Comments
 (0)