Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

Expand Down
6 changes: 6 additions & 0 deletions ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading