Skip to content

Add per-test execution cancellation - #6569

Merged
thomhurst merged 7 commits into
mainfrom
agent/test-execution-cancel
Aug 8, 2026
Merged

Add per-test execution cancellation#6569
thomhurst merged 7 commits into
mainfrom
agent/test-execution-cancel

Conversation

@thomhurst

Copy link
Copy Markdown
Owner

Summary

  • add TestContext.Execution.Cancel() for cooperative, test-scoped cancellation
  • create one lightweight cancellation source per executed attempt and propagate it through injected tokens, timeouts, and custom test executors
  • mark the execution Cancelled even when the body or a custom executor consumes the cancellation exception
  • ignore late cancellation requests after execution completes
  • document usage and update public API snapshots

Performance

A .NET 10 microprobe measured source creation and disposal at approximately:

  • 48 B and 11 ns with a non-cancellable parent token
  • 64 B and 47 ns with a cancellable parent token

There is no background task, polling, TaskCompletionSource, or Task.WhenAny overhead.

Validation

  • dotnet build TUnit.Dev.slnx --no-restore -graphBuild:True
  • focused cancellation/custom executor/timeout integration tests
  • existing linked cancellation regression tests
  • public API verification on net472, net8.0, net9.0, and net10.0
  • documentation production build

yarn typecheck remains blocked by the existing TypeScript 7 baseUrl incompatibility in docs/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 AddLinkedCancellationToken and [Timeout]. Retarget to main after #6568 merges.

@greptile-apps

greptile-apps Bot commented Aug 8, 2026

Copy link
Copy Markdown

Greptile Summary

The PR adds cooperative, per-test execution cancellation and integrates it with retry transitions, timeout tokens, custom executors, documentation, and public API snapshots.

  • Adds ITestExecution.Cancel() and per-attempt cancellation-source lifecycle management.
  • Preserves accepted cancellation across retries and converts consumed cancellation into a cancelled terminal result.
  • Adds integration and unit coverage for cancellation, retry backoff, transition races, and late calls.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

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
Loading

Reviews (8): Last reviewed commit: "fix(engine): close retry cancellation ga..." | Re-trigger Greptile

Comment thread src/TUnit.Core/TestContext.Execution.cs Outdated
Comment thread src/TUnit.Core/TestContext.Execution.cs
Comment thread src/TUnit.Core/TestContext.Execution.cs
Base automatically changed from fix/6567-linked-cancellation to main August 8, 2026 18:48
Comment thread src/TUnit.Core/TestContext.Execution.cs
Comment thread src/TUnit.Engine/TestExecutor.cs Outdated
Comment thread src/TUnit.Engine/Services/TestExecution/RetryHelper.cs
@thomhurst
thomhurst enabled auto-merge (squash) August 8, 2026 20:05
@thomhurst
thomhurst merged commit fda0500 into main Aug 8, 2026
14 of 15 checks passed
@thomhurst
thomhurst deleted the agent/test-execution-cancel branch August 8, 2026 20:11
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.

1 participant