Repository navigation
Keep kit variables out of worker callbacks; find drives made in a function - #28
Merged
Merged
Conversation
…ction Loading the kit as a module fixed closure-less callbacks on the main thread, but inside a worker runspace the kit's functions are plain copies, not a module. So a plain -OnComplete in a worker could still reach the batch's own $results or $state, and read its $TimeoutSeconds. Batch, Dispatch, DispatchCore and the group-building helper now use only pk-prefixed variables: locals are renamed, and parameters are copied and removed. Public parameter names don't change. A callback there now sees the worker's variables, so .GetNewClosure() isn't needed for that anywhere. A drive made with New-PSDrive inside a function stopped working when the kit became a module, since a module can't see it. When the module's own lookup fails, the path is now resolved again in the caller's session. Start-PkProcess became a thin wrapper over an internal function that takes that session, and the entry points pass theirs down. This adds nothing to paths the module can already resolve. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Two gaps were left after the kit became an in-memory module in #27:
-OnCompletegiven toInvoke-PkProcessBatchin a worker could still add to the batch's own$results, write to its$state, and read its$TimeoutSeconds, with silently wrong results. Dispatcher callbacks had the same problem, for example reading the kit's$running.New-PSDriveinside a function. It can only be seen from that function, so the module couldn't resolve aWorkingDirectoryorFilePathon it.Generated lookup script (added in #27) also quoted names and paths by doubling ASCII single quotes only. PowerShell also treats curly quotes as string delimiters, so a curly quote could end the literal early. A new check showed a path running as code in the caller's scope.
What changed
pk-prefixed variables inInvoke-PkProcessBatch,Invoke-PkProcessDispatch,Invoke-PkProcessDispatchCoreand the group-building helper. Locals are renamed, and parameters are copied topknames and removed. Public parameter names don't change. The diff is large, but the rename itself was done mechanically from the syntax tree.Start-PkProcessis now a thin wrapper over an internal function that takes the caller's session, andInvoke-PkProcessWithTimeout, the dispatcher's default starter and the batch pass theirs. Paths the module can already resolve take the same route as before.CodeGeneration.EscapeSingleQuotedStringContent, in the function lookup and the path lookup..GetNewClosure()inside a worker, and the-Scope Scriptadvice for drives is gone.Still true: a closure-less callback in a worker can see a kit function's automatic variables, such as
$PSBoundParameters, and a variable of the worker's own named with thepkprefix could collide.Testing
$results,$state,$TimeoutSecondsand$running;WorkingDirectoryon a function-local drive works fromInvoke-PkProcessWithTimeout,Start-PkProcess, the batch, the batch underInvoke-PkRunand the dispatcher;FilePathon that drive works;main's kit (5.1), the worker and drive checks fail (5 FAILED, 1 passed, run before the curly-quote check existed). The nonexistent-drive check passes on both, as a guard. The curly-quote check failed before its fix on this branch.ALL PASSED (288 assertions)on Windows PowerShell 5.1 and PowerShell 7.🤖 Generated with Claude Code