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

Abierto
#6,754 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python
Área
frontend, testing

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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)
Lenguaje dominante
Python
Estrellas
37.7k
Forks
1.5k
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Textualize/textual

Todos los issues de Textualize/textual

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.