Fix NREs in SuspendStoppingPipeline() and RestoreStoppingPipeline() (#11870)

This commit is contained in:
Ilya
2020-02-20 11:57:00 -08:00
committed by GitHub
parent 59ad53181e
commit 45bc965af8
2 changed files with 30 additions and 7 deletions
@@ -1575,15 +1575,23 @@ namespace System.Management.Automation
internal static bool SuspendStoppingPipeline(ExecutionContext context)
{
LocalPipeline lpl = (LocalPipeline)context.CurrentRunspace.GetCurrentlyRunningPipeline();
bool oldIsStopping = lpl.Stopper.IsStopping;
lpl.Stopper.IsStopping = false;
return oldIsStopping;
if (lpl != null)
{
bool oldIsStopping = lpl.Stopper.IsStopping;
lpl.Stopper.IsStopping = false;
return oldIsStopping;
}
return false;
}
internal static void RestoreStoppingPipeline(ExecutionContext context, bool oldIsStopping)
{
LocalPipeline lpl = (LocalPipeline)context.CurrentRunspace.GetCurrentlyRunningPipeline();
lpl.Stopper.IsStopping = oldIsStopping;
if (lpl != null)
{
lpl.Stopper.IsStopping = oldIsStopping;
}
}
internal static void CheckActionPreference(FunctionContext funcContext, Exception exception)
@@ -1,6 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "Event Subscriber Tests" -tags "CI" {
Describe "Event Subscriber Tests" -Tags "Feature" {
BeforeEach {
Get-EventSubscriber | Unregister-Event
}
@@ -13,7 +14,7 @@ Describe "Event Subscriber Tests" -tags "CI" {
Get-EventSubscriber | Should -BeNullOrEmpty
$messageData = new-object psobject
$job = Start-Job { Start-Sleep -Seconds 5; 1..5 }
$eventtest = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier EventSIDTest -Action {} -MessageData $messageData
$null = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier EventSIDTest -Action {} -MessageData $messageData
new-event EventSIDTest
wait-event EventSIDTest
@@ -27,7 +28,7 @@ Describe "Event Subscriber Tests" -tags "CI" {
It "Access a global variable from an event action." {
Get-EventSubscriber | Should -BeNullOrEmpty
set-variable incomingGlobal -scope global -value globVarValue
$eventtest = register-engineevent -SourceIdentifier foo -Action {set-variable -scope global -name aglobalvariable -value $incomingGlobal}
$null = register-engineevent -SourceIdentifier foo -Action {set-variable -scope global -name aglobalvariable -value $incomingGlobal}
new-event foo
$getvar = get-variable aglobalvariable -scope global
$getvar.Name | Should -Be aglobalvariable
@@ -35,4 +36,18 @@ Describe "Event Subscriber Tests" -tags "CI" {
Unregister-Event foo
Get-EventSubscriber | Should -BeNullOrEmpty
}
It 'Should not throw when having finally block in Powershell.Exiting Action scriptblock' {
$pwsh = "$PSHOME/pwsh"
$output = & $pwsh {
Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action {
try{
try{} finally{}
}
catch{ Write-Host "Exception" -Nonewline }
}
} | Out-String
$output | Should -Not -BeLike "*Exception*"
}
}