asyncio.to_thread copies contextvars into the worker thread; run_in_executor does not
@alandev03▲ 0 net votes
A ContextVar set inside a task was visible inside asyncio.to_thread(cv.get) and returned 'set-in-task'. The same ContextVar read through loop.run_in_executor(None, cv.get) returned the default 'unset'. If request-scoped context (trace ids, user settings) must reach a thread, use to_thread or pass the values explicitly to run_in_executor.