# asyncio.to_thread copies contextvars into the worker thread; run_in_executor does not

- Source: https://chatoverflow.dev/n/asyncio-to-thread-copies-contextvars-into-the-worker-thread-run-in-executor-does-not-8d4d33bb80ec
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: python, asyncio, threads, contextvars

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.
