Terminal: Ctrl+C can't be intercepted by PowerShell for shutdown

Created on 9 Jul 2019  路  8Comments  路  Source: microsoft/terminal

Environment

Windows build number: 10.0.18362.175
Windows Terminal version: 0.2.1831.0
PowerShell Core 6.2.1
PowerShell 5.1

I'm on a Microsoft Surface Book. There is no Pause key and no Break key. Ctrl+Break is not an option for stopping a program unless I plug in an external keyboard or do some sort of key remapping specifically for this.

Steps to reproduce

Create a script in PowerShell that intercepts Ctrl+C so it can gracefully shut down. Run that script and try to use Ctrl+C. The script can't intercept it.

It behaves the same in PowerShell Core and in PowerShell.

[Console]::TreatControlCAsInput = $True
Start-Sleep -Seconds 1
$Host.UI.RawUI.FlushInputBuffer()
$exiting = $false
while ((-Not $exiting)) {
    Write-Host "Sleeping..."
    Start-Sleep -Seconds 1
    while ($Host.UI.RawUI.KeyAvailable -and ($Key = $Host.UI.RawUI.ReadKey("AllowCtrlC,NoEcho,IncludeKeyUp"))) {
        If ([Int]$Key.Character -eq 3) {
            Write-Warning "Got CTRL-C!"
            $exiting = $true
        }

        $Host.UI.RawUI.FlushInputBuffer()
    }
}
Write-Host "Done."
[Console]::TreatControlCAsInput = $False

Expected behavior

I should be able to hit Ctrl+C to stop the program. In a standard PowerShell terminal (not hosted in the new Microsoft Terminal) this works fine.

Actual behavior

The program pauses, ostensibly to allow copy/paste behavior to work, and if I hit Enter it continues running. There is no way to capture Ctrl+C.

Area-Input Issue-Bug Priority-2 Product-Conpty Resolution-Fix-Committed v1-Scrubbed

Most helpful comment

This is a breaking issue for me.

All 8 comments

This reproduces with pwincon as well. It's not a copy/paste issue, but definitely something in ConPTY. Tagged.

Just ran into this when using Get-Content -Tail 10 -Wait to tail a log file. Kinda annoying that I can't hit Ctrl+C to return back to the prompt to switch to a different log file...and I'm in the same boat as @tillig with no pause/break key (on a MacBook Pro.)

EDIT: My issue was unrelated to the main thread. Apologies for any confusion!

I'm getting something similar. Try to cancel a long-running PowerShell command using Ctrl-C just terminates the entire terminal tab instead of cancelling the command. It's very disruptive :(. If this is sounds like a different issue, I can file a separate bug. This does not repro when using pwsh.exe directly.

PowerShellCtrlCBug

(The window disappears when I press Ctrl-C).

This is a breaking issue for me.

FYI, my issue ended up being something unrelated (and a problem on my side, I was launching a wrapper executable around pwsh because I was using scoop and that was terminating from the Ctrl-C). Apologies for any confusion! I don't think it's related to the original issue.

Dropping this here to let you know this might've been fixed with PowerShell v7 which comes with a fix to unexpected Ctrl+C behavior powershell/powershell#11959

Installed Powershell Core v7 but still hitting the issue. Cannot get Ctrl-C as a input.

:tada:This issue was addressed in #5431, which has now been successfully released as Windows Terminal Preview v0.11.1191.0.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings