Skip to content

Avoid mutable ChildJobs enumeration in PSTaskJob#27510

Closed
KirtiRamchandani wants to merge 1 commit into
PowerShell:masterfrom
KirtiRamchandani:codex/fix-pstaskjob-childjobs-crash
Closed

Avoid mutable ChildJobs enumeration in PSTaskJob#27510
KirtiRamchandani wants to merge 1 commit into
PowerShell:masterfrom
KirtiRamchandani:codex/fix-pstaskjob-childjobs-crash

Conversation

@KirtiRamchandani
Copy link
Copy Markdown

PR Summary

Avoids an unhandled process-terminating exception when PSTaskJob.ChildJobs is mutated while a ForEach-Object -Parallel -AsJob job is starting.

PR Context

Fix #26160.

PSTaskJob.Start() currently enumerates the public ChildJobs list 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 keeps an internal list of PSTaskChildJob objects for scheduler/state enumeration while preserving the existing public ChildJobs list for compatibility.

PR Checklist

  • PR has a meaningful title
  • Summarized changes
  • This PR is focused on one issue
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge and is not work-in-progress
  • Breaking change: No
  • User-facing change: Not required
  • Tests added/updated

Validation

  • Start-PSBuild -Clean -PSModuleRestore -UseNuGetOrg built pwsh.exe. The Windows PowerShell 5.1 build wrapper emitted post-build ConvertFrom-Json -Depth compatibility warnings after build output was produced.
  • Start-PSPester -Path test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 -UseNuGetOrg -ThrowOnFailure -Terse -SkipTestToolBuild passed: 62 passed, 0 failed.

@KirtiRamchandani
Copy link
Copy Markdown
Author

Closing in favor of replacement draft PR from neutral branch: #27512

@KirtiRamchandani KirtiRamchandani deleted the codex/fix-pstaskjob-childjobs-crash branch May 24, 2026 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mutating PSTaskJob.ChildJobs from ForEach-Object -Parallel -AsJob throws unhandled exception and crashes PowerShell

1 participant