Repository navigation
Start each worker job in the caller's location, in a fresh scope - #23
Merged
Merged
Conversation
Worker runspaces started in the PowerShell process's working directory, not the caller's current location, so a relative path in a worker cmdlet (Get-Content .\data.txt) looked in the wrong folder. Runspaces are also reused from job to job, and the worker ran at the runspace's global scope. So a job that changed folder moved every later job on that runspace, and a variable a job set, $ErrorActionPreference included, carried over to the next one. A worker that set $ErrorActionPreference = 'Stop' made later jobs on that runspace crash on errors they would otherwise only have reported. Each job now moves to the caller's current location first (the real folder behind a New-PSDrive drive), then runs the worker in a scope of its own with the job's values. Both happen in one script: a Set-Location statement added ahead of the worker hangs the pipeline in a runspace pool, on 5.1 and 7. Error line numbers still match the worker's own code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
Invoke-PkRunspaceJobsworkers had three problems with where they ran and what they inherited:Get-Content .\data.txt, looked in the wrong folder.$ErrorActionPreference: a worker that set it to'Stop'made later jobs on the same runspace crash on errors they would otherwise only have reported.Reproduced on Windows PowerShell 5.1 and PowerShell 7.
What changed
Each job now does two things, in one script:
New-PSDrivedrive, and the same folderStart-PkProcessuses by default since Start programs in the current location, and read program paths as PowerShell does #22.Why one script. A
Set-Locationstatement added ahead of the worker (AddCommand(...).AddStatement()) left the pool runspace's pipeline incomplete: it still hadn't finished after 10 s on both 5.1 and 7. So the move and the worker are a single script, and the worker runs as& { <worker> } @values. The prefix shares the worker's first line, so error line numbers still match the worker's own code. I checked that againstmain.If the folder disappears mid-run, a job runs wherever its runspace already is, without reporting an error.
Docs: the README worker section and the
Invoke-PkRunspaceJobshelp describe this. The README also notes that relative paths in .NET calls still follow the process's working directory, as they do outside workers.Compatibility:
$MyInvocationnow describes a scriptblock call rather than a top-level script.Testing
$ErrorActionPreferenceset by one job aren't there in the next;New-PSDrivedrive, workers start in the real folder.main: all 4 fail (4 FAILED, 257 passed). The failures show jobs 2 and 3 starting inC:\WINDOWSand inheritingEAP=Stop.ALL PASSED (261 assertions)on 5.1 and 7.🤖 Generated with Claude Code