Repository navigation
Preserve graceful cancellation through test host controller #11054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Amaury Levé (Evangelink)
merged 25 commits into
main
from
dev/amauryleve/investigate-issue-11040
Sep 7, 2026
Merged
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
90afc72
Preserve graceful cancellation through test host controller
Evangelink 14b2ec6
Merge main into phased cancellation fix
Evangelink c11eaa2
Address phased cancellation review feedback
Evangelink d39b07f
Avoid shutdown logging reentrancy
Evangelink 31576aa
Document expected control connection cancellation
Evangelink e4f1505
Honor configured cooperative shutdown budget
Evangelink 1a1e0d9
Serialize console cancellation acceptance tests
Evangelink b362316
Force-terminate child on second cancellation
Evangelink 0d9b591
Bound cancellation teardown edge cases
Evangelink 77621dd
Improve cancellation teardown test seams
Evangelink 2e4912c
Make forced cancellation assertion deterministic
Evangelink 01b9eca
Track linked pipe API baselines
Evangelink 1ba9074
Stabilize cancellation channel disposal
Evangelink f90bff3
Start child cancellation listener before handshake
Evangelink f75c7db
Preserve late cooperative completion reporting
Evangelink 91e7332
Keep force termination registered through finalization
Evangelink 97c6cc2
Cover cancellation queued before child connection
Evangelink f2adc9d
Handle Unix cancellation channel shutdown
Evangelink db30990
Allow CI scheduling slack in disposal tests
Evangelink 9b03841
Synchronize named pipe loop publication
Evangelink f94dd99
Harden cancellation disposal test timing
Evangelink b870211
Use target-appropriate lifecycle lock
Evangelink 354dfd8
Honor child cancellation timeout overrides
Evangelink 841865e
Verify delayed completion cancellation
Evangelink 69da75a
Snapshot pipe connection state atomically
Evangelink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
src/Platform/Microsoft.Testing.Extensions.HangDump/InternalAPI/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| #nullable enable | ||
| const Microsoft.Testing.Platform.Helpers.EnvironmentVariableConstants.TESTINGPLATFORM_TESTHOSTCONTROLLER_CONTROLPIPENAME = "TESTINGPLATFORM_TESTHOSTCONTROLLER_CONTROLPIPENAME" -> string! |
1 change: 1 addition & 0 deletions
1
src/Platform/Microsoft.Testing.Extensions.HotReload/InternalAPI/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| #nullable enable | ||
| const Microsoft.Testing.Platform.Helpers.EnvironmentVariableConstants.TESTINGPLATFORM_TESTHOSTCONTROLLER_CONTROLPIPENAME = "TESTINGPLATFORM_TESTHOSTCONTROLLER_CONTROLPIPENAME" -> string! |
1 change: 1 addition & 0 deletions
1
src/Platform/Microsoft.Testing.Extensions.MSBuild/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| #nullable enable | ||
| const Microsoft.Testing.Platform.Helpers.EnvironmentVariableConstants.TESTINGPLATFORM_TESTHOSTCONTROLLER_CONTROLPIPENAME = "TESTINGPLATFORM_TESTHOSTCONTROLLER_CONTROLPIPENAME" -> string! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/Platform/Microsoft.Testing.Extensions.TrxReport/InternalAPI/InternalAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| #nullable enable | ||
| const Microsoft.Testing.Platform.Helpers.EnvironmentVariableConstants.TESTINGPLATFORM_TESTHOSTCONTROLLER_CONTROLPIPENAME = "TESTINGPLATFORM_TESTHOSTCONTROLLER_CONTROLPIPENAME" -> string! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllerCancellationListener.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.Testing.Platform.Helpers; | ||
| using Microsoft.Testing.Platform.IPC; | ||
| using Microsoft.Testing.Platform.IPC.Models; | ||
| using Microsoft.Testing.Platform.IPC.Serializers; | ||
| using Microsoft.Testing.Platform.Logging; | ||
| using Microsoft.Testing.Platform.Services; | ||
|
|
||
| namespace Microsoft.Testing.Platform.Hosts; | ||
|
|
||
| [UnsupportedOSPlatform("browser")] | ||
| internal sealed class TestHostControllerCancellationListener : IDisposable | ||
| #if NET | ||
| #pragma warning disable SA1001 // Commas should be spaced correctly | ||
| , IAsyncDisposable | ||
| #pragma warning restore SA1001 // Commas should be spaced correctly | ||
| #endif | ||
| { | ||
| private static readonly TimeSpan ConnectionTimeout = TimeSpan.FromSeconds(30); | ||
|
|
||
| private readonly CTRLPlusCCancellationTokenSource _testApplicationCancellationTokenSource; | ||
| private readonly ILogger _logger; | ||
| private readonly NamedPipeClient _client; | ||
| private readonly CancellationTokenSource _shutdownCancellationTokenSource = new(); | ||
| private readonly Task _listenerTask; | ||
| private int _cancellationRequestedByController; | ||
| private int _disposed; | ||
|
|
||
| public TestHostControllerCancellationListener( | ||
| string pipeName, | ||
| CTRLPlusCCancellationTokenSource testApplicationCancellationTokenSource, | ||
| IEnvironment environment, | ||
| ILogger logger) | ||
| { | ||
| _testApplicationCancellationTokenSource = testApplicationCancellationTokenSource; | ||
| _logger = logger; | ||
| _client = new NamedPipeClient(pipeName, environment, exitProcessOnConnectionLoss: false); | ||
| _client.RegisterAllSerializers(); | ||
| _listenerTask = ListenAsync(_shutdownCancellationTokenSource.Token); | ||
| } | ||
|
|
||
| public bool WasCancellationRequestedByController | ||
| => Volatile.Read(ref _cancellationRequestedByController) != 0; | ||
|
|
||
| public bool ShouldReportCompletionAfterCancellation | ||
| => WasCancellationRequestedByController | ||
| || _testApplicationCancellationTokenSource.WasCancellationRequestedByConsole; | ||
|
|
||
| private async Task ListenAsync(CancellationToken cancellationToken) | ||
| { | ||
| bool connected = false; | ||
| try | ||
| { | ||
| using var connectCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | ||
| connectCancellationTokenSource.CancelAfter(ConnectionTimeout); | ||
| await _client.ConnectAsync(connectCancellationTokenSource.Token).ConfigureAwait(false); | ||
| connected = true; | ||
|
|
||
| while (!cancellationToken.IsCancellationRequested) | ||
| { | ||
| ServerControlMessage message = await _client.RequestReplyAsync<WaitForServerControlRequest, ServerControlMessage>( | ||
| WaitForServerControlRequest.CachedInstance, | ||
| cancellationToken).ConfigureAwait(false); | ||
| if (message.Kind == ServerControlKinds.CancelSession) | ||
| { | ||
| RequestCancellation(); | ||
| return; | ||
| } | ||
|
|
||
| if (message.Kind == ServerControlKinds.CloseChannel) | ||
| { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| catch (Exception ex) when (cancellationToken.IsCancellationRequested) | ||
| { | ||
| // The platform logger can already be disposed when this listener is torn down. Keep the expected | ||
| // shutdown race observable for debugger diagnostics without re-entering the logging pipeline. | ||
| Debug.WriteLine($"Test host controller cancellation channel stopped during shutdown: {ex}"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| await _logger.LogDebugAsync($"Test host controller cancellation channel stopped unexpectedly: {ex}").ConfigureAwait(false); | ||
| if (connected) | ||
| { | ||
| RequestCancellation(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| _client.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| private void RequestCancellation() | ||
| { | ||
| Volatile.Write(ref _cancellationRequestedByController, 1); | ||
| try | ||
| { | ||
| _testApplicationCancellationTokenSource.Cancel(); | ||
| } | ||
| catch (AggregateException ex) | ||
| { | ||
| _logger.LogWarning($"Exception while propagating test host controller cancellation:\n{ex}"); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| => DisposeAsyncCore().GetAwaiter().GetResult(); | ||
|
|
||
| #if NET | ||
| public async ValueTask DisposeAsync() | ||
| => await DisposeAsyncCore().ConfigureAwait(false); | ||
| #endif | ||
|
|
||
| private async Task DisposeAsyncCore() | ||
| { | ||
| if (Interlocked.Exchange(ref _disposed, 1) != 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| #if NET | ||
| await _shutdownCancellationTokenSource.CancelAsync().ConfigureAwait(false); | ||
| #else | ||
| _shutdownCancellationTokenSource.Cancel(); | ||
| #endif | ||
| _client.Dispose(); | ||
| await _listenerTask.ConfigureAwait(false); | ||
| _shutdownCancellationTokenSource.Dispose(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.