Skip to content

[Bug]: OperationCancelledException body swallowed when [Timeout] hit #6688

Closed

Description

@afscrome

Description

If at test provides a custom OperationCancelledException on a timeout, TUnit swallows that exception and replaces it with it's own timeout message.

For example, Aspire provides some more details on resource state in its timeout exceptions which can be really helpful in diagnosing why a wait fails. But TUnit swallows these.

Expected Behavior

The message from the thrown OperationCancelled exception

Minimal repo:

Failed due to XYZ

More realistic scenario

Resource 'job' failed to reach one of the target states: [Finished] before the operation was cancelled.
- Current State: Starting
- Creation Time: 29/08/2026 19:20:27
- Current Health: (null)

Actual Behavior

[Timeout] Test timed out after 1000ms

[Timeout] Test timed out after 15000ms

Steps to Reproduce

Minimal Example:

    [Test]
    [Timeout(50)]
    public async Task TimeOut(CancellationToken cancellationToken)
    {
        try
        {
            await Task.Delay(Timeout.Infinite, cancellationToken);
        } 
        catch(OperationCanceledException ex)
        {
            throw new OperationCanceledException("Failed due to XYZ", ex.CancellationToken);
        }
    }

More realistic scenario

    [Test]
    [Timeout(15_000)]
    public async Task ResourceNeverExits(CancellationToken cancellationToken)
    {
        var appHost = DistributedApplicationTestingBuilderFactory.Create();

        var job = appHost.AddContainer("job", "alpine")
            .WithEntrypoint("sh")
            .WithArgs("-c", "sleep 3600");

        await using var app = await appHost.BuildAsync(cancellationToken);
        await app.StartAsync(cancellationToken);

        await app.ResourceNotifications.WaitForResourceAsync(job.Resource.Name, KnownResourceStates.Finished, cancellationToken);
    }

TUnit Version

1.65.68

.NET Version

Microsoft.NETCore.App 10.0.9

Operating System

macOS

IDE / Test Runner

VS Code

Error Output / Stack Trace

Running tests...

❌ FAILED: TimeOut (1016ms)
   [Timeout] Test timed out after 1000ms

========================================
Test Run Summary
========================================
Total Tests: 1
Passed: 0
Failed: 1
Skipped: 0
❌ Result: FAILED
Test Execution Time: 1.51s (1514ms)
Total Time (including build): 3.19s (3191ms)
========================================

Additional Context

This is particularly troublesome for Aspire's ResourceNotifications.WaitXYZ. On cancellation, Aspire adds additional information to help troubleshoot what state thew wait go to before cancellation. These errors get through if you use your own cancellation token, but get swallowed if you hit TUnit's timeout.

(See microsoft/aspire#13729 for more details on Aspire's implementation)

Resource 'nginx' failed to become healthy before the operation was cancelled.
- Current State: Starting
- Creation Time: 29/08/2026 18:54:54
- Current Health: (null)
- Health Reports:
  - nginx_http_/does-not-exist_200_check: Unknown
at Aspire.Hosting.ApplicationModel.ResourceNotificationService.WaitForResourceCoreAsync(String resourceName, Func`2 predicate, String cancellationMessage, CancellationToken cancellationToken, String waitCondition)
at Aspire.Hosting.ApplicationModel.ResourceNotificationService.WaitForResourceHealthyAsync(String resourceName, WaitBehavior waitBehavior, CancellationToken cancellationToken)
at Aspire.Hosting.ApplicationModel.ResourceNotificationService.WaitForResourceHealthyAsync(String resourceName, CancellationToken cancellationToken)
at AlexCrome.Aspire.Hosting.Testing.IntegrationTests.Failures.WaitForResourceHealthyFailures.ResourceStartsButHealthCheckNeverPasses(CancellationToken cancellationToken) (file:///Users/alex/Developer/aspire-ci-testing/tests/integration/Failures/WaitForResourceHealthyFailures.cs#L57,0)
at AlexCrome.Aspire.Hosting.Testing.IntegrationTests.Failures.WaitForResourceHealthyFailures.ResourceStartsButHealthCheckNeverPasses(CancellationToken cancellationToken) (file:///Users/alex/Developer/aspire-ci-testing/tests/integration/Failures/WaitForResourceHealthyFailures.cs#L57,0)
at TUnit.Core.ExecutableTest`1.InvokeTestAsync(Object instance, CancellationToken cancellationToken)
at TUnit.Engine.TestExecutor.ExecuteTestAsync(AbstractExecutableTest executableTest, CancellationToken cancellationToken)
at TUnit.Engine.Helpers.TimeoutHelper.ExecuteWithTimeoutAsync(Func`2 taskFactory, TimeSpan timeout, CancellationTokenSource timeoutCts, CancellationToken externalToken, String timeoutMessage)
at TUnit.Engine.TestExecutor.ExecuteAsync(AbstractExecutableTest executableTest, TestInitializer testInitializer, CancellationToken cancellationToken, Nullable`1 testTimeout)
at TUnit.Engine.TestExecutor.ExecuteAsync(AbstractExecutableTest executableTest, TestInitializer testInitializer, CancellationToken cancellationToken, Nullable`1 testTimeout)
at TUnit.Engine.Services.TestExecution.TestCoordinator.ExecuteTestAsync(AbstractExecutableTest test, CancellationToken cancellationToken)
at TUnit.Engine.Services.TestExecution.TestCoordinator.ExecuteTestAsync(AbstractExecutableTest test, CancellationToken cancellationToken)

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE

Activity

  1. added
    bug
    on Aug 29, 2026
  2. added a commit that references this issue on Sep 4, 2026
    1bc2b6d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions