Skip to content

A job that is stopped while executing clean{} does not complete executing clean{} #24679

@alx9r

Description

@alx9r

A job that is stopped while it is executing clean{} interrupts execution of that clean{} block. This appears to contradict the documentation

The clean block is a convenient way for users to clean up resources... Resource cleanup is enforced for the following scenarios:

...
4. when the pipeline is being stopped by StopProcessing()

It seems that when StopProcessing() occurs while clean{} is executing, resource cleanup is interrupted rather than enforced.

Steps to reproduce

$log = [System.Collections.Concurrent.ConcurrentQueue[string]]::new()

$job =
    Start-ThreadJob  `
        -ArgumentList $log `
        -ScriptBlock {
            param($log)
            begin {}
            clean {
                $log.Enqueue('clean{} start')
                Start-Sleep 1
                $log.Enqueue('clean{} end')
            }
        }

Start-Sleep -Milliseconds 100
$job.StopJobAsync()
Start-Sleep -Milliseconds 100
$job | Receive-Job -Wait
$log

Expected output

clean{} start
clean{} end

perhaps with

OperationStopped: The pipeline has been stopped.

mentioned.

Actual output

OperationStopped: The pipeline has been stopped.
clean{} start

Related Issues

This issue arose from Provide a way to assign a returned object to a variable that is explicitly atomic when Stopping PowerShell #24658.

There are a number of other related issues as summarized in the table below. Note that none of those issues are the same as this because of the following distinctions:

  • The issues in the table are only reproducible with Ctrl+C, and not by stopping a job. This issue is reproducible by stopping a job.
  • None of the issues below contemplate the scenario where execution has already entered clean{} when execution is interrupted. This issue is about interrupting execution in clean{}.
Issue Shortened Description Ctrl+C
repro 1.
Job.Stopping
repro
#23786 Finally block isn't executed upon Ctrl+C yes no
#19988 Attempts to write to success in finally causes termination yes no
#19987 statements in finally fail with Ctrl+C and Read-Host OP N/A 2.
#24241 "Clean" block does not run on Ctrl+C yes no

Notes:

  1. "yes" and "no" in the repro columns are indicated only if I was able to successfully repeat a check for the behavior reported in the issue.
  2. Write-Host statements, file redirections in a finally block intermittently fail quietly on Windows when Ctrl+C is used to terminate a Read-Host call #19987 seems to hinge on Read-Host in a way that doesn't have an obvious analog in a job where there is no console.

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.6
PSEdition                      Core
GitCommitId                    7.4.6
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifNeeds-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