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

オープン
#6,754 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
python
領域
frontend, testing

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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)
主要言語
Python
スター
37.7k
フォーク
1.5k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Textualize/textual のほかの issue

Textualize/textual の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。