Avoid mutable ChildJobs enumeration in PSTaskJob#27512
Open
KirtiRamchandani wants to merge 2 commits into
Open
Avoid mutable ChildJobs enumeration in PSTaskJob#27512KirtiRamchandani wants to merge 2 commits into
KirtiRamchandani wants to merge 2 commits into
Conversation
8 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds regression coverage and internal changes to prevent crashes when PSTaskJob.ChildJobs is mutated while ForEach-Object -Parallel -AsJob is starting.
Changes:
- Added a Pester test that reproduces a crash by mutating
ChildJobsduring job startup. - Introduced an internal
_taskChildJobslist and switched several enumerations fromChildJobsto_taskChildJobsto avoid iteration over a mutating collection.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 | Adds a regression test that mutates ChildJobs during parallel job startup and asserts the process does not crash. |
| src/System.Management.Automation/engine/hostifaces/PSTask.cs | Adds an internal child-job list and uses it for enumeration to reduce crash risk from ChildJobs mutations. |
Comments suppressed due to low confidence (1)
src/System.Management.Automation/engine/hostifaces/PSTask.cs:1067
- Switching
HasMoreDataand completion logic to_taskChildJobscan makePSTaskJobobservably inconsistent when consumers mutateChildJobs(which this PR’s test explicitly does). Removals fromChildJobsare not reflected in_taskChildJobs, soHasMoreData/ final-state evaluation may continue to consider jobs that the consumer removed (and may no longer expect to affect the parent job). To keep behavior consistent, consider iterating over a snapshot of the authoritativeChildJobscollection (e.g.,ChildJobs.ToArray()) rather than a separate list, or ensure_taskChildJobsis kept in sync withChildJobsmutations (including removals).
foreach (var childJob in _taskChildJobs)
{
if (childJob.HasMoreData)
{
return true;
}
}
Comment on lines
+1000
to
+1001
| private readonly PSTaskPool _taskPool; | ||
| private readonly List<PSTaskChildJob> _taskChildJobs; |
Comment on lines
+1034
to
+1035
| _taskPool = new PSTaskPool(throttleLimit, useNewRunspace); | ||
| _taskChildJobs = new List<PSTaskChildJob>(); |
Comment on lines
+1123
to
+1124
| ChildJobs.Add(childJob); | ||
| _taskChildJobs.Add(childJob); |
Comment on lines
1140
to
1146
| System.Threading.ThreadPool.QueueUserWorkItem( | ||
| (_) => | ||
| { | ||
| foreach (var childJob in ChildJobs) | ||
| foreach (var childJob in _taskChildJobs) | ||
| { | ||
| _taskPool.Add((PSTaskChildJob)childJob); | ||
| _taskPool.Add(childJob); | ||
| } |
Comment on lines
1168
to
1171
| foreach (var childJob in _taskChildJobs) | ||
| { | ||
| if (childJob.JobStateInfo.State != JobState.Completed) | ||
| { |
| $_ | ||
| } -ThrottleLimit 2 | ||
|
|
||
| Start-Sleep -Milliseconds 100 |
Author
|
@microsoft-github-policy-service agree |
| $job | Remove-Job -Force | ||
| '@ | ||
|
|
||
| $powershell = Join-Path -Path $PSHOME -ChildPath "pwsh" |
| '@ | ||
|
|
||
| $powershell = Join-Path -Path $PSHOME -ChildPath "pwsh" | ||
| $process = Start-Process -FilePath $powershell -ArgumentList @( |
Comment on lines
+522
to
+523
| ) -RedirectStandardOutput $stdoutPath -RedirectStandardError $stderrPath -Wait -PassThru | ||
|
|
Comment on lines
+1060
to
1062
| foreach (var childJob in ChildJobs.ToArray()) | ||
| { | ||
| if (childJob.HasMoreData) |
Comment on lines
+1060
to
1061
| foreach (var childJob in ChildJobs.ToArray()) | ||
| { |
| (_) => | ||
| { | ||
| foreach (var childJob in ChildJobs) | ||
| foreach (PSTaskChildJob childJob in ChildJobs.ToArray()) |
| // Final state will be 'Complete', only if all child jobs completed successfully. | ||
| JobState finalState = JobState.Completed; | ||
| foreach (var childJob in ChildJobs) | ||
| foreach (var childJob in ChildJobs.ToArray()) |
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.
PR Summary
Avoids an unhandled process-terminating exception when
PSTaskJob.ChildJobsis mutated while aForEach-Object -Parallel -AsJobjob is starting.PR Context
Fix #26160.
PSTaskJob.Start()currently enumerates the publicChildJobslist on a ThreadPool callback. That list is externally mutable, so removing an item can invalidate the enumerator and let the exception escape the ThreadPool callback. This change snapshots the authoritativeChildJobslist at the internal enumeration points, so external mutations do not invalidate active enumerators and the public collection remains the source of truth.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerValidation
Start-PSBuild -PSModuleRestore -UseNuGetOrgbuiltpwsh.exe. The Windows PowerShell 5.1 build wrapper emitted post-buildConvertFrom-Json -Depthcompatibility warnings after build output was produced.Start-PSPester -Path test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 -UseNuGetOrg -ThrowOnFailure -Terse -SkipTestToolBuildpassed: 62 passed, 0 failed.