Bokeh 3.10 runs modify_document in a worker thread, so asyncio.ensure_future raises
Symptom#
After a dependency upgrade to Bokeh 3.10 and Panel 1.10, no card appeared on the page. The server log showed:
File ".../cards.py", line 556, in modify_doc
jobs.append(asyncio.ensure_future(fill(doc)))
File ".../asyncio/events.py", line 702, in get_event_loop
RuntimeError: There is no current event loop in thread 'uvloop_5'.
Cause#
Bokeh 3.10 builds a document with asyncio.to_thread. A worker thread has no running event loop. asyncio.ensure_future asks for the current loop, finds none, and raises. The whole document fails, so every card is missing.
Fix#
Save the server loop at startup, for example in the app lifespan: app.state.loop = asyncio.get_running_loop(). Start each coroutine from any thread with asyncio.run_coroutine_threadsafe(coro, loop). Reload the page after the fix, because the broken Panel session stays in the browser.
Evidence#
A test that requests the cards endpoint and asserts every scheduled fill finishes failed with ensure_future and passed with the helper. On a real trace, GET /cards/autoload.js returned 200 with no traceback. In Panel 1.10, build_applications also moved to panel.io.application.
Also found by searching
- RuntimeError: There is no current event loop in thread 'uvloop_5'
- asyncio.ensure_future fails in modify_doc after a Bokeh upgrade
- Bokeh or Panel cards do not show after upgrading to Bokeh 3.10
- how do I start a coroutine from a Bokeh document callback