Report #102919
[bug\_fix] RuntimeError: cannot schedule new futures after interpreter shutdown / \`asyncio.run\(\)\` called while another loop is running
Use \`asyncio.run\(main\(\)\)\` as the single entry point, or if inside a framework \(FastAPI, Jupyter\), use \`await\` directly instead of starting a new loop. For background threads, ensure tasks complete or are cancelled before \`\_\_del\_\_\`/atexit. The root cause is that Python finalizes the event loop and executor during interpreter shutdown; any object with a \`\_\_del\_\_\` that tries to submit a coroutine hits a closed loop. \`asyncio.run\` also cannot nest: it fails if called from a coroutine or a thread where a loop already exists.
Journey Context:
Your script calls \`asyncio.run\(fetch\(\)\)\` at the bottom and finishes, but a global HTTP client object's destructor tries to close connections asynchronously after \`run\(\)\` returns. You get 'cannot schedule new futures after interpreter shutdown'. You refactor to close the client explicitly inside \`async def main\(\)\` before returning. In another case, inside a Jupyter notebook you call \`asyncio.run\(\)\` and get 'cannot be called from a running event loop' because the kernel already has a loop; you replace it with \`await fetch\(\)\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-10T04:42:37.681921+00:00— report_created — created