Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$PSCmdlet.Stopping is false when stopping #6322

Closed
alx9r opened this issue Mar 6, 2018 · 8 comments
Closed

$PSCmdlet.Stopping is false when stopping #6322

alx9r opened this issue Mar 6, 2018 · 8 comments
Labels
Resolution-No Activity Issue has had no activity for 6 months or more

Comments

@alx9r
Copy link

alx9r commented Mar 6, 2018

Steps to reproduce

function a {
    param( [Parameter(ValueFromPipeline)]$InputObject )
    process {
        try { $InputObject }
        finally {
            [console]::WriteLine('a finally')
            [console]::WriteLine("a Stopping: $($PSCmdlet.Stopping)")
        }
    }
}
Add-Type '
    using System;
    using System.Management.Automation; 
    
    [Cmdlet("Invoke", "b")]
    public class b : Cmdlet 
    {
        [Parameter(ValueFromPipeline=true)]
        public object InputObject { get; set; }

        protected override void ProcessRecord()
        { 
            WriteObject(InputObject,true);
        }

        protected override void StopProcessing()
        {
            Console.WriteLine("b StopProcessing");
            Console.WriteLine("b Stopping: {0}",Stopping);
        }
    }' -PassThru | % Assembly | Import-Module
Set-Alias b Invoke-b

1 | a | b | % { sleep 10 } # press Ctrl-C here

Expected behavior

b StopProcessing
b Stopping: True
a finally
a Stopping: True

Actual behavior

b StopProcessing
b Stopping: True
a finally
a Stopping: False

Environment data

> $PSVersionTable

Name                           Value                                            
----                           -----                                            
PSVersion                      6.0.0                                            
PSEdition                      Core                                             
GitCommitId                    v6.0.0                                           
OS                             Microsoft Windows 6.3.9600                       
Platform                       Win32NT                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                          
PSRemotingProtocolVersion      2.3                                              
SerializationVersion           1.1.0.1                                          
WSManStackVersion              3.0                                              
@powercode
Copy link
Collaborator

Interesting that

1 |  b | b | % { sleep 10 } # press Ctrl-C here

b StopProcessing
b Stopping: True
b StopProcessing
b Stopping: False

@powercode
Copy link
Collaborator

powercode commented Mar 12, 2018

and

1 |  a | a | % { sleep 10 } # press Ctrl-C here

a finally
a Stopping: False
a finally
a Stopping: False

@alx9r
Copy link
Author

alx9r commented Mar 12, 2018

@powercode I'm not seeing that on my computer for 1 | b | b | % { sleep 10 }. I see Stopping: True for both invocations of b:

1 | b | b | % { sleep 10 } # press Ctrl-C here
b StopProcessing
b Stopping: True
b StopProcessing
b Stopping: True

@BrucePay
Copy link
Collaborator

Weird. Of course it doesn't really matter what Stopping is set to in StopProcessing(). If you're in StopProcessing(), you're stopping by definition :-) (Some background: this method is called on the break handler thread, not the pipeline thread with the intent that be able to unblock the pipeline thread if it's hung doing IO or some such.)

@alx9r
Copy link
Author

alx9r commented Mar 15, 2018

@BrucePay

Some background: this method is called on the break handler thread, not the pipeline thread with the intent that be able to unblock the pipeline thread if it's hung doing IO or some such.

Oh I see. Is that why there is no analogous stop {} block available in PSCmdlets?

Because PSCmdlets have no stop{} block, but finally{} does run on ctrl-C, I was hoping to be able to predicate certain cleanup tasks on $PSCmdlet.Stopping.

Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label Nov 16, 2023
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

Copy link
Contributor

This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution-No Activity Issue has had no activity for 6 months or more
Projects
None yet
Development

No branches or pull requests

3 participants