Writing to a `Log` before it's mounted crashes the app (`WorkerFailed: NoActiveAppError`)
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
Research direction
Start by tracing Log.write_line/write_lines into _log.py's _update_size worker and compare how RichLog.write handles writes before mounting. Reproduce with the supplied App.run(headless=True) example; run_test() may not reliably expose the race. Done means pre-mount writes no longer crash and a regression test covers the App.run() behavior.
Written by the indexing model from the issue text.
Description
Hi! I hit this while writing generated tests against Textual's widgets.
Writing to a Log in compose(), before yielding it, crashes the app when it's run with App.run():
from textual.app import App, ComposeResult
from textual.widgets import Log
class LogApp(App):
def compose(self) -> ComposeResult:
log = Log()
log.write_line("hello") # written before the Log is yielded
yield log
def on_mount(self) -> None:
self.set_timer(0.05, self.exit)
if __name__ == "__main__":
app = LogApp()
app.run(headless=True)
print("return code:", app.return_code)
Output (the same on 8.2.7 and on main at 5e5b7ef; traceback trimmed to the frames that matter):
LookupError: <ContextVar name='active_app' at 0x...>
During handling of the above exception, another exception occurred:
worker.py:370 in _run self._result = await self.run()
worker.py:326 in _run_threaded return await loop.run_in_executor(None, runner, self._work)
_log.py:141 in _update_size self.app.call_from_thread(self._update_maximum_width, ...)
message_pump.py:264 in app raise NoActiveAppError()
NoActiveAppError
return code: 1
Running that app 500 times in a loop with app.run(headless=True) failed 500 of 500 times, on 8.2.7 and on main. Under run_test() the same app passed 300 of 300 runs, so tests written with the pilot mostly don't show it. With the eager task factory set on the loop (as run() / run_async() do), run_test() failed 1 run in 50, so it's a race, and under run() the thread wins it every time here.
write_lines() starts the _update_size thread worker, which calls self.app from the thread. active_app isn't set in the executor thread, so app falls back to walking _parent, and fails if the Log hasn't been attached yet.
I'd expect a Log to accept writes before it's mounted. RichLog does (50 of 50 runs of the same app with RichLog.write exit cleanly).
Textual 8.2.7 and main (5e5b7ef), Python 3.13, macOS.
- Dominant language
- Python
- Stars
- 37.7k
- Forks
- 1.5k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/textual
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6790 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Textualize/textual#6789 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6787 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6782 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Textualize/textual#6778 · 1 comment ·
All issues in Textualize/textual
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day