Skip to content

nest_asyncio.apply() breaks Python 3.14: asyncio task corruption → HTTP 500 on every static-asset request #2952

Description

@pidefrem

Bug

chainlit run fails on Python 3.14. The browser shows a white page because
every static-asset request (/assets/*.js, /public/*) returns HTTP 500:

anyio.NoEventLoopError: Not currently running on any asynchronous event loop.
Available async backends: asyncio, trio

Root cause

chainlit/cli/__init__.py calls nest_asyncio.apply() at module import time.
nest_asyncio ≤ 1.6.0 patches loop.run_until_complete() via:

asyncio.ensure_future(future, loop=self)   # nest_asyncio 1.6.0, line 88

The loop= keyword was deprecated in Python 3.8 and removed in Python 3.14
(bpo-39529). On Python 3.14 this silently
corrupts asyncio task registration: asyncio.current_task() returns None
inside running coroutines.

The failure chain:

  1. chainlit runimport chainlit.clinest_asyncio.apply() at module level
  2. nest_asyncio patches the loop using the removed loop= kwarg → tasks not registered
  3. Browser requests /assets/index-*.js
  4. FileResponse.__call__anyio.to_thread.run_sync(os.stat, path)
  5. anyiosniffio.current_async_library()asyncio.current_task()None
  6. anyio.NoEventLoopError → HTTP 500 → no JS → white page

Note: nest_asyncio has not been updated since 2023 and is effectively
unmaintained.

Fix

Remove nest_asyncio entirely. asyncio.run(start()) at the bottom of
run_chainlit() is a top-level entry point — it does not run inside any other
event loop and has never needed re-entrant loop support. The existing comment
in the source ("Not sure if it is necessary...") confirms this was always
uncertain.

A PR with the fix, updated pyproject.toml, regenerated lockfile, and a
regression test is ready: #2953

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendPertains to the Python backend.bugSomething isn't workingneeds-triage

    Type

    No fields configured for Bug.

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions