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

Aperta
#6,766 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend, testing

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
37.7k
Fork
1.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/textual

Tutte le issue di Textualize/textual

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.