Skip to content

Finally block isn't executed upon Ctrl+C  #23786

@noseratio

Description

@noseratio

Prerequisites

Steps to reproduce

try
{
  echo Sleeping...
  Start-Sleep 5
  echo Exiting...
}
finally {
  echo Finally...
  echo "Exit code: $LASTEXITCODE"
}

Running it: conhost cmd /k pwsh -f test.ps1, then hitting Ctrl+C after Sleeping... shows up, I only see Finally... about 1 out 10 runs.

Expected behavior

The following output should be consistently displayed: 


Finally...
Exit code: 0


### Actual behavior

```console
In most cases (but not always), `finally` block doesn't get executed when Ctrl+C is pressed in the middle of `Start-Sleep` execution.

Error details

N/A

Environment data

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


### Visuals

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.WG-Enginecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working Group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions