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
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ await _messageBus.PublishAsync(
// Execute cleanup hooks
try
{
await _hookOrchestrator.OnTestCompletedAsync(test, cancellationToken);
// Use a separate cancellation token for cleanup to ensure cleanup hooks execute
// even when the main test execution is cancelled. Give cleanup a reasonable timeout.
using var cleanupCts = new CancellationTokenSource(TimeSpan.FromMinutes(1));
await _hookOrchestrator.OnTestCompletedAsync(test, cleanupCts.Token);
}
catch (Exception ex)
{
Expand Down
18 changes: 14 additions & 4 deletions TUnit.Engine/TestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,23 @@ public async Task ExecuteTests(
}
finally
{
var afterSessionContext = await hookOrchestrator.ExecuteAfterTestSessionHooksAsync(cancellationToken);
// Execute session cleanup hooks with a separate cancellation token to ensure
// cleanup executes even when test execution is cancelled
try
{
using var cleanupCts = new CancellationTokenSource(TimeSpan.FromMinutes(2));
var afterSessionContext = await hookOrchestrator.ExecuteAfterTestSessionHooksAsync(cleanupCts.Token);
#if NET
if (afterSessionContext != null)
if (afterSessionContext != null)
{
ExecutionContext.Restore(afterSessionContext);
}
#endif
}
catch (Exception ex)
{
ExecutionContext.Restore(afterSessionContext);
await _logger.LogErrorAsync($"Error in session cleanup hooks: {ex}");
}
#endif
}
}

Expand Down