Skip to content

Commit a43f0f8

Browse files
committed
🐛 Use Child CancelTokens to avoid cascading cancels
1 parent e594479 commit a43f0f8

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

ModuleFast.psm1

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ function Install-ModuleFast {
232232
| Out-File -FilePath $CILockFilePath -Encoding UTF8
233233
}
234234
}
235-
clean {
235+
CLEAN {
236236
$cancelSource.Dispose()
237237
}
238238
}
@@ -300,9 +300,10 @@ function Get-ModuleFastPlan {
300300
$ErrorActionPreference = 'Stop'
301301
[HashSet[ModuleFastSpec]]$modulesToResolve = @()
302302

303-
#We use this token to cancel the HTTP requests if the user hits ctrl-C without having to dispose of the HttpClient
304-
$cancelTokenSource = [CancellationTokenSource]::new()
305-
$CancellationToken ??= $cancelTokenSource.Token
303+
#We use this token to cancel the HTTP requests if the user hits ctrl-C without having to dispose of the HttpClient.
304+
#We get a child so that a cancellation here does not affect any upstream commands
305+
$cancelTokenSource = $CancellationToken ? [CancellationTokenSource]::CreateLinkedTokenSource($CancellationToken) : [CancellationTokenSource]::new()
306+
$CancellationToken = $cancelTokenSource.Token
306307

307308
#We pass this splat to all our HTTP requests to cut down on boilerplate
308309
$httpContext = @{
@@ -621,6 +622,12 @@ function Install-ModuleFastHelper {
621622
[HttpClient]$HttpClient,
622623
[switch]$Update
623624
)
625+
BEGIN {
626+
#We use this token to cancel the HTTP requests if the user hits ctrl-C without having to dispose of the HttpClient.
627+
#We get a child so that a cancellation here does not affect any upstream commands
628+
$cancelTokenSource = $CancellationToken ? [CancellationTokenSource]::CreateLinkedTokenSource($CancellationToken) : [CancellationTokenSource]::new()
629+
$CancellationToken = $cancelTokenSource.Token
630+
}
624631
END {
625632
$ErrorActionPreference = 'Stop'
626633

0 commit comments

Comments
 (0)