Add per-test execution cancellation - #6569
Merged
Merged
Conversation
Greptile SummaryThe PR adds cooperative, per-test execution cancellation and integrates it with retry transitions, timeout tokens, custom executors, documentation, and public API snapshots.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| src/TUnit.Core/TestContext.Execution.cs | Adds the public cancellation implementation, synchronization, retry preservation, and deferred source disposal; the previously reported races are addressed at current HEAD. |
| src/TUnit.Engine/TestExecutor.cs | Routes test execution and timeout handling through the per-test token and preserves cancellation during retryable attempt transitions. |
| src/TUnit.Engine/Services/TestExecution/RetryHelper.cs | Keeps cancellation active through retry decisions and backoff and preserves cancelled results when retry callbacks throw. |
| tests/TUnit.Engine.Tests/TestExecutionCancellationTests.cs | Adds end-to-end assertions for normal cancellation, late calls, retry backoff, retry transitions, and retry callback exceptions. |
| tests/TUnit.UnitTests/TestContextCancellationTests.cs | Adds focused synchronization tests covering deferred disposal, source replacement, and cancellation between attempts. |
| docs/docs/execution/cancellation.md | Documents cooperative test-scoped cancellation, callback usage, lifecycle behavior, and composition with existing timeout and token APIs. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Test attempt begins] --> B[Initialize per-attempt cancellation source]
B --> C[Test body / custom executor]
C --> D{Cancel requested?}
D -->|Yes| E[Signal injected and execution tokens]
D -->|No| F{Attempt failed and retryable?}
E --> G[Record Cancelled]
F -->|Yes| H[Keep cancellation open through retry decision and backoff]
H --> I[Initialize next attempt while preserving request]
I --> C
F -->|No| J[Complete cancellation acceptance]
J --> K[Finalize result and dispose sources]
G --> K
Reviews (8): Last reviewed commit: "fix(engine): close retry cancellation ga..." | Re-trigger Greptile
thomhurst
force-pushed
the
agent/test-execution-cancel
branch
from
August 8, 2026 18:52
4152d4c to
a134df5
Compare
This was referenced Aug 9, 2026
Closed
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
TestContext.Execution.Cancel()for cooperative, test-scoped cancellationCancelledeven when the body or a custom executor consumes the cancellation exceptionPerformance
A .NET 10 microprobe measured source creation and disposal at approximately:
There is no background task, polling,
TaskCompletionSource, orTask.WhenAnyoverhead.Validation
dotnet build TUnit.Dev.slnx --no-restore -graphBuild:Truenet472,net8.0,net9.0, andnet10.0yarn typecheckremains blocked by the existing TypeScript 7baseUrlincompatibility indocs/tsconfig.json; the Docusaurus production build succeeds.Stack
Based on #6568 because its linked cancellation ownership changes are the prerequisite for composing this API with
AddLinkedCancellationTokenand[Timeout]. Retarget tomainafter #6568 merges.