{"record":{"id":"88aa6ee5ec3930dd","repo":"Textualize/textual","slug":"there-is-no-active-worker-in-this-task-or-thread","errorCode":null,"errorMessage":"There is no active worker in this task or thread.","messagePattern":"There is no active worker in this task or thread\\.","errorType":"exception","errorClass":"NoActiveWorker","httpStatus":null,"severity":"error","filePath":"src/textual/worker.py","lineNumber":77,"sourceCode":"\n\nclass WorkerCancelled(WorkerError):\n    \"\"\"The worker was cancelled and did not complete.\"\"\"\n\n\ndef get_current_worker() -> Worker:\n    \"\"\"Get the currently active worker.\n\n    Raises:\n        NoActiveWorker: If there is no active worker.\n\n    Returns:\n        A Worker instance.\n    \"\"\"\n    try:\n        return active_worker.get()\n    except LookupError:\n        raise NoActiveWorker(\n            \"There is no active worker in this task or thread.\"\n        ) from None\n\n\nclass WorkerState(enum.Enum):\n    \"\"\"A description of the worker's current state.\"\"\"\n\n    PENDING = 1\n    \"\"\"Worker is initialized, but not running.\"\"\"\n    RUNNING = 2\n    \"\"\"Worker is running.\"\"\"\n    CANCELLED = 3\n    \"\"\"Worker is not running, and was cancelled.\"\"\"\n    ERROR = 4\n    \"\"\"Worker is not running, and exited with an error.\"\"\"\n    SUCCESS = 5\n    \"\"\"Worker is not running, and completed successfully.\"\"\"\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/worker.py#L59-L95","documentation":"NoActiveWorker raised by get_current_worker(): there is no worker associated with the current asyncio task or thread. Worker context is tracked via a contextvar/Local set only inside running worker callables, so calling this outside a worker has nothing to find.","triggerScenarios":"Calling `get_current_worker()` from a regular async method, a button handler, `on_mount`, or a plain thread that was not started via run_worker/run_thread with the worker machinery.","commonSituations":"Shared helper functions used both inside worker functions and in normal app code; calling worker APIs like `worker.check_cancelled()` or progress updates outside a worker.","solutions":["Only call get_current_worker() inside code run via `self.run_worker(...)` / `@work` decorated methods.","Guard the call: try/except NoActiveWorker and fall back to non-worker behavior.","Pass the Worker instance explicitly to helpers instead of relying on ambient context."],"exampleFix":"# before\nworker = get_current_worker()\n# after\nfrom textual.worker import NoActiveWorker\ntry:\n    worker = get_current_worker()\nexcept NoActiveWorker:\n    worker = None","handlingStrategy":"try-catch","validationCode":"from textual.worker import NoActiveWorker\ntry:\n    worker = get_current_worker()\nexcept NoActiveWorker:\n    worker = None","typeGuard":null,"tryCatchPattern":"from textual.worker import NoActiveWorker\ntry:\n    get_current_worker().check_cancelled()\nexcept NoActiveWorker:\n    pass  # not running as a worker","preventionTips":["Restrict get_current_worker() calls to code run via run_worker/@work","Pass Worker instances explicitly to shared helpers"],"tags":["worker","asyncio","context","textual"],"backgroundTag":"no-active-worker-context","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}