{"record":{"id":"a3ab9a87f22e1133","repo":"pola-rs/polars","slug":"asyncio-is-already-patched-by-nest-asyncio","errorCode":null,"errorMessage":"asyncio is already patched by nest_asyncio","messagePattern":"asyncio is already patched by nest_asyncio","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/nest_asyncio.py","lineNumber":204,"sourceCode":"            if new_event_loop:\n                # Avoid ResourceWarning: unclosed event loop\n                loop.close()\n\n\ndef _patch_asyncio(*, error_on_mispatched: bool = False):\n    \"\"\"Patch asyncio module to use pure Python tasks and futures.\"\"\"\n\n    def _get_event_loop(stacklevel=3):\n        loop = events._get_running_loop()\n        if loop is None:\n            loop = events.get_event_loop_policy().get_event_loop()\n        return loop\n\n    # Use module level _current_tasks, all_tasks and patch run method.\n    if hasattr(asyncio, \"_nest_patched\"):\n        if not hasattr(asyncio, \"_nest_asyncio2\"):\n            if error_on_mispatched:\n                raise RuntimeError(\"asyncio is already patched by nest_asyncio\")\n            elif sys.version_info >= (3, 12, 0):\n                import warnings\n\n                warnings.warn(\n                    \"asyncio is already patched by nest_asyncio. You may encounter bugs related to asyncio\"\n                )\n        return\n\n    # Using _PyTask on Python 3.14+ will break current_task() (and all_tasks(),\n    # _swap_current_task())\n    # Even we replace it with _py_current_task(), it only works with _PyTask, but\n    # the external loop is probably using _CTask.\n    # https://github.com/python/cpython/pull/129899\n    if sys.version_info >= (3, 6, 0) and sys.version_info < (3, 14, 0):\n        asyncio.Task = asyncio.tasks._CTask = asyncio.tasks.Task = asyncio.tasks._PyTask\n        asyncio.Future = asyncio.futures._CFuture = asyncio.futures.Future = (\n            asyncio.futures._PyFuture\n        )","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/py-polars/src/polars/_utils/nest_asyncio.py#L186-L222","documentation":"RuntimeError raised by _patch_asyncio: asyncio already carries the _nest_patched marker from a previous (or external) patch, and error_on_mispatched requested strict behavior, so re-patching is refused rather than silently double-patching task/future classes.","triggerScenarios":"Applying nest_asyncio to an already patched event loop.","commonSituations":"See trigger scenarios.","solutions":["Do not apply nest_asyncio twice; apply it once at startup."],"exampleFix":"nest_asyncio.apply()  # once only","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}