Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions backend/chainlit/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import sys

import click
import nest_asyncio
import uvicorn

# Not sure if it is necessary to call nest_asyncio.apply() before the other imports
nest_asyncio.apply()

# ruff: noqa: E402
# nest_asyncio was intentionally removed (see https://github.com/Chainlit/chainlit/issues/2767).
# nest_asyncio ≤ 1.6.0 patches loop.run_until_complete() via
# asyncio.ensure_future(future, loop=self), where the ``loop=`` keyword was
# deprecated in Python 3.8 and **removed in Python 3.14** (bpo-39529).
# Applying it at import time silently corrupted asyncio task registration:
# asyncio.current_task() returned None inside running coroutines, which
# caused anyio.NoEventLoopError on every static-asset request (HTTP 500 /
# white page) on Python 3.14. The entry point asyncio.run(start()) is a
# top-level call and has never needed re-entrant loop support.
from chainlit.auth import ensure_jwt_secret
from chainlit.cache import init_lc_cache
from chainlit.config import (
Expand Down
1 change: 0 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ dependencies = [
"syncer>=2.0.3,<3.0.0",
"asyncer>=0.0.8,<0.1.0",
"mcp>=1.11.0,<2.0.0",
"nest-asyncio>=1.6.0,<2.0.0",
"click>=8.1.3,<9.0.0",
"tomli>=2.0.1,<3.0.0",
"pydantic>=2.7.2,<3",
Expand Down
33 changes: 33 additions & 0 deletions backend/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Regression tests for chainlit.cli import behaviour.

Ensures nest_asyncio is not imported by chainlit.cli.

Background
----------
nest_asyncio ≤ 1.6.0 patches ``asyncio.BaseEventLoop.run_until_complete`` via
``asyncio.ensure_future(future, loop=self)``. The ``loop=`` keyword was
deprecated in Python 3.8 and **removed in Python 3.14** (bpo-39529). When
``nest_asyncio.apply()`` ran at module-import time it silently corrupted asyncio
task registration: ``asyncio.current_task()`` returned ``None`` inside running
coroutines, causing ``anyio.NoEventLoopError`` on every static-asset request
and a white screen for users on Python 3.14.

See https://github.com/Chainlit/chainlit/issues/2767
"""

import chainlit.cli


def test_nest_asyncio_not_in_cli_namespace():
"""chainlit.cli must not expose nest_asyncio in its module namespace.

If ``import nest_asyncio`` is ever re-added to cli/__init__.py this test
will fail immediately, preventing the Python 3.14 regression from
being reintroduced.
"""
assert not hasattr(chainlit.cli, "nest_asyncio"), (
"chainlit.cli exposes 'nest_asyncio' in its namespace. "
"Remove 'import nest_asyncio' and 'nest_asyncio.apply()' from "
"backend/chainlit/cli/__init__.py — nest_asyncio breaks Python 3.14 "
"via the removed loop= kwarg in asyncio.ensure_future (bpo-39529)."
)
14 changes: 4 additions & 10 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.