Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

CancelledError when using yield and TabbedContent.add_pane(TabPane(..., some_child_widget, ...) in a threaded worker #3890

Closed
iscas-zac opened this issue Dec 17, 2023 · 0 comments

Comments

@iscas-zac
Copy link

The issue

When I'm working with yielders, the app prints a lot of traceback after I close it.

The sample code is like this, which I run with textual run --dev main.py with main.py containing just a app.run():

class TestApp(App):
    def compose(self) -> ComposeResult:
        yield TabbedContent()
        yield Input()

    async def on_mount(self):
        self.async_caller()

    @work(thread=True, exit_on_error=False)
    async def async_caller(self):
        a = self.call_from_thread(self.yielder)
        next(a)

    def yielder(self):
        tc = self.query_one(TabbedContent)
        # tp = TabPane("1234", id="_1234")
        # tc.add_pane(tp)
        tc.add_pane(TabPane("1234", Static(), id="_1234"))
        yield "the work is done now. close it"

After I Ctrl-C it, it exits and prints

Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\textual\message_pump.py", line 483, in _process_messages   
    if not await self._pre_process():
  File "C:\Python310\lib\site-packages\textual\message_pump.py", line 507, in _pre_process        
    await self._dispatch_message(events.Compose())
  File "C:\Python310\lib\site-packages\textual\message_pump.py", line 614, in _dispatch_message   
    await self.on_event(message)
  File "C:\Python310\lib\site-packages\textual\message_pump.py", line 683, in on_event
    await self._on_message(event)
  File "C:\Python310\lib\site-packages\textual\message_pump.py", line 704, in _on_message
    await invoke(method, message)
  File "C:\Python310\lib\site-packages\textual\_callback.py", line 81, in invoke
    return await _invoke(callback, *params)
  File "C:\Python310\lib\site-packages\textual\_callback.py", line 47, in _invoke
    result = await result
  File "C:\Python310\lib\site-packages\textual\widget.py", line 3384, in _on_compose
    await self.mount(*widgets)
  File "C:\Python310\lib\site-packages\textual\widget.py", line 117, in await_mount
    await wait(aws)
  File "C:\Python310\lib\asyncio\tasks.py", line 384, in wait
    return await _wait(fs, timeout, return_when, loop)
  File "C:\Python310\lib\asyncio\tasks.py", line 491, in _wait
    await waiter
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\textual\app.py", line 1441, in run_app
    await self.run_async(
  File "C:\Python310\lib\site-packages\textual\app.py", line 1413, in run_async
    await app._shutdown()
  File "C:\Python310\lib\site-packages\textual\app.py", line 2502, in _shutdown
    await self._close_all()
  File "C:\Python310\lib\site-packages\textual\app.py", line 2482, in _close_all
    await self._prune_node(stack_screen)
  File "C:\Python310\lib\site-packages\textual\app.py", line 3030, in _prune_node
    await asyncio.gather(*close_messages)
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "**********\main.py", line 7, in <module>
    app.run()
  File "C:\Python310\lib\site-packages\textual\app.py", line 1452, in run
    asyncio.run(run_app())
  File "C:\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
    return future.result()
asyncio.exceptions.CancelledError

Triggering conditions

The issue involves some patterns.

  • The add_pane() should sit in a yielder.
  • If I remove the worker statement and put the yielder in the async on_mount() function, it won't trigger.
  • Also the TabPane must contain a child widget (the child can also be added afterwards instead of as a parameter, both will trigger).

My textual diagnose output

Textual Diagnostics

Versions

Name Value
Textual 0.45.1
Rich 13.3.5

Python

Name Value
Version 3.10.7
Implementation CPython
Compiler MSC v.1933 64 bit (AMD64)
Executable C:\Python310\python.exe

Operating System

Name Value
System Windows
Release 10
Version 10.0.22635

Terminal

Name Value
Terminal Application vscode (1.86.0-insider)
TERM Not set
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=98, height=13
legacy_windows False
min_width 1
max_width 98
is_terminal True
encoding utf-8
max_height 13
justify None
overflow None
no_wrap False
highlight None
markup None
height None

Afterwords

I'm not sure if I use it right. I want the computation-heavy yielder task both to give some feedback when running and to run in a different thread, so I write codes with workers and yield statements. The problem doesn't affect my experience when it runs, so I guess I'll be OK if this isn't fixed.

@Textualize Textualize deleted a comment from github-actions bot Dec 18, 2023
@Textualize Textualize locked and limited conversation to collaborators Dec 18, 2023
@rodrigogiraoserrao rodrigogiraoserrao converted this issue into discussion #3895 Dec 18, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant