Repository navigation
test(tasks): make the StartAndForget DoesNotBlockCaller tests deterministic - #68
Merged
Merged
Conversation
…nistic StartAndForget_WithTCS_DoesNotBlockCaller and StartAndForget_LongRunningTask_DoesNotBlockCaller asserted that a Task.Run lambda had started 50 ms after the call, and that the call returned within 100 ms. Both are wall-clock assumptions: when the thread pool is busy for more than 50 ms (parallel test classes on a loaded runner) the lambda has not run yet and the assertion fails with "Expected taskStarted to be True, but found False." The task now waits on a gate the test controls, and the test waits for a "started" signal instead of sleeping. StartAndForget can only return while the task is still running if it does not wait for the task, so the elapsed-time assertion is no longer needed. The call is bounded by a 10 s safety timeout, so an implementation that blocks the caller fails the test instead of hanging the run.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain in the synchronization changes to the two nonblocking tests.
No open issues 🎉
What changed in this PR
Makes the two StartAndForget nonblocking tests less sensitive to thread-pool scheduling by replacing timing assumptions with explicit coordination. Production library behavior is unchanged.
Changes:
- Replaces fixed delays and elapsed-time assertions with task-start signals and test-controlled completion gates.
- Adds a shared 10-second safety timeout to bound calls and waits.
- Releases each gate in a
finallyblock so failures do not leave tasks waiting on it.
| File | Description |
|---|---|
Plugin.BaseTypeExtensions.Tests/TaskExtensionsTests.cs |
Rewrites both nonblocking tests with explicit synchronization, bounded waits, and gate cleanup. |
Review effort: Balanced
framinosona
deleted the
framinosona/fix-flaky-startandforget-tcs-test
branch
November 22, 2026 20:45
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.
Summary
Rewrites two timing-based tests so they no longer depend on how fast the thread pool picks up work:
StartAndForget_WithTCS_DoesNotBlockCallerStartAndForget_LongRunningTask_DoesNotBlockCaller(same defect,Action<Exception>overload)The task now waits on a gate the test controls, the test waits for a "started" signal, and the call is bounded by a 10 s safety timeout. No library code changes.
Why
Both tests started a
Task.Run, calledStartAndForget, slept 50 ms and then asserted that the lambda had run (taskStarted.Should().BeTrue()), plus a< 100 mswall-clock assertion on the call. When the thread pool is busy for more than 50 ms, which happens on a loaded runner with test classes running in parallel, the lambda has not started yet and CI fails withExpected taskStarted to be True, but found False.The failure shows up as an occasional red run that passes on re-run.StartAndForgetcan only return while the task is still running if it does not wait for it, so holding the task open makes the elapsed-time assertion unnecessary.Change Type
Affected Areas
Behavior And Compatibility
Tests
How it was checked (net9.0 target, run on the .NET 10 runtime locally):
Expected taskStarted to be True, but found False.The two rewritten tests passed under the same starvation. A plain CPU-load loop (7 runs on 10 cores) did not reproduce it, so that is the only reproduction I have.StartAndForgettemporarily changed to block on the task (GetAwaiter().GetResult()), both tests fail withTimeoutExceptionafter 10 s instead of hanging. The change was reverted.Documentation
Notes
StartAndForget_WithTCS_MultipleOperations_EachCompletesIndependently(Task.Delay(200)). They have more headroom and were not touched here.ci.ymlonmain, which builds a package and waits on thenuget-orgapproval like any other push, even though only a test file changed.Checklist
type(scope): subject)