Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 4, 2025

When a test run is cancelled (e.g., via Ctrl+C), cleanup hooks were not executing properly because they were using the same cancelled CancellationToken that triggered the cancellation. This could lead to incomplete cleanup and resource leaks.

Problem

The issue occurred in two key locations:

  1. Test-level cleanup in HookOrchestratingTestExecutorAdapter.ExecuteTestAsync() - the finally block was calling cleanup hooks with the main cancellationToken
  2. Session-level cleanup in TestExecutor.ExecuteTests() - session cleanup was also using the cancelled token

When cancellation occurred, these cleanup operations would receive an already-cancelled token, potentially preventing proper execution of [After(Test)], [After(Class)], [After(Assembly)], and [After(TestSession)] hooks.

Solution

Modified both cleanup paths to use separate CancellationTokenSource instances with reasonable timeouts:

  • Test cleanup: 1-minute timeout for individual test cleanup operations
  • Session cleanup: 2-minute timeout for session-level cleanup operations

This ensures cleanup hooks have independent cancellation tokens and sufficient time to complete their work, even when the main test execution is cancelled.

Code Changes

// Before: Used the cancelled token
await _hookOrchestrator.OnTestCompletedAsync(test, cancellationToken);

// After: Uses independent cleanup token
using var cleanupCts = new CancellationTokenSource(TimeSpan.FromMinutes(1));
await _hookOrchestrator.OnTestCompletedAsync(test, cleanupCts.Token);

Benefits

  • Reliable cleanup: After hooks now execute consistently even during cancellation
  • Resource safety: Prevents resource leaks from incomplete cleanup
  • Graceful shutdown: Test environment maintains proper state even when interrupted
  • Backwards compatible: No breaking changes to existing functionality

Fixes #2731.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: curl -I -sSL --retry 5 --retry-delay 2 --connect-timeout 15 REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] If a test run is cancelled, invoke all relevant clean up methods Fix cancellation cleanup: ensure After hooks execute when test run is cancelled Aug 4, 2025
Copilot AI requested a review from thomhurst August 4, 2025 00:33
Copilot finished work on behalf of thomhurst August 4, 2025 00:33
@thomhurst thomhurst marked this pull request as ready for review August 4, 2025 08:01
@thomhurst thomhurst merged commit 36f803b into main Aug 4, 2025
1 of 2 checks passed
@thomhurst thomhurst deleted the copilot/fix-2731 branch August 4, 2025 08:01
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.

If a test run is cancelled, invoke all relevant clean up methods

2 participants