Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions TUnit.Engine/TestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ await TimeoutHelper.ExecuteWithTimeoutAsync(
TUnitActivitySource.StopActivity(testBodyActivity);
#endif
executableTest.Context.Execution.TestEnd ??= DateTimeOffset.UtcNow;

// The timeout path (TimeoutHelper) set Context.CancellationToken to a linked CTS
// token that is disposed the moment the test body returns. Restore the still-valid
// outer token — colocated here with the timeout call that mutated it — so the
// test-end event receivers, After(Test)/AfterEvery(Test) hooks, and any retry
// back-off never observe a disposed CancellationTokenSource (fixes #6339).
if (testTimeout.HasValue)
{
executableTest.Context.CancellationToken = cancellationToken;
}
}

executableTest.SetResult(TestState.Passed);
Expand Down
37 changes: 37 additions & 0 deletions TUnit.TestProject/Bugs/_6339/TimeoutAfterHookTokenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using TUnit.TestProject.Attributes;

namespace TUnit.TestProject.Bugs._6339;

/// <summary>
/// Reproduction for issue #6339: "After(Test) hook failed: The CancellationTokenSource has been disposed."
/// https://github.com/thomhurst/TUnit/issues/6339
///
/// When a test is subject to a timeout (an explicit <c>[Timeout]</c> or a configured
/// <c>DefaultTestTimeout</c>), the engine ran the body with a linked <see cref="CancellationTokenSource"/>
/// token stored on <c>TestContext.Execution.CancellationToken</c>. That source was disposed the moment the
/// body returned, so an <c>[After(Test)]</c> hook that read the context token and touched the underlying
/// source (e.g. accessing <see cref="CancellationToken.WaitHandle"/> — what a synchronous wait inside
/// ASP.NET Core integration cleanup via EF Core / Respawn / SemaphoreSlim ends up doing) threw
/// <see cref="ObjectDisposedException"/> "The CancellationTokenSource has been disposed."
///
/// The body completes fast (no actual timeout) so this is a plain passing test — the regression is the
/// disposed-token leak into the After phase, not cancellation. Before the fix the After hook throws and the
/// test is reported failed; after the fix the context token is the still-valid outer token and it passes.
/// </summary>
public class TimeoutAfterHookTokenTests
{
[Test]
[Timeout(30_000)]
[EngineTest(ExpectedResult.Pass)]
public Task Body_Completes_Then_After_Hook_Uses_Context_Token() => Task.CompletedTask;

[After(Test)]
public void After_Hook_Can_Use_Context_Cancellation_Token(TestContext context)
{
// Pre-fix: threw ObjectDisposedException "The CancellationTokenSource has been disposed."
// because the timeout-scoped CTS backing this token was already disposed by TimeoutHelper.
// Accessing WaitHandle calls the source's ThrowIfDisposed unconditionally, mirroring what
// synchronous waits inside real cleanup code (EF Core, Respawn, SemaphoreSlim.Wait) hit.
_ = context.Execution.CancellationToken.WaitHandle;
}
}
Loading