Skip to content

"Clean" block does not run or partially runs when using Ctrl+C #24241

@FinneganMacePike

Description

@FinneganMacePike

Prerequisites

Steps to reproduce

Code to reproduce error:

function Test-Function {
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$InputTst,
        [Parameter(Mandatory = $false)]
        [switch]$ShowInput
    )
    if ($ShowInput) {
         Write-Host "You wrote: $( $InputTst )"
    }
}

function Start-Main {
    [CmdletBinding()]
    param()
    begin {}
    process {
        $Close = $false
        Write-Host "Process block runs."
        # Test 1 - Does not work.
        # $Testing = Read-Host "Please type something: "
        # Test 2 - Does not work.
        # Test-Function -ShowInput
        # Test 3 - Works properly.
        # Write-Host "Start Sleep"
        # Start-Sleep("10")
    }
    end {}
    clean {
        Write-Host "Clean block runs."
        Write-Host "Cleaning...."
        $Close = $true
        Write-Host "Value: $( $Close )"
    }
}

try {
    Start-Main
}
finally {
    Write-Host "Finally block runs."
    Write-Host "Finally...."
    $Close = 8
    Write-Host "Value: $( $Close )"
}

Test 1

Uncomment the "Test 1" part of the process. Run the code. Ctrl+C when prompted for function parameter input. Sometimes, none of the "Clean" block output even shows at all. I have another personal script where this is the case. Other times, only the first Write-Host is run. However, in every case, the full "Clean Block" does not run. This seems to occur whenever Ctrl+C is used and the user is prompted for input.

Test 2

Comment the "Test 1" and uncomment the "Test 2" part of the process. Run the code. Ctrl+C when prompted for user input. Exactly the same issue as in "Test 1".

I questioned the Microsoft documentation that states:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_methods?view=powershell-7.4
image

Test 3

Comment the "Test 2" and uncomment the "Test 3" part of the process. Run the code. Ctrl+C when the program is sleeping. The output is correct. It seems that Microsoft's documentation is in general correct, but fails specifically when terminated from a user input. I hope that this can be fixed, so that I can properly use the clean block.

Expected behavior

Test 1 >
Process block runs.

cmdlet Test-Function at command pipeline position 1
Supply values for the following parameters:
InputTst: Clean block runs.
Cleaning....
Value: True
Finally block runs.
Finally....
Value: 8

Test 2 >
Process block runs.
Please type something: : Clean block runs.
Cleaning....
Value: True
Finally block runs.
Finally....
Value: 8

Test 3 >
Process block runs.
Start Sleep
Clean block runs.
Cleaning....
Value: True
Finally block runs.
Finally....
Value: 8

Actual behavior

Test 1 >
cmdlet Test-Function at command pipeline position 1
Supply values for the following parameters:
InputTst: Clean block runs.
Finally block runs.
Finally....
Value: 8

Test 2 >
Process block runs.
Please type something: : Clean block runs.
Finally block runs.
Finally....
Value: 8

Test 3 >
Process block runs.
Start Sleep
Clean block runs.
Cleaning....
Value: True
Finally block runs.
Finally....
Value: 8

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.5
PSEdition                      Core
GitCommitId                    7.4.5
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT

Visuals

No response

Metadata

Metadata

Assignees

Labels

WG-Enginecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working Group

Type

No type

Projects

Status

Issue Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions