Command palette waits for a 0.1 s timeout after the last provider finishes before showing results

Open
#6,781 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli

Research direction

Start with CommandPalette._search_for, especially its wait on commands.get() and how provider tasks finish; the issue includes a reproduction script using InstantProvider and TimedPalette. Verify that the list refreshes promptly after the final provider finishes, without the roughly 0.1-second delay.

Written by the indexing model from the issue text.

Description

Hi! I hit this while writing generated tests against Textual's widgets.

CommandPalette._search_for loops while any provider's task is running, waiting on wait_for(commands.get(), 0.1). When a provider finishes, it puts nothing on the queue, so if the loop is already waiting it sleeps until the timeout before it notices. The list is filled at the end of the search, so with a provider that does any async work after its last hit, the results appear about 0.1 s late on every keystroke.

import asyncio
from time import perf_counter

import textual
from textual.app import App
from textual.command import CommandPalette, Hit, Hits, Provider

provider_done: list[float] = []
list_filled: list[float] = []

class InstantProvider(Provider):
    async def search(self, query: str) -> Hits:
        yield Hit(1.0, f"Result for {query!r}", lambda: None)
        await asyncio.sleep(0.01)  # e.g. checking a source for more results
        provider_done.append(perf_counter())

class TimedPalette(CommandPalette):
    def _refresh_command_list(self, command_list, commands, clear_current) -> None:
        super()._refresh_command_list(command_list, commands, clear_current)
        list_filled.append(perf_counter())

class PaletteApp(App):
    COMMANDS = {InstantProvider}

async def main() -> None:
    app = PaletteApp()
    async with app.run_test() as pilot:
        app.push_screen(TimedPalette())
        await pilot.pause()
        for key in "abcde":
            provider_done.clear()
            list_filled.clear()
            await pilot.press(key)
            await pilot.pause(0.3)
            delay = list_filled[-1] - provider_done[-1]
            print(f"query {key!r}: list filled {delay * 1000:.0f} ms after the provider finished")
            await pilot.press("backspace")
            await pilot.pause(0.3)
    print("Textual", textual.__version__)

asyncio.run(main())

Output on Textual 8.2.7:

query 'a': list filled 92 ms after the provider finished
query 'b': list filled 93 ms after the provider finished
query 'c': list filled 92 ms after the provider finished
query 'd': list filled 91 ms after the provider finished
query 'e': list filled 92 ms after the provider finished

Main at 5e5b7ef gives 91 to 92 ms. (TimedPalette only records when the list is refreshed.)

A provider that finishes in the same step as its last put doesn't see the delay, which is probably why it isn't obvious with the built-in providers. I expected the list to fill as soon as the last provider is done. A small fix is to wake the loop when a provider's task finishes, using a done callback that puts a None marker on the queue. With that, the same script gives 2 to 3 ms. I have a PR with a test ready.

Dominant language
Python
Stars
37.7k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.