Skip to content

DirectoryTree: expanding a folder after the _loader worker is cancelled blocks app exit forever #6799

Open

Description

@visionik

Hi! We use DirectoryTree in a Textual app and found that quitting while a folder is being expanded can leave the app unable to exit.

DirectoryTree._on_tree_node_expanded awaits _add_to_load_queue(node), which returns AwaitComplete(self._load_queue.join()). join() only finishes when the _loader worker (@work(exclusive=True, group="_loader")) calls task_done() for that node. If the worker has been cancelled first, as happens while the app shuts down, join() never finishes. The handler never returns, so the tree's message loop never ends, and each ancestor's _message_loop_exit waits on it, up to the screen. The app never exits. (The same join() would also never finish if reload() replaces _load_queue while a node is waiting on the old one.)

Repro (Textual 8.2.7; _add_to_load_queue is the same on main at 5e5b7ef):

import asyncio
import sys
import tempfile
from pathlib import Path

from textual.app import App, ComposeResult
from textual.widgets import DirectoryTree


class TreeApp(App):
    def __init__(self, path: Path) -> None:
        super().__init__()
        self.path = path

    def compose(self) -> ComposeResult:
        yield DirectoryTree(self.path)


async def main() -> None:
    root = Path(tempfile.mkdtemp())
    (root / "sub").mkdir()
    (root / "sub" / "file.txt").write_text("x")
    app = TreeApp(root)
    async with app.run_test() as pilot:
        tree = app.query_one(DirectoryTree)
        await pilot.pause(0.5)  # the root has loaded
        sub = next(node for node in tree.root.children if node.label.plain == "sub")
        app.workers.cancel_group(tree, "_loader")  # as happens when the app shuts down
        sub.expand()
        await pilot.pause()
    print("exited")


async def run() -> None:
    task = asyncio.ensure_future(main())
    done, _ = await asyncio.wait({task}, timeout=10)
    if not done:
        print("hung: the app did not exit within 10 s", file=sys.__stderr__, flush=True)
        import os

        os._exit(1)


asyncio.run(run())

Output, every time (3 of 3 runs):

hung: the app did not exit within 10 s

Without the cancel_group line it prints exited. Cancelling the group stands in for shutdown cancelling the worker; we hit it for real when an app exits right after mounting, while the tree is still expanding a folder.

I think the expand handler shouldn't wait on join() once the loader worker has ended or the widget is closing (or shouldn't await it at all). In our app we override _add_to_load_queue to wait on join() in short steps and stop when the widget is closing, the queue has been replaced, or the _loader worker is cancelled or finished. I'm happy to open a PR along those lines if that approach suits you, or something else if you'd rather.

Activity

  1. github-actions commented on Nov 20, 2026

    @github-actions

    We found the following entry in the FAQ which you may find helpful:

    Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

    This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).

    This is an automated reply, generated by FAQtory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions