Skip to content

Prompt Cancellation is broken #1076

@azchohfi

Description

@azchohfi

Information

  • OS: Windows
  • Version: 0.45.0
  • Terminal: Windows Terminal

Describe the bug
Issue #417 mentions that cancellation should be supported, and PR #477 is said to fix it, but it you, for example, press Ctrl+C while selecting items from a list, the process gets stuck.

I debugged the source, and I believe I found the root cause.
This is the only line of code that checks for cancellation, at all, and this is correct:

if (cancellationToken.IsCancellationRequested)
{
return null;
}

The problem is that the SelectionPrompt, and likely all other Prompts, are doing this:

while (true)
{
var rawKey = await _console.Input.ReadKeyAsync(true, cancellationToken).ConfigureAwait(false);
if (rawKey == null)
{
continue;

They should also check for the cancellation of the cancellation token before they call the underlying method.

To Reproduce

await new SelectionPrompt<T>()
...
    .AddChoices(choices)
    .ShowAsync(AnsiConsole.Console, ct)

Then simply cancel the cancellation token ct, and the process will get stuck.

Expected behavior
The SelectionPrompt.ShowAsync(...) method should return when the cancellation token is cancelled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageSomeone needs to take a look at this

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions