From fbded5b3d03d8bb99568ce2a472730c920306dc8 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Wed, 2 Dec 2026 00:59:06 -0500 Subject: [PATCH 1/2] Warn when -GroupProvider outputs more than one value in a call Several values in one call (a stray ArrayList.Add index next to the group, or a list returned without the leading comma) are taken together as one group, as before, but now with a warning once per run that says how many values arrived. The provider's output is counted as it comes down the pipeline, since collecting it by assignment can't tell one list from several values. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 29 +++++++++++++++++++++++++++++ ParallelKit.ps1 | 29 ++++++++++++++++++++++++++--- README.md | 2 +- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index 8bf8573..bca7630 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -4420,6 +4420,35 @@ $null = Invoke-PkProcessDispatch -MaxConcurrent 1 ` -JobFinalize { param($job, $pending) } Test-Assert ($glSeen.Count -eq 1 -and $glSeen[0] -is [array] -and ($glSeen[0] -join ',') -eq 'a,b,c' -and $script:messages.Count -eq 0) ("-GroupProvider returning ,@('a', 'b', 'c') gives one group holding the list, without a warning (" + $glSeen.Count + " group(s))") +# -GroupProvider outputting several values in one call: they're one group, as +# without the comma, and the run warns once, with how many arrived. One value, +# $null, nothing, and a list returned with a comma (one value, even holding one +# item) aren't warned about. Each provider gives two groups, if it gives any. +$gvCases = [ordered]@{ + 'one value' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; return ('g' + $gv.Calls) }; Groups = 'g1|g2'; Count = 0 } + '$null' = @{ Provider = { return $null }; Groups = ''; Count = 0 } + 'nothing' = @{ Provider = { }; Groups = ''; Count = 0 } + 'a list with the comma' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; return ,@('a', 'b') }; Groups = '[a,b]|[a,b]'; Count = 0 } + 'a one-item list, comma' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; return ,@('a') }; Groups = '[a]|[a]'; Count = 0 } + 'a stray value and the group' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; $gv.Log.Add('x'); return ('g' + $gv.Calls) }; Groups = '[0,g1]|[1,g2]'; Count = 2 } + 'a list without the comma' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; return @('a', 'b', 'c') }; Groups = '[a,b,c]|[a,b,c]'; Count = 3 } +} +foreach ($gvName in $gvCases.Keys) { + Reset-Harness + $gvCase = $gvCases[$gvName] + $gv = @{ Calls = 0; Log = [System.Collections.ArrayList]::new() } + $gvSeen = [System.Collections.Generic.List[string]]::new() + $null = Invoke-PkProcessDispatch -MaxConcurrent 1 -GroupProvider $gvCase.Provider.GetNewClosure() ` + -JobBuilder { param($g) if ($g -is [array]) { $gvSeen.Add('[' + ($g -join ',') + ']') } else { $gvSeen.Add([string]$g) }; return @() }.GetNewClosure() ` + -JobFinalize { param($job, $pending) } + $gvWarnings = @($script:messages | Where-Object { $_.Level -eq 'Warn' }) + $gvText = ($gvWarnings | ForEach-Object { $_.Message }) -join ' / ' + $gvOk = (($gvSeen -join '|') -eq $gvCase.Groups) + if ($gvCase.Count -eq 0) { $gvOk = $gvOk -and $gvWarnings.Count -eq 0 } + else { $gvOk = $gvOk -and $gvWarnings.Count -eq 1 -and $gvText -like ('*-GroupProvider output ' + $gvCase.Count + ' values*') -and $gvText -like '*$null = ...*' -and $gvText -like '*return ,$list*' } + Test-Assert $gvOk ("-GroupProvider giving " + $gvName + ": groups " + ($gvSeen -join '|') + ", " + $gvWarnings.Count + " warning(s)" + $(if ($gvText) { ': ' + $gvText })) +} + # -NextJobProvider: only the last value decides. Two hashtables run the # second, with a warning about the first; a hashtable let out before # `return $null` isn't run: the run ends, with a warning. diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 6a9f9c6..2272fd9 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -2347,8 +2347,9 @@ function Invoke-PkProcessDispatch { # .PARAMETER GroupProvider # { } returning the next group, or $null when there are no more, and # outputting nothing else. Return a list as one group with a comma: - # return ,@('a', 'b'). Output ending in $null is taken as the end, with a - # warning, so a list group can't end in $null. + # return ,@('a', 'b'). Several values output in one call are taken + # together as one group, with a warning. Output ending in $null is taken + # as the end, with a warning, so a list group can't end in $null. # .PARAMETER JobBuilder # { param($group) } returning the group's job hashtable(s), or @() for none. # .PARAMETER JobStarter @@ -2683,6 +2684,9 @@ function Invoke-PkProcessDispatchCore { # Invoke-PkCallback's -PkContext: callbacks that used continue/break are # warned about once each, under this prefix. $pkCallbackContext = @{ Prefix = 'ProcessDispatch: '; Warned = @{} } + # -GroupProvider outputting several values in one call is warned about + # once per run. + $pkProviderWarned = $false # Teardown is in a finally, guarded by this flag: when PowerShell stops # the script (Ctrl+C outside Invoke-PkRun, the runspace engine # force-stopping a worker), catch blocks are skipped. @@ -2703,7 +2707,26 @@ function Invoke-PkProcessDispatchCore { } while ((-not $pkProviderExhausted) -and $pkActiveGroupCount -lt $pkMaxQueueAhead -and (-not $pkCancelToken.IsCancellationRequested)) { - $pkGroupInfo = Invoke-PkCallback -PkCallback $pkGroupProvider -PkName '-GroupProvider' -PkContext $pkCallbackContext + # Invoke-PkCallback inlined, to count the values as they come down + # the pipeline. Collected by assignment, as Invoke-PkCallback does, + # `return ,@('a', 'b')` (one value, a list) and two values output + # separately are the same array, and only the second is a mistake. + $pkProvided = [System.Collections.Generic.List[object]]::new() + $pkCbDone = $false + do { & $pkGroupProvider | ForEach-Object { $pkProvided.Add($_) }; $pkCbDone = $true } while ($false) + if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-GroupProvider' -PkContext $pkCallbackContext } + # What assignment would give: one value as itself, several as one + # array, which is the group (unchanged; it only gets a warning). + $pkGroupInfo = $null + if ($pkProvided.Count -eq 1) { $pkGroupInfo = $pkProvided[0] } + elseif ($pkProvided.Count -gt 1) { + $pkGroupInfo = $pkProvided.ToArray() + # Output ending in $null has its own warning, below. + if (-not $pkProviderWarned -and $null -ne $pkGroupInfo[-1]) { + $pkProviderWarned = $true + Write-PkMessage ("ProcessDispatch: -GroupProvider output " + $pkProvided.Count + " values in one call, taken together as one group. Output one value per call: `$null = ... discards a value you don't want (an ArrayList.Add index), and return ,`$list returns a list as one group. Shown once per run.") -Level Warn + } + } # A value the provider leaks before `return $null` (an ArrayList.Add # index) arrives as @(, $null). Taken as one more group, the # dispatcher would ask again forever, so output ending in $null is diff --git a/README.md b/README.md index f48dca0..945b141 100644 --- a/README.md +++ b/README.md @@ -568,7 +568,7 @@ All of these callbacks run on whichever thread called the dispatcher: the main t - **`-GroupCompleteCallback` also runs for a group an abort cut short**, so you can record the jobs in it that did finish. Before doing anything irreversible there (zipping the folder, deleting sources), check `Test-PkCancelRequested` (or your own `-CancelToken`) and skip it if true. - Groups are pulled lazily, at most `-MaxQueueAhead` (default 3) ahead. - A group can be any value: a hashtable, a path, a name. Each job gets its group as `$job.Group`. -- Each `-GroupProvider` call outputs exactly one group, or `$null` to end, and nothing else: `return $group`, `return ,@('a', 'b', 'c')` for a list as one group (the comma keeps a one-item list a list), or `return $null`. Anything else it lets out (an `ArrayList.Add` index, say) becomes part of the group, so discard it with `$null = ...`. Output that ends in `$null` is taken as the end, with a warning, so a list group can't end in `$null`. +- Each `-GroupProvider` call outputs exactly one group, or `$null` to end, and nothing else: `return $group`, `return ,@('a', 'b', 'c')` for a list as one group (the comma keeps a one-item list a list), or `return $null`. Anything else it lets out (an `ArrayList.Add` index, say) becomes part of the group, so discard it with `$null = ...`. When a call outputs more than one value (a stray value, or a list returned without the comma), the dispatcher warns once per run, with how many values arrived; the group is the same as without the warning. Output that ends in `$null` is taken as the end, with a warning, so a list group can't end in `$null`. - `-GroupReleaseCheck { param($group) }` makes a group with zero jobs keep its queue-ahead slot until the check returns true. Use it to stop one dispatcher racing ahead of another that it's paired with. The dispatcher doesn't return until every held group has been released, unless the run is canceled or aborted. It runs on every loop pass, so keep it cheap. Only its last output value counts, so a stray value written before the answer doesn't release the group. From 05ce00cabd4a819ad8fa04b0bd6dd6bace4b9ef0 Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Wed, 2 Dec 2026 01:03:45 -0500 Subject: [PATCH 2/2] Keep a group value exactly as the provider output it Count -GroupProvider's values with @() instead of passing each through a .NET list, whose Add() unwrapped a value's PSObject and lost any note property on it. The harness's static guard check accepts the @(do { } while ($false)) form. Co-Authored-By: Claude Opus 5.5 --- ParallelKit.Harness.ps1 | 12 ++++++++++++ ParallelKit.ps1 | 24 +++++++++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index bca7630..4e257d1 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3989,6 +3989,9 @@ function Get-HarnessUnguardedCallbacks { $flagPattern = [regex]::Escape($flag) + '(?![\w:])' $statement = $loop if ($loop.Parent -is [System.Management.Automation.Language.AssignmentStatementAst]) { $statement = $loop.Parent } + # `$x = @(do { ... } while ($false))`, which counts the values the + # callback output (-GroupProvider's call). + elseif ($loop.Parent.Parent -is [System.Management.Automation.Language.ArrayExpressionAst] -and $loop.Parent.Statements.Count -eq 1 -and $loop.Parent.Parent.Parent.Parent -is [System.Management.Automation.Language.AssignmentStatementAst]) { $statement = $loop.Parent.Parent.Parent.Parent } $siblings = $statement.Parent.Statements $index = $siblings.IndexOf($statement) # The last statement before the call that mentions the flag resets it. @@ -4449,6 +4452,15 @@ foreach ($gvName in $gvCases.Keys) { Test-Assert $gvOk ("-GroupProvider giving " + $gvName + ": groups " + ($gvSeen -join '|') + ", " + $gvWarnings.Count + " warning(s)" + $(if ($gvText) { ': ' + $gvText })) } +# A group value keeps what PowerShell added to it: a note property on a string. +Reset-Harness +$gn = @{ Given = $false; Tag = 'not seen' } +$null = Invoke-PkProcessDispatch -MaxConcurrent 1 ` + -GroupProvider { if ($gn.Given) { return $null }; $gn.Given = $true; return ('a' | Add-Member -NotePropertyName Tag -NotePropertyValue 'pk-tag' -PassThru) }.GetNewClosure() ` + -JobBuilder { param($g) $gn.Tag = [string]$g.Tag; return @() }.GetNewClosure() ` + -JobFinalize { param($job, $pending) } +Test-Assert ($gn.Tag -eq 'pk-tag') ("a group value keeps a note property PowerShell added to it (Tag: " + $gn.Tag + ")") + # -NextJobProvider: only the last value decides. Two hashtables run the # second, with a warning about the first; a hashtable let out before # `return $null` isn't run: the run ends, with a warning. diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 2272fd9..235dc7d 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1184,7 +1184,8 @@ function Invoke-PkCallback { # only the callback, like `return`. A labeled `break outer` still reaches # the caller's loop. Inlined where a callback runs per job or per result # (on 5.1 this call more than doubled the dispatcher's own time per job), - # and in Invoke-PkMessageHandler. + # in Invoke-PkMessageHandler, and for -GroupProvider in + # Invoke-PkProcessDispatchCore, which counts the values it outputs. # pk-prefixed: in a worker, a callback without .GetNewClosure() sees this frame. param( [Parameter(Mandatory)][scriptblock]$PkCallback, @@ -2707,22 +2708,23 @@ function Invoke-PkProcessDispatchCore { } while ((-not $pkProviderExhausted) -and $pkActiveGroupCount -lt $pkMaxQueueAhead -and (-not $pkCancelToken.IsCancellationRequested)) { - # Invoke-PkCallback inlined, to count the values as they come down - # the pipeline. Collected by assignment, as Invoke-PkCallback does, - # `return ,@('a', 'b')` (one value, a list) and two values output - # separately are the same array, and only the second is a mistake. - $pkProvided = [System.Collections.Generic.List[object]]::new() + # Invoke-PkCallback inlined, to count the values the provider + # output. Collected by plain assignment, `return ,@('a', 'b')` (one + # value, a list) and two values output separately are the same + # array, and only the second is a mistake. @() keeps one value per + # element, each as it was output (a ForEach-Object or a .NET + # collection's Add() would change a value's PSObject wrapper). $pkCbDone = $false - do { & $pkGroupProvider | ForEach-Object { $pkProvided.Add($_) }; $pkCbDone = $true } while ($false) + $pkProvided = @(do { & $pkGroupProvider; $pkCbDone = $true } while ($false)) if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-GroupProvider' -PkContext $pkCallbackContext } - # What assignment would give: one value as itself, several as one - # array, which is the group (unchanged; it only gets a warning). + # The group is what assignment gives: one value as itself, several + # as one array (unchanged; it only gets a warning now). $pkGroupInfo = $null if ($pkProvided.Count -eq 1) { $pkGroupInfo = $pkProvided[0] } elseif ($pkProvided.Count -gt 1) { - $pkGroupInfo = $pkProvided.ToArray() + $pkGroupInfo = $pkProvided # Output ending in $null has its own warning, below. - if (-not $pkProviderWarned -and $null -ne $pkGroupInfo[-1]) { + if (-not $pkProviderWarned -and $null -ne $pkProvided[-1]) { $pkProviderWarned = $true Write-PkMessage ("ProcessDispatch: -GroupProvider output " + $pkProvided.Count + " values in one call, taken together as one group. Output one value per call: `$null = ... discards a value you don't want (an ArrayList.Add index), and return ,`$list returns a list as one group. Shown once per run.") -Level Warn }