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

CancellationToken does not cancel ReadLine until a character has been pressed #3693

Closed
3 tasks done
rhubarb-geek-nz opened this issue May 18, 2023 · 2 comments
Closed
3 tasks done
Labels
Resolution-By Design The behavior is by design.

Comments

@rhubarb-geek-nz
Copy link

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues, especially the pinned issues.

Exception report

Not an exception

Screenshot

Nothing to show here

Environment data

PS Version: 7.3.4
PS HostName: ConsoleHost
PSReadLine Version: 2.2.6
PSReadLine EditMode: Windows
OS: 10.0.22621.1 (WinBuild.160101.0800)
BufferWidth: 146
BufferHeight: 40

Steps to reproduce

Sample app

#!/usr/bin/env pwsh

Import-Module PSReadLine

$runspace=[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace

$cancellationTokenSource = New-Object -Type System.Threading.CancellationTokenSource
$cancellationTokenSource.CancelAfter(5000)
$cancellationToken = $cancellationTokenSource.Token
try
{
	$line = [Microsoft.PowerShell.PSConsoleReadLine]::ReadLine($runspace,$executionContext,$cancellationToken,$null)

	$line
}
finally
{
	$cancellationTokenSource.Dispose()
}

The sample app should exit after 5 seconds ( or so ) but requires a key to be pressed before ReadLine detects the cancellation

Expected behavior

ReadLine should respect the cancellation and return immediately and in good order.

Actual behavior

App hangs until a key is pressed

@ghost ghost added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label May 18, 2023
@rhubarb-geek-nz rhubarb-geek-nz changed the title CancellationToken does to cancel ReadLine until a character has been pressed CancellationToken does not cancel ReadLine until a character has been pressed May 18, 2023
@daxian-dbw
Copy link
Member

This is by design because Console.ReadKey cannot be cancelled.
To workaround this, PS VSCode extension (integrated terminal) has to override the ReadKey method with a custom implementation in a hacky way.

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Aug 14, 2023
@daxian-dbw daxian-dbw added the Resolution-By Design The behavior is by design. label Aug 14, 2023
@andyleejordan
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution-By Design The behavior is by design.
Projects
None yet
Development

No branches or pull requests

3 participants