on_mount runs without composed children if the widget is removed before it starts: intended?

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

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
frontend, testing

Direzione di ricerca

Start by running the supplied reproduction with app.run_test() and trace the lifecycle through Widget.mount and MessagePump._pre_process, the entry points named in the report. Compare the test-harness case with removal under a real App.run(). Done means the behavior is defined and consistent: on_mount should not run with composed children missing, and a regression test covers the removal timing.

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

Descrizione

Hi! I work on an app built on Textual, and our test suite fails now and then on a slow CI runner with NoMatches raised from an on_mount. I narrowed it down to a few lines with no app code of ours. If a widget is removed before it has started, on_mount still runs, but its children were never mounted, so a query_one for a child from its own compose fails and the app exits.

This runs as it is:

import asyncio

from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Label


class Card(Vertical):
    def compose(self) -> ComposeResult:
        yield Label("title", id="title")

    def on_mount(self) -> None:
        print("on_mount: children =", list(self.children))
        self.query_one("#title", Label).update("mounted")


async def main() -> None:
    app = App()
    async with app.run_test() as pilot:
        card = Card()
        app.mount(card)
        card.remove()
        await pilot.pause()


asyncio.run(main())

Output on 8.2.8 and on main at 5e5b7ef:

on_mount: children = []
...
textual.css.query.NoMatches: No nodes match '#title' on Card()

What I think is happening

  • Widget.mount returns early when the widget is _closing or _pruning, so the children from compose are dropped without an error.
  • MessagePump._pre_process then dispatches Mount anyway, and the exception from on_mount goes to App._handle_exception, which exits the app.

Where it does and doesn't happen, in the cases I tried

  • Removing the widget's parent straight after mounting, or calling app.exit() straight after mounting, fail the same way under run_test.
  • Under a real App.run() I couldn't make it crash. Mounting and then removing from a timer, a worker or call_later runs on_mount with _pruning true, but the children are there. So it may only be reachable with the test harness's timing, or on a machine slower than mine.

Is on_mount meant to run in this case? I'd have expected to be able to rely on the children from compose being there in on_mount, so my preference would be for Mount to be skipped once the widget is being removed, but there may be a reason for it that I'm missing. For now we're guarding the lookups in our own widgets, and I wanted to check before relying on that as the long-term answer.

I searched the open and closed issues for NoMatches with on_mount and removal, and didn't find this one.

Textual Diagnostics

Versions

Name Value
Textual 8.2.8
Rich 15.0.0

Python

Name Value
Version 3.14.10
Implementation CPython
Compiler Clang 23.1.3

Operating System

Name Value
System Darwin
Release 24.6.0
Version Darwin Kernel Version 24.6.0: Tue Jul 21 20:51:25 PDT 2026; root:xnu-11417.140.69.711.44~1/RELEASE_ARM64_T8132

Terminal

Name Value
Terminal Application rex (0.0.1793072516-g956d74)
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.