Skip to content

Commit 45bc965

Browse files
authored
Fix NREs in SuspendStoppingPipeline() and RestoreStoppingPipeline() (PowerShell#11870)
1 parent 59ad531 commit 45bc965

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,15 +1575,23 @@ private static int FindMatchingHandlerByType(Type exceptionType, Type[] types)
15751575
internal static bool SuspendStoppingPipeline(ExecutionContext context)
15761576
{
15771577
LocalPipeline lpl = (LocalPipeline)context.CurrentRunspace.GetCurrentlyRunningPipeline();
1578-
bool oldIsStopping = lpl.Stopper.IsStopping;
1579-
lpl.Stopper.IsStopping = false;
1580-
return oldIsStopping;
1578+
if (lpl != null)
1579+
{
1580+
bool oldIsStopping = lpl.Stopper.IsStopping;
1581+
lpl.Stopper.IsStopping = false;
1582+
return oldIsStopping;
1583+
}
1584+
1585+
return false;
15811586
}
15821587

15831588
internal static void RestoreStoppingPipeline(ExecutionContext context, bool oldIsStopping)
15841589
{
15851590
LocalPipeline lpl = (LocalPipeline)context.CurrentRunspace.GetCurrentlyRunningPipeline();
1586-
lpl.Stopper.IsStopping = oldIsStopping;
1591+
if (lpl != null)
1592+
{
1593+
lpl.Stopper.IsStopping = oldIsStopping;
1594+
}
15871595
}
15881596

15891597
internal static void CheckActionPreference(FunctionContext funcContext, Exception exception)

test/powershell/Modules/Microsoft.PowerShell.Utility/Eventing.Tests.ps1

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3-
Describe "Event Subscriber Tests" -tags "CI" {
3+
4+
Describe "Event Subscriber Tests" -Tags "Feature" {
45
BeforeEach {
56
Get-EventSubscriber | Unregister-Event
67
}
@@ -13,7 +14,7 @@ Describe "Event Subscriber Tests" -tags "CI" {
1314
Get-EventSubscriber | Should -BeNullOrEmpty
1415
$messageData = new-object psobject
1516
$job = Start-Job { Start-Sleep -Seconds 5; 1..5 }
16-
$eventtest = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier EventSIDTest -Action {} -MessageData $messageData
17+
$null = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier EventSIDTest -Action {} -MessageData $messageData
1718
new-event EventSIDTest
1819

1920
wait-event EventSIDTest
@@ -27,12 +28,26 @@ Describe "Event Subscriber Tests" -tags "CI" {
2728
It "Access a global variable from an event action." {
2829
Get-EventSubscriber | Should -BeNullOrEmpty
2930
set-variable incomingGlobal -scope global -value globVarValue
30-
$eventtest = register-engineevent -SourceIdentifier foo -Action {set-variable -scope global -name aglobalvariable -value $incomingGlobal}
31+
$null = register-engineevent -SourceIdentifier foo -Action {set-variable -scope global -name aglobalvariable -value $incomingGlobal}
3132
new-event foo
3233
$getvar = get-variable aglobalvariable -scope global
3334
$getvar.Name | Should -Be aglobalvariable
3435
$getvar.Value | Should -Be globVarValue
3536
Unregister-Event foo
3637
Get-EventSubscriber | Should -BeNullOrEmpty
3738
}
39+
40+
It 'Should not throw when having finally block in Powershell.Exiting Action scriptblock' {
41+
$pwsh = "$PSHOME/pwsh"
42+
$output = & $pwsh {
43+
Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action {
44+
try{
45+
try{} finally{}
46+
}
47+
catch{ Write-Host "Exception" -Nonewline }
48+
}
49+
} | Out-String
50+
51+
$output | Should -Not -BeLike "*Exception*"
52+
}
3853
}

0 commit comments

Comments
 (0)