Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awaiting the mount of many widgets may break the app #4022

Closed
rodrigogiraoserrao opened this issue Jan 15, 2024 · 3 comments · Fixed by #4078
Closed

Awaiting the mount of many widgets may break the app #4022

rodrigogiraoserrao opened this issue Jan 15, 2024 · 3 comments · Fixed by #4078
Assignees
Labels
bug Something isn't working enhancement New feature or request Task

Comments

@rodrigogiraoserrao
Copy link
Contributor

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.)

Screen.Recording.2024-01-15.at.11.53.28.mov

Code:

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()
@rodrigogiraoserrao rodrigogiraoserrao added bug Something isn't working enhancement New feature or request Task labels Jan 15, 2024
@willmcgugan
Copy link
Collaborator

willmcgugan commented Jan 21, 2024

Seems to be stack related. If you increase the stack size it can handle 500 widgets.

Simpler MRE:

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.

@darrenburns darrenburns self-assigned this Jan 30, 2024
@darrenburns
Copy link
Member

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)

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants