Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- Fixed `loading` reactive not being thread-safe when set from a worker thread https://github.com/Textualize/textual/issues/5108

## [8.2.8] - 2026-06-30

### Fixed
Expand Down
9 changes: 8 additions & 1 deletion src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from __future__ import annotations

import threading

from asyncio import create_task, gather, wait
from collections import Counter
from contextlib import asynccontextmanager
Expand Down Expand Up @@ -1074,7 +1076,12 @@ def _watch_loading(self, loading: bool) -> None:
if not self.is_mounted:
self.call_later(self.set_loading, loading)
else:
self.set_loading(loading)
if threading.get_ident() == self.app._thread_id:
self.set_loading(loading)
else:
# Setting `loading` from a worker thread: hop over to the
# app's thread instead of touching the UI directly.
self.app.call_from_thread(self.set_loading, loading)

ExpectType = TypeVar("ExpectType", bound="Widget")

Expand Down
22 changes: 22 additions & 0 deletions tests/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,25 @@ def compose(self) -> ComposeResult:
app = LoadingApp()
async with app.run_test() as pilot:
await pilot.pause()


async def test_loading_set_from_worker_thread():
"""Regression test for #5108: setting `loading` from a background
thread should not touch the DOM directly, and should not raise."""

class LoadingApp(App[None]):
def compose(self) -> ComposeResult:
yield Label("hello")

app = LoadingApp()
async with app.run_test() as pilot:
label = app.query_one(Label)

def set_loading_from_thread() -> None:
label.loading = True

app.run_worker(set_loading_from_thread, thread=True)
await pilot.pause()
await pilot.pause()

assert label.loading is True