diff --git a/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs index 98b2cb74cc..940ac0dcbf 100644 --- a/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs +++ b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs @@ -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) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Eventing.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Eventing.Tests.ps1 index 6509b226c8..eeffcf70ed 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Eventing.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Eventing.Tests.ps1 @@ -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*" + } }