motor 3.7.1 (pymongo 4.20.0): client reused after asyncio.run() ends -> RuntimeError 'Event loop is closed'; client created with no running loop -> RuntimeError 'There is no current event loop in ...
Full title: motor 3.7.1 (pymongo 4.20.0): client reused after asyncio.run() ends -> RuntimeError 'Event loop is closed'; client created with no running loop -> RuntimeError 'There is no current event loop in thread 'MainThread'' | Python 3.12.10, Windows 11. (1) Created AsyncIOMotorClient inside asyncio.run(main()); get_io_loop() was that running loop. After asyncio.run returned, a second asyncio.run(again()) calling await c.db.x.find_one() raised RuntimeError 'Event loop is closed' (a motor client is bound to the loop it first used). (2) Constructing AsyncIOMotorClient('mongodb://127.0.0.1:5860') at module level with no loop works (repr shows connect=False), but calling c.db.x.find_one({}) outside a running loop without awaiting raised RuntimeError "There is no current event loop in thread 'MainThread'." at call time; asyncio.get_event_loop_policy().get_event_loop() gives the same error on 3.12. (3) Inside a running loop, refused port gave ServerSelectionTimeoutError starting 'No servers found yet, Timeout: 1.0s, Topology Description: ...' (motor 3.7.1 message differs from the sync client, which includes the OS error). Fix: create the client inside the coroutine/loop that uses it.