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
github-actions Bot pushed a commit to BenjaminMichaelis/TrxLib that referenced this pull request Aug 10, 2026
Updated [TUnit](https://github.com/thomhurst/TUnit) from 1.63.0 to
1.64.6.

<details>
<summary>Release notes</summary>

_Sourced from [TUnit's
releases](https://github.com/thomhurst/TUnit/releases)._

## 1.64.6

<!-- Release notes generated using configuration in .github/release.yml
at v1.64.6 -->

## What's Changed
### Other Changes
* Switch NuGet publishing to trusted publishing by @​thomhurst in
thomhurst/TUnit#6573
* Expose results directory through TestContext by @​thomhurst in
thomhurst/TUnit#6575
* Fix outcome timeline overflow in HTML reports by @​thomhurst in
thomhurst/TUnit#6574
### Dependencies
* chore(deps): update tunit to 1.64.0 by @​thomhurst in
thomhurst/TUnit#6570


**Full Changelog**:
thomhurst/TUnit@v1.64.0...v1.64.6

## 1.64.0

<!-- Release notes generated using configuration in .github/release.yml
at v1.64.0 -->

## What's Changed
### Other Changes
* Honor cancellation tokens linked by custom test executors by
@​thomhurst in thomhurst/TUnit#6565
* Fix linked cancellation from before-test hooks by @​thomhurst in
thomhurst/TUnit#6568
* Add per-test execution cancellation by @​thomhurst in
thomhurst/TUnit#6569
### Dependencies
* chore(deps): update tunit to 1.63.25 by @​thomhurst in
thomhurst/TUnit#6559
* chore(deps): update dependency serialize-javascript to v7.1.0 by
@​thomhurst in thomhurst/TUnit#6561
* chore(deps): update dependency nsubstitute to 6.1.0 by @​thomhurst in
thomhurst/TUnit#6564
* chore(deps): bump mermaid from 11.15.0 to 11.16.1 in /docs by
@​dependabot[bot] in thomhurst/TUnit#6563
* chore(deps): bump nanoid from 3.3.16 to 3.3.18 in /docs by
@​dependabot[bot] in thomhurst/TUnit#6562


**Full Changelog**:
thomhurst/TUnit@v1.63.25...v1.64.0

## 1.63.25

<!-- Release notes generated using configuration in .github/release.yml
at v1.63.25 -->

## What's Changed
### Other Changes
* Fix Mermaid label contrast in dark mode by @​thomhurst in
thomhurst/TUnit#6531
* Enhance matrix tests documentation by @​koryphaee in
thomhurst/TUnit#6537
* fix: support nested Member after IsTypeOf by @​mvanhorn in
thomhurst/TUnit#6540
* docs: Add documentation for TestContext.Parameters by @​thomhurst with
@​Copilot in thomhurst/TUnit#6558
* fix: run event receivers on injected properties by @​thomhurst in
thomhurst/TUnit#6556
### Dependencies
* chore(deps): update tunit to 1.63.0 by @​thomhurst in
thomhurst/TUnit#6526
* chore(deps): update dependency stackexchange.redis to 3.1.0 by
@​thomhurst in thomhurst/TUnit#6529
* chore(deps): update verify to 31.28.0 by @​thomhurst in
thomhurst/TUnit#6532
* chore(deps): update dependency stackexchange.redis to 3.1.3 by
@​thomhurst in thomhurst/TUnit#6541
* chore(deps): update dependency dompurify to v3.4.13 by @​thomhurst in
thomhurst/TUnit#6542
* chore(deps): update dependency rabbitmq.client to 7.2.2 by @​thomhurst
in thomhurst/TUnit#6546
* chore(deps): update dependency polyfill to 11.0.2 by @​thomhurst in
thomhurst/TUnit#6549
* chore(deps): update dependency polyfill to 11.0.2 by @​thomhurst in
thomhurst/TUnit#6548
* chore(deps): bump fast-uri from 3.1.4 to 3.1.5 in /docs by
@​dependabot[bot] in thomhurst/TUnit#6545
* chore(deps): bump postcss from 8.5.22 to 8.5.25 in /docs by
@​dependabot[bot] in thomhurst/TUnit#6547
* chore(deps): update dependency stackexchange.redis to 3.1.11 by
@​thomhurst in thomhurst/TUnit#6550
* chore(deps): update dependency stackexchange.redis to 3.1.13 by
@​thomhurst in thomhurst/TUnit#6552

## New Contributors
* @​koryphaee made their first contribution in
thomhurst/TUnit#6537

**Full Changelog**:
thomhurst/TUnit@v1.63.0...v1.63.25

Commits viewable in [compare
view](thomhurst/TUnit@v1.63.0...v1.64.6).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=TUnit&package-manager=nuget&previous-version=1.63.0&new-version=1.64.6)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
github-actions Bot pushed a commit to IntelliTect/CodingGuidelines that referenced this pull request Aug 10, 2026
Updated [TUnit.Core](https://github.com/thomhurst/TUnit) from 1.63.0 to
1.64.6.

<details>
<summary>Release notes</summary>

_Sourced from [TUnit.Core's
releases](https://github.com/thomhurst/TUnit/releases)._

## 1.64.6

<!-- Release notes generated using configuration in .github/release.yml
at v1.64.6 -->

## What's Changed
### Other Changes
* Switch NuGet publishing to trusted publishing by @​thomhurst in
thomhurst/TUnit#6573
* Expose results directory through TestContext by @​thomhurst in
thomhurst/TUnit#6575
* Fix outcome timeline overflow in HTML reports by @​thomhurst in
thomhurst/TUnit#6574
### Dependencies
* chore(deps): update tunit to 1.64.0 by @​thomhurst in
thomhurst/TUnit#6570


**Full Changelog**:
thomhurst/TUnit@v1.64.0...v1.64.6

## 1.64.0

<!-- Release notes generated using configuration in .github/release.yml
at v1.64.0 -->

## What's Changed
### Other Changes
* Honor cancellation tokens linked by custom test executors by
@​thomhurst in thomhurst/TUnit#6565
* Fix linked cancellation from before-test hooks by @​thomhurst in
thomhurst/TUnit#6568
* Add per-test execution cancellation by @​thomhurst in
thomhurst/TUnit#6569
### Dependencies
* chore(deps): update tunit to 1.63.25 by @​thomhurst in
thomhurst/TUnit#6559
* chore(deps): update dependency serialize-javascript to v7.1.0 by
@​thomhurst in thomhurst/TUnit#6561
* chore(deps): update dependency nsubstitute to 6.1.0 by @​thomhurst in
thomhurst/TUnit#6564
* chore(deps): bump mermaid from 11.15.0 to 11.16.1 in /docs by
@​dependabot[bot] in thomhurst/TUnit#6563
* chore(deps): bump nanoid from 3.3.16 to 3.3.18 in /docs by
@​dependabot[bot] in thomhurst/TUnit#6562


**Full Changelog**:
thomhurst/TUnit@v1.63.25...v1.64.0

## 1.63.25

<!-- Release notes generated using configuration in .github/release.yml
at v1.63.25 -->

## What's Changed
### Other Changes
* Fix Mermaid label contrast in dark mode by @​thomhurst in
thomhurst/TUnit#6531
* Enhance matrix tests documentation by @​koryphaee in
thomhurst/TUnit#6537
* fix: support nested Member after IsTypeOf by @​mvanhorn in
thomhurst/TUnit#6540
* docs: Add documentation for TestContext.Parameters by @​thomhurst with
@​Copilot in thomhurst/TUnit#6558
* fix: run event receivers on injected properties by @​thomhurst in
thomhurst/TUnit#6556
### Dependencies
* chore(deps): update tunit to 1.63.0 by @​thomhurst in
thomhurst/TUnit#6526
* chore(deps): update dependency stackexchange.redis to 3.1.0 by
@​thomhurst in thomhurst/TUnit#6529
* chore(deps): update verify to 31.28.0 by @​thomhurst in
thomhurst/TUnit#6532
* chore(deps): update dependency stackexchange.redis to 3.1.3 by
@​thomhurst in thomhurst/TUnit#6541
* chore(deps): update dependency dompurify to v3.4.13 by @​thomhurst in
thomhurst/TUnit#6542
* chore(deps): update dependency rabbitmq.client to 7.2.2 by @​thomhurst
in thomhurst/TUnit#6546
* chore(deps): update dependency polyfill to 11.0.2 by @​thomhurst in
thomhurst/TUnit#6549
* chore(deps): update dependency polyfill to 11.0.2 by @​thomhurst in
thomhurst/TUnit#6548
* chore(deps): bump fast-uri from 3.1.4 to 3.1.5 in /docs by
@​dependabot[bot] in thomhurst/TUnit#6545
* chore(deps): bump postcss from 8.5.22 to 8.5.25 in /docs by
@​dependabot[bot] in thomhurst/TUnit#6547
* chore(deps): update dependency stackexchange.redis to 3.1.11 by
@​thomhurst in thomhurst/TUnit#6550
* chore(deps): update dependency stackexchange.redis to 3.1.13 by
@​thomhurst in thomhurst/TUnit#6552

## New Contributors
* @​koryphaee made their first contribution in
thomhurst/TUnit#6537

**Full Changelog**:
thomhurst/TUnit@v1.63.0...v1.63.25

Commits viewable in [compare
view](thomhurst/TUnit@v1.63.0...v1.64.6).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=TUnit.Core&package-manager=nuget&previous-version=1.63.0&new-version=1.64.6)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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