Skip to content

Fix WaitForHelixJobCompletion pre-expansion race (ExitCode -4 / Send to Helix 404) - #17130

Merged
mmitche merged 4 commits into
dotnet:mainfrom
mmitche:fix/waitforhelix-completion-race
Jul 16, 2026
Merged

mmitche merged 4 commits into
dotnet:mainfrom
mmitche:fix/waitforhelix-completion-race

Conversation

@mmitche

@mmitche mmitche commented Jul 16, 2026

Copy link
Copy Markdown
Member

Fixes #17129.

Problem

WaitForHelixJobCompletion waited 1 second after submitting a Helix job, then polled Job.PassFailAsync and treated the job as complete when pf.Working == 0 && pf.Total != 0. Immediately after submission the only work item that exists is the system item HelixController Work Queueing, which finishes almost instantly (exit 0). The real work items are not written until the HelixController asynchronously expands the job. Under expansion lag (queue/controller congestion) the poll saw Total=1, Working=0 and returned prematurely.

Downstream this completed the AzDO Test Run early; the real work items ran later, reported to a Completed test run, were rejected (TestCaseResults cannot be added or updated for a test run which is in Completed state), ended up ExitCode -4, and the "Send to Helix" download 404'd.

Fix

Mirror the robust completion detection the JobMonitor already uses (JobMonitorRunner.AreAllWorkItemsTerminalAsync + HelixService.ListWorkItemsAsync):

  • Complete when Job.DetailsAsync reports Finished != null, or when the real work items (excluding HelixController Work Queueing) number at least InitialWorkItemCount and all have terminal ExitCodes.
  • Add a 4-hour max-wait fallback that errors clearly if a job never expands and never reports a job-level error, so the task fails instead of hanging.
  • Preserve the existing job-level error handling and cancellation behavior.

DetailsAsync is now called every poll (it supplies Finished/InitialWorkItemCount), so the previous % 12-iteration throttle on the error check is no longer needed — error checking now happens every poll as well.

Tests

Adds WaitForHelixJobCompletionTests covering: system-item-only does not complete, completes once expected real work items are terminal, and job-level Finished short-circuits.

Notes

Each poll now makes two API calls (DetailsAsync + WorkItem.ListAsync) instead of one PassFailAsync, matching the JobMonitor pattern. This is a modest increase in API load, notably ListAsync on very large jobs.

WaitForHelixJobCompletion waited 1s after submission then polled
Job.PassFailAsync, treating the job complete when Working==0 && Total!=0.
Immediately after submission the only work item present is the system
item "HelixController Work Queueing" (finishes ~instantly), so under
HelixController expansion lag the wait returned prematurely. Downstream
this completed the AzDO Test Run early; real work items later reported to
a Completed run, were rejected, and ended up ExitCode -4, with the
Send-to-Helix download 404ing.

Mirror the robust completion detection the JobMonitor already uses:
- Complete when Job.DetailsAsync reports Finished != null, or when the
  real work items (excluding "HelixController Work Queueing") number at
  least InitialWorkItemCount and all have terminal ExitCodes.
- Add a 4-hour max-wait fallback that errors clearly if a job never
  expands and never reports a job-level error.
- Preserve job-level error handling and cancellation behavior.

Adds unit tests covering the system-item-only, all-expected-terminal,
and job-level-Finished cases.

Fixes dotnet#17129

Co-authored-by: Copilot <[email protected]>
Copilot-Session: be2120bf-5298-4ac7-aebd-7344585914df
Copilot AI review requested due to automatic review settings July 16, 2026 17:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a race in WaitForHelixJobCompletion where a Helix job could be treated as “complete” before the controller expands and publishes the real work items, leading to early AzDO test-run completion and downstream ExitCode -4 / “Send to Helix” 404 failures.

Changes:

  • Replaces the pre-expansion-prone PassFailAsync completion heuristic with job DetailsAsync + work-item enumeration, filtering out the system “HelixController Work Queueing” work item.
  • Adds a max-wait timeout path intended to fail clearly if a job never expands and never reports a job-level error.
  • Adds unit tests covering system-item-only behavior, terminal work-item completion, and job-level Finished short-circuiting.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Microsoft.DotNet.Helix/Sdk/WaitForHelixJobCompletion.cs Updates completion detection to avoid pre-expansion false positives by using JobDetails + real work-item terminal detection, and adds a timeout fallback.
src/Microsoft.DotNet.Helix/Sdk.Tests/Microsoft.DotNet.Helix.Sdk.Tests/WaitForHelixJobCompletionTests.cs Adds tests for the new “don’t complete on system item” and “complete when expected items are terminal / job finished” logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Compute the real work-item list before the completion check so the job-level Finished path also logs 'is completed with N finished work items' as before, instead of a bare 'is completed'.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: be2120bf-5298-4ac7-aebd-7344585914df
Copilot AI review requested due to automatic review settings July 16, 2026 17:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Remove the 4-hour MaxHelixJobCompletionWait fallback. The original task had no internal wall-clock cap and relied on the cancellation token (AzDO job/pipeline timeout or build cancellation) to break the wait loop; a fixed cap risks failing legitimately long-running Helix jobs. The InitialWorkItemCount + terminal-exit-code check fixes the premature-completion race, and the cancellation token continues to bound any expansion hang as before.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: be2120bf-5298-4ac7-aebd-7344585914df
Copilot AI review requested due to automatic review settings July 16, 2026 17:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

@mmitche
mmitche marked this pull request as ready for review July 16, 2026 17:48
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings July 16, 2026 17:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@mmitche
mmitche enabled auto-merge (squash) July 16, 2026 18:18
@akoeplinger

Copy link
Copy Markdown
Member

Add a 4-hour max-wait fallback that errors clearly if a job never expands and never reports a job-level error, so the task fails instead of hanging.

this isn't implemented in the code from what I can see (I actually think this is better, not sure if we have some stress tests that take longer)

@mmitche
mmitche merged commit f6ce67c into dotnet:main Jul 16, 2026
14 of 15 checks passed
@mmitche

mmitche commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

Add a 4-hour max-wait fallback that errors clearly if a job never expands and never reports a job-level error, so the task fails instead of hanging.

this isn't implemented in the code from what I can see (I actually think this is better, not sure if we have some stress tests that take longer)

That's because I yelled at it and told it not to get too uppity and implement new features ;)

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.

Arcade WaitForHelixJobCompletion race: job reported 'complete' pre-expansion -> Test Run Completed early -> work items ExitCode -4 + Send to Helix 404

3 participants