-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
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 byStopProcessing()
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
$logExpected output
clean{} start
clean{} endperhaps with
OperationStopped: The pipeline has been stopped.
mentioned.
Actual output
OperationStopped: The pipeline has been stopped.
clean{} startRelated 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 inclean{}.
| Issue | Shortened Description | Ctrl+C repro 1. |
Job.Stoppingrepro |
|---|---|---|---|
| #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:
- "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.
Write-Hoststatements, file redirections in afinallyblock intermittently fail quietly on Windows when Ctrl+C is used to terminate aRead-Hostcall #19987 seems to hinge onRead-Hostin 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