Skip to content

fix: skip nest_asyncio on Python 3.12+ to prevent uvicorn loop_factory error#635

Closed
Kubudak90 wants to merge 2 commits into
coinbase:mainfrom
Kubudak90:fix-nest-asyncio-py312
Closed

fix: skip nest_asyncio on Python 3.12+ to prevent uvicorn loop_factory error#635
Kubudak90 wants to merge 2 commits into
coinbase:mainfrom
Kubudak90:fix-nest-asyncio-py312

Conversation

@Kubudak90

Copy link
Copy Markdown

Problem

nest_asyncio.apply() patches asyncio.run() with a signature that doesn't support the loop_factory parameter added in Python 3.12. This causes uvicorn (and other modern async frameworks) to fail with:

TypeError: _patch_asyncio.<locals>.run() got an unexpected keyword argument 'loop_factory'

This was reported in #591.

Solution

Only apply nest_asyncio on Python < 3.12 where the patch is safe. On Python 3.12+, the standard asyncio behavior is preserved, allowing uvicorn and other frameworks to use the new loop_factory parameter.

Changes

  • Added import sys at the top of the file
  • Moved import nest_asyncio inside the version check block
  • Wrapped the nest_asyncio.apply() call in a sys.version_info < (3, 12) condition

Testing

This fix ensures:

  1. Python 3.10 and 3.11 continue to work with nested event loops via nest_asyncio
  2. Python 3.12+ works with modern async frameworks like uvicorn that use loop_factory
  3. The existing error handling for uvloop incompatibility is preserved

Fixes #591

…y error

nest_asyncio.apply() patches asyncio.run() with a signature that doesn't
support the loop_factory parameter added in Python 3.12. This causes
uvicorn to fail with:
  TypeError: _patch_asyncio.<locals>.run() got an unexpected keyword argument 'loop_factory'

Fix: Only apply nest_asyncio on Python < 3.12 where the patch is safe.
On Python 3.12+, the standard asyncio behavior is preserved.

Fixes coinbase#591
@cb-heimdall

cb-heimdall commented Apr 4, 2026

Copy link
Copy Markdown

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 1
Sum 2

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Kubudak90

Copy link
Copy Markdown
Author

Closing in favor of #637 which has a more comprehensive solution

@Kubudak90 Kubudak90 closed this Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Python: Move away from nest_async (is not compatible with modern Python) and is unmaintained.

2 participants