Skip to content
Closed
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
11 changes: 7 additions & 4 deletions src/System.Management.Automation/engine/hostifaces/PSTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ public sealed class PSTaskJob : Job
#region Members

private readonly PSTaskPool _taskPool;
private readonly List<PSTaskChildJob> _taskChildJobs;
private bool _isOpen;
private bool _stopSignaled;

Expand Down Expand Up @@ -1031,6 +1032,7 @@ internal PSTaskJob(
bool useNewRunspace) : base(command, string.Empty)
{
_taskPool = new PSTaskPool(throttleLimit, useNewRunspace);
_taskChildJobs = new List<PSTaskChildJob>();
_isOpen = true;
PSJobTypeName = nameof(PSTaskJob);

Expand All @@ -1056,7 +1058,7 @@ public override bool HasMoreData
{
get
{
foreach (var childJob in ChildJobs)
foreach (var childJob in _taskChildJobs)
{
if (childJob.HasMoreData)
{
Expand Down Expand Up @@ -1119,6 +1121,7 @@ internal bool AddJob(PSTaskChildJob childJob)
}

ChildJobs.Add(childJob);
_taskChildJobs.Add(childJob);
return true;
}

Expand All @@ -1137,9 +1140,9 @@ internal void Start()
System.Threading.ThreadPool.QueueUserWorkItem(
(_) =>
{
foreach (var childJob in ChildJobs)
foreach (var childJob in _taskChildJobs)
{
_taskPool.Add((PSTaskChildJob)childJob);
_taskPool.Add(childJob);
}

_taskPool.Close();
Expand All @@ -1162,7 +1165,7 @@ private void HandleTaskPoolComplete(object sender, EventArgs args)

// 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 _taskChildJobs)
{
if (childJob.JobStateInfo.State != JobState.Completed)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,35 @@ Describe 'ForEach-Object -Parallel -AsJob Basic Tests' -Tags 'CI' {
$job | Wait-Job | Remove-Job
}

It 'Does not crash when the ChildJobs collection is mutated while a parallel job starts' {
$testScript = Join-Path $TestDrive 'Mutate-ChildJobs.ps1'
$stdoutPath = Join-Path $TestDrive 'stdout.txt'
$stderrPath = Join-Path $TestDrive 'stderr.txt'
Set-Content -LiteralPath $testScript -Value @'
$job = 0..20 | ForEach-Object -AsJob -Parallel {
Start-Sleep -Milliseconds 300
$_
} -ThrottleLimit 2

Start-Sleep -Milliseconds 100
$job.ChildJobs.RemoveAt(15)

$job | Wait-Job -Timeout 30 | Out-Null
$job | Remove-Job -Force
'@

$powershell = Join-Path -Path $PSHOME -ChildPath "pwsh"
$process = Start-Process -FilePath $powershell -ArgumentList @(
'-NoLogo'
'-NoProfile'
'-NonInteractive'
'-File'
$testScript
) -RedirectStandardOutput $stdoutPath -RedirectStandardError $stderrPath -Wait -PassThru

$process.ExitCode | Should -Be 0
}

It 'Verifies dollar underbar variable' {

$expected = 1..10
Expand Down