textual: Crash opening and closing dialog really fast

I’ve been stress-testing my app and I think I found a concurrency issue in the library:

<╭────────────────────────────────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────────────────────────────────────────────────────────────────────╮
│ /home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/widget.py:3734 in _compose                                                                                                                         │
│                                                                                                                                                                                                                                       │
│   3731 │   │   │   self.app._handle_exception(error)                                            ╭────────────────────────────────────────── locals ───────────────────────────────────────────╮                                       │
│   3732 │   │   else:                                                                            │    self = TradeDialog()                                                                     │                                       │
│   3733 │   │   │   self._extend_compose(widgets)                                                │ widgets = [ToastRack(id='textual-toastrack'), Tooltip(id='textual-tooltip'), TradeWidget()] │                                       │
│ ❱ 3734 │   │   │   await self.mount_composed_widgets(widgets)                                   ╰─────────────────────────────────────────────────────────────────────────────────────────────╯                                       │
│   3735 │                                                                                                                                                                                                                              │
│   3736 │   async def mount_composed_widgets(self, widgets: list[Widget]) -> None:                                                                                                                                                     │
│   3737 │   │   """Called by Textual to mount widgets after compose.                                                                                                                                                                   │
│                                                                                                                                                                                                                                       │
│ /home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/widget.py:3747 in mount_composed_widgets                                                                                                           │
│                                                                                                                                                                                                                                       │
│   3744 │   │   │   widgets: A list of child widgets.                                            ╭────────────────────────────────────────── locals ───────────────────────────────────────────╮                                       │
│   3745 │   │   """                                                                              │    self = TradeDialog()                                                                     │                                       │
│   3746 │   │   if widgets:                                                                      │ widgets = [ToastRack(id='textual-toastrack'), Tooltip(id='textual-tooltip'), TradeWidget()] │                                       │
│ ❱ 3747 │   │   │   await self.mount_all(widgets)                                                ╰─────────────────────────────────────────────────────────────────────────────────────────────╯                                       │
│   3748 │                                                                                                                                                                                                                              │
│   3749 │   def _extend_compose(self, widgets: list[Widget]) -> None:                                                                                                                                                                  │
│   3750 │   │   """Hook to extend composed widgets.                                                                                                                                                                                    │
│                                                                                                                                                                                                                                       │
│ /home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/widget.py:1016 in mount_all                                                                                                                        │
│                                                                                                                                                                                                                                       │
│   1013 │   │   """                                                                              ╭────────────────────────────────────────── locals ───────────────────────────────────────────╮                                       │
│   1014 │   │   if self.app._exit:                                                               │   after = None                                                                              │                                       │
│   1015 │   │   │   return AwaitMount(self, [])                                                  │  before = None                                                                              │                                       │
│ ❱ 1016 │   │   await_mount = self.mount(*widgets, before=before, after=after)                   │    self = TradeDialog()                                                                     │                                       │
│   1017 │   │   return await_mount                                                               │ widgets = [ToastRack(id='textual-toastrack'), Tooltip(id='textual-tooltip'), TradeWidget()] │                                       │
│   1018 │                                                                                        ╰─────────────────────────────────────────────────────────────────────────────────────────────╯                                       │
│   1019 │   if TYPE_CHECKING:                                                                                                                                                                                                          │
│                                                                                                                                                                                                                                       │
│ /home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/widget.py:947 in mount                                                                                                                             │
│                                                                                                                                                                                                                                       │
│    944 │   │   if self._closing:                                                                ╭────────────────────────────────────────── locals ───────────────────────────────────────────╮                                       │
│    945 │   │   │   return AwaitMount(self, [])                                                  │   after = None                                                                              │                                       │
│    946 │   │   if not self.is_attached:                                                         │  before = None                                                                              │                                       │
│ ❱  947 │   │   │   raise MountError(f"Can't mount widget(s) before {self!r} is mounted")        │    self = TradeDialog()                                                                     │                                       │
│    948 │   │   # Check for duplicate IDs in the incoming widgets                                │ widgets = (ToastRack(id='textual-toastrack'), Tooltip(id='textual-tooltip'), TradeWidget()) │                                       │
│    949 │   │   ids_to_mount = [                                                                 ╰─────────────────────────────────────────────────────────────────────────────────────────────╯                                       │
│    950 │   │   │   widget_id for widget in widgets if (widget_id := widget.id) is not None                                                                                                                                            │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
MountError: Can't mount widget(s) before TradeDialog() is mounted

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (8 by maintainers)

Most upvoted comments

Yep, I removed the textual completely, installed it from the tree and can no longer bang out any of the problem.

For what it’s worth, after pulling the latest changes on my fork of Textual, I can’t reproduce the issue using the simplified MRE in https://github.com/Textualize/textual/issues/4668#issuecomment-2186463224 when mashing the <kbd>o</kbd> and <kbd>Esc</kbd> keys.

The MRE can be even further simplified. Recomposition and dynamic structure is irrelevant. The issue pops up the more eagerly the more controls are on the modal screen. The last crash occurred with just two o-ESC sequences.

import sys

from textual import work
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.screen import ModalScreen
from textual.widgets import Label, Select, Footer

exc: Exception | None = None


class StressScreen(ModalScreen):
    BINDINGS = [Binding("escape", "dismiss", "dismiss")]

    def compose(self) -> ComposeResult:
        for idx in range(40):
            yield Select([(f"Foo{idx}", f"foo{idx}"), (f"Bar{idx}", f"bar{idx}")],
                         id=f"independent{idx}_value",
                         value=Select.BLANK)


class StressApp(App):
    BINDINGS = [Binding("o", "modal", "modal")]

    def compose(self) -> ComposeResult:
        yield Label("BAR")
        yield Footer()

    @work(exclusive=True)
    async def action_modal(self) -> None:
        await self.push_screen_wait(StressScreen())

    def _handle_exception(self, error: Exception) -> None:
        global exc
        exc = error
        super()._handle_exception(error)


if __name__ == "__main__":
    try:
        StressApp().run()
    finally:
        if exc:
            sys.excepthook(type(exc), exc, None)
Traceback (most recent call last):
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/message_pump.py", line 540, in _pre_process
    await self._dispatch_message(events.Compose())
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/message_pump.py", line 655, in _dispatch_message
    await self.on_event(message)
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/message_pump.py", line 724, in on_event
    await self._on_message(event)
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/message_pump.py", line 745, in _on_message
    await invoke(method, message)
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/_callback.py", line 85, in invoke
    return await _invoke(callback, *params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/_callback.py", line 47, in _invoke
    result = await result
             ^^^^^^^^^^^^
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/widget.py", line 3720, in _on_compose
    await self._compose()
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/widget.py", line 3734, in _compose
    await self.mount_composed_widgets(widgets)
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/widget.py", line 3747, in mount_composed_widgets
    await self.mount_all(widgets)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/widget.py", line 1016, in mount_all
    await_mount = self.mount(*widgets, before=before, after=after)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/widget.py", line 947, in mount
    raise MountError(f"Can't mount widget(s) before {self!r} is mounted")
textual.widget.MountError: Can't mount widget(s) before StressScreen() is mounted
Traceback (most recent call last):
  File "/home/arcivanov/Documents/src/karellen/app/src/main/python/app/stress.py", line 44, in <module>
    StressApp().run()
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/app.py", line 1620, in run
    asyncio.run(run_app())
  File "/home/arcivanov/.pyenv/versions/3.12.3/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/home/arcivanov/.pyenv/versions/3.12.3/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/arcivanov/.pyenv/versions/3.12.3/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/app.py", line 1606, in run_app
    await self.run_async(
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/app.py", line 1572, in run_async
    await app._shutdown()
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/app.py", line 2804, in _shutdown
    await self._close_all()
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/app.py", line 2784, in _close_all
    await self._prune_node(stack_screen)
  File "/home/arcivanov/.pyenv/versions/app/lib/python3.12/site-packages/textual/app.py", line 3445, in _prune_node
    raise asyncio.TimeoutError(
TimeoutError: Timeout waiting for [Label(), Footer(), ToastRack(id='textual-toastrack'), Tooltip(id='textual-tooltip')] to close; possible deadlock (consider changing App.CLOSE_TIMEOUT)