Writing to a `Log` before it's mounted crashes the app (`WorkerFailed: NoActiveAppError`)

Open
#6,766 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend, testing

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.