diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index b9eea4e..86def59 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -801,6 +801,18 @@ Set-PkMessageHandler -Handler { param($Message, $Level) "noise"; 42 } $leaked = Write-PkMessage "hello" Test-Assert ($null -eq $leaked) "a message handler's output doesn't leak out of Write-PkMessage" Set-PkMessageHandler -Handler { param($Message, $Level) $messageSink.Add(@{ Message = $Message; Level = $Level }) }.GetNewClosure() + +# Set in a worker, the handler would be the worker's scriptblock, run by the main thread. +$handlerCrash = [System.Collections.Generic.List[string]]::new() +$handlerBefore = [PkState]::MessageHandler +$handlerRun = Invoke-PkRunspaceJobs -MaxParallel 1 -JobParams (New-JobList -Jobs @(@{ Reset = $false }, @{ Reset = $true })) -SharedFunctionNames Set-PkMessageHandler -JobCrashedHandler { param($e) $handlerCrash.Add([string]$e.Exception.InnerException.Message) }.GetNewClosure() -WorkerScript { + param($Reset) + if ($Reset) { Set-PkMessageHandler $null } else { Set-PkMessageHandler { param($Message, $Level) } } +} +$handlerKept = [object]::ReferenceEquals($handlerBefore, [PkState]::MessageHandler) +[PkState]::MessageHandler = $handlerBefore +Test-Assert ($handlerRun.CrashedCount -eq 2 -and @($handlerCrash | Where-Object { $_ -like '*Set-PkMessageHandler*main thread*' }).Count -eq 2) ("Set-PkMessageHandler throws in a worker, for a handler or `$null (" + ($handlerCrash -join '; ') + ")") +Test-Assert $handlerKept "and leaves the handler as it was" Write-Host "" } diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index d3a1d09..d618f85 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1098,12 +1098,18 @@ function Set-PkMessageHandler { # always runs on the main thread, including for messages from worker code. If it # throws, the message is shown on the console instead and the run carries on. # Leave it early with return: continue and break end it too, but warn once. + # Throws in worker code; call it on the main thread. # .PARAMETER Handler # { param($Message, $Level) ... }, or $null to go back to the default (console). # .EXAMPLE # Set-PkMessageHandler { param($Message, $Level) Add-Content run.log "$Level $Message" } [CmdletBinding()] param([scriptblock]$Handler) + # A worker's scriptblock belongs to its runspace: the main thread running + # it can hang or crash. + if ($null -ne $global:PkWorkerQueue) { + throw "Set-PkMessageHandler: this is worker code, and the handler can only be set on the main thread, which runs it. Set it before starting the workers; Write-PkMessage in a worker already goes to it." + } [PkState]::MessageHandler = $Handler } diff --git a/README.md b/README.md index e7d2f61..23d0df5 100644 --- a/README.md +++ b/README.md @@ -578,7 +578,7 @@ Workers can't safely write to the console. `Write-PkMessage "text" -Level Info|W Set-PkMessageHandler { param($Message, $Level) Write-MyLog $Message $Level } ``` -The handler always runs on the main thread, so it can use anything. The kit's own notices (abort announcements, crashed workers, force-stops) go through the same handler. If your handler throws, the message is shown on the console instead and the run carries on. `Set-PkMessageHandler $null` restores the default. +The handler always runs on the main thread, so it can use anything. The kit's own notices (abort announcements, crashed workers, force-stops) go through the same handler. If your handler throws, the message is shown on the console instead and the run carries on. `Set-PkMessageHandler $null` restores the default. Call it on the main thread: in worker code it throws. ## Abort