Repository: Textualize/textual

Issue:
Awaiting the mount of many widgets may break the app
If you mount many widgets in succession with `mount` and await their mount, your app may break.
Even if using `mount_all` works just fine.

In the example below, if I use `mount_all` to mount 500 labels, Textual doesn't even bat an eye.
If I use `mount` on a loop 500 times, the app breaks and doesn't even clean up properly.
The exact number may depend on your machine.

(Seems like it may be the fact that `AwaitMount` creates a task whenever it is awaited, so that's 500 tasks for `mount` vs a single task for `mount_all`.)


https://github.com/Textualize/textual/assets/5621605/2629e0cf-4138-495a-ae04-f40b8f1b9208

Code:

```py
from textual import work
from textual.app import App
from textual.widgets import Label


class LabelApp(App[None]):
    def on_mount(self):
        self.mount_labels()

    @work
    async def mount_labels(self):
        for _ in range(500):
            await self.mount(Label(";)"))

    @work
    async def mount_all_labels(self):
        await self.mount_all(Label(";)") for _ in range(500))


if __name__ == "__main__":
    LabelApp().run()
```


Discussion:
Seems to be stack related. If you increase the stack size it can handle 500 widgets.

Simpler MRE:

```python
from textual.app import App
from textual.widgets import Label

# import sys
# sys.setrecursionlimit(10**6)

class LabelApp(App[None]):
    async def on_mount(self):
        for _ in range(500):
            await self.mount(Label(";)"))

if __name__ == "__main__":
    LabelApp().run()
```

Suggest we try to get a failed stack trace, and seem if we can make it less deep.

I'm also wondering if there is something significant about mounting widgets from the mount handler, which feels like an odd thing to do, but should be possible.

*Update:* it also only occurs if you await the mounts. 
It looks like this was introduced in https://github.com/Textualize/textual/pull/3065/files. Specifically the change from `invoke` to `self._dispatch_message` is causing a stack overflow as we recursively bounce between `_dispatch_message` and `flush_next_callbacks`.

```
Fatal Python error: Cannot recover from stack overflow.
Python runtime state: initialized

Thread 0x000000016fca7000 (most recent call first):
  File "/Users/darrenburns/.pyenv/versions/3.8.12/lib/python3.8/selectors.py", line 558 in select
  File "/Users/darrenburns/code/textual/src/textual/drivers/linux_driver.py", line 276 in run_input_thread
  File "/Users/darrenburns/code/textual/src/textual/drivers/linux_driver.py", line 242 in _run_input_thread
  File "/Users/darrenburns/.pyenv/versions/3.8.12/lib/python3.8/threading.py", line 870 in run
  File "/Users/darrenburns/.pyenv/versions/3.8.12/lib/python3.8/threading.py", line 932 in _bootstrap_inner
  File "/Users/darrenburns/.pyenv/versions/3.8.12/lib/python3.8/threading.py", line 890 in _bootstrap

Thread 0x000000016ec9b000 (most recent call first):
  File "/Users/darrenburns/.pyenv/versions/3.8.12/lib/python3.8/threading.py", line 302 in wait
  File "/Users/darrenburns/.pyenv/versions/3.8.12/lib/python3.8/queue.py", line 170 in get
  File "/Users/darrenburns/code/textual/src/textual/drivers/_writer_thread.py", line 57 in run
  File "/Users/darrenburns/.pyenv/versions/3.8.12/lib/python3.8/threading.py", line 932 in _bootstrap_inner
  File "/Users/darrenburns/.pyenv/versions/3.8.12/lib/python3.8/threading.py", line 890 in _bootstrap

Current thread 0x0000000102570580 (most recent call first):
  File "/Users/darrenburns/.pyenv/versions/3.8.12/lib/python3.8/re.py", line 275 in escape
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 36 in regex_opt_inner
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in <genexpr>
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in regex_opt_inner
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in <genexpr>
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in regex_opt_inner
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in <genexpr>
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in regex_opt_inner
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in <genexpr>
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in regex_opt_inner
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in <genexpr>
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in regex_opt_inner
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in <genexpr>
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in regex_opt_inner
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in <genexpr>
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in regex_opt_inner
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in <genexpr>
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 77 in regex_opt_inner
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/regexopt.py", line 91 in regex_opt
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/lexer.py", line 491 in get
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/lexer.py", line 503 in _process_regex
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/lexer.py", line 575 in _process_state
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/lexer.py", line 559 in _process_state
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/lexer.py", line 596 in process_tokendef
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/lexer.py", line 657 in __call__
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/pygments/lexers/__init__.py", line 127 in get_lexer_by_name
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/syntax.py", line 433 in lexer
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/syntax.py", line 480 in highlight
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/syntax.py", line 649 in _get_syntax
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/segment.py", line 667 in __init__
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/syntax.py", line 621 in __rich_console__
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1332 in render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/segment.py", line 291 in split_and_crop_lines
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1372 in render_lines
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/padding.py", line 97 in __rich_console__
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1332 in render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/segment.py", line 291 in split_and_crop_lines
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1372 in render_lines
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/table.py", line 822 in _render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/table.py", line 509 in __rich_console__
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1332 in render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1336 in render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1336 in render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/segment.py", line 291 in split_and_crop_lines
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1372 in render_lines
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/padding.py", line 97 in __rich_console__
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1332 in render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/segment.py", line 291 in split_and_crop_lines
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1372 in render_lines
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/panel.py", line 224 in __rich_console__
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1332 in render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1336 in render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/console.py", line 1336 in render
  File "/Users/darrenburns/Library/Caches/pypoetry/virtualenvs/textual-DcnGcHTN-py3.8/lib/python3.8/site-packages/rich/segment.py", line 667 in __init__
  File "/Users/darrenburns/code/textual/src/textual/app.py", line 2170 in _fatal_error
  File "/Users/darrenburns/code/textual/src/textual/app.py", line 2159 in _handle_exception
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 597 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 595 in _flush_next_callbacks
  File "/Users/darrenburns/code/textual/src/textual/message_pump.py", line 624 in _dispatch_message
  ...
fish: Job 1, 'textual run --dev sandbox/darre…' terminated by signal SIGABRT (Abort)
```

Resolving patch:
diff --git a/src/textual/message_pump.py b/src/textual/message_pump.py
--- a/src/textual/message_pump.py
+++ b/src/textual/message_pump.py
@@ -592,7 +592,8 @@ async def _flush_next_callbacks(self) -> None:
         self._next_callbacks.clear()
         for callback in callbacks:
             try:
-                await self._dispatch_message(callback)
+                with self.prevent(*callback._prevent):
+                    await invoke(callback.callback)
             except Exception as error:
                 self.app._handle_exception(error)
                 break


Test patch: