--- /dev/null
+.. change::
+ :tags: bug, asyncio, regression
+ :tickets: 6409
+
+ Fixed a regression introduced by :ticket:`6337` that would create an
+ ``asyncio.Lock`` which could be attached to the wrong loop when
+ instantiating the async engine before any asyncio loop was started, leading
+ to an asyncio error message when attempting to use the engine under certain
+ circumstances.
import greenlet
from . import compat
+from .langhelpers import memoized_property
from .. import exc
class AsyncAdaptedLock:
- def __init__(self):
- self.mutex = asyncio.Lock()
+ @memoized_property
+ def mutex(self):
+ # there should not be a race here for coroutines creating the
+ # new lock as we are not using await, so therefore no concurrency
+ return asyncio.Lock()
def __enter__(self):
+ # await is used to acquire the lock only after the first calling
+ # coroutine has created the mutex.
await_fallback(self.mutex.acquire())
return self
is_false(async_engine == None)
+ @async_test
+ async def test_no_attach_to_event_loop(self, testing_engine):
+ """test #6409"""
+
+ import asyncio
+ import threading
+
+ errs = []
+
+ def go():
+ loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(loop)
+
+ engine = testing_engine(asyncio=True, transfer_staticpool=True)
+
+ async def main():
+ tasks = [task() for _ in range(2)]
+
+ await asyncio.gather(*tasks)
+
+ async def task():
+ async with engine.begin() as connection:
+ result = await connection.execute(select(1))
+ result.all()
+
+ try:
+ asyncio.run(main())
+ except Exception as err:
+ errs.append(err)
+
+ t = threading.Thread(target=go)
+ t.start()
+ t.join()
+
+ if errs:
+ raise errs[0]
+
@async_test
async def test_connection_info(self, async_engine):