[MTP] Cannot stop running tests if parallel testing used
using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
// without it, process stops
[assembly: Parallelizable(ParallelScope.Children)]
internal sealed class Tests
{
private static readonly int[] _cases = [.. Enumerable.Range(1, 100)];
[TestCaseSource(nameof(_cases))]
public async Task Test(int pause)
{
await Task.Delay(TimeSpan.FromSeconds(pause % 10));
}
}
When I want to stop my tests by pressing Ctrl-C in console, test process exists only when tests are not executed in parallel. For parallel execution it hangs and I need to terminate process
Microsoft.Testing.Platform v1.7.0+927c3c2120 (UTC 5/7/2025) [win-x64 - net9.0]
NUnit Adapter 5.1.0.0: Test execution started
Running all tests in MTPIssues\bin\Debug\net9.0\MTPIssues.dll
NUnit3TestExecutor discovered 100 of 100 NUnit test cases using Current Discovery mode, Non-Explicit run
Error processing Test(11) event for Tests.Test(11)
System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at Microsoft.Testing.Extensions.VSTestBridge.ObjectModel.FrameworkHandlerAdapter.RecordEnd(TestCase testCase, TestOutcome outcome) in /_/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FrameworkHandlerAdapter.cs:line 122
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 178
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 85
Error processing Test(3) event for Tests.Test(3)
System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at Microsoft.Testing.Extensions.VSTestBridge.ObjectModel.FrameworkHandlerAdapter.RecordEnd(TestCase testCase, TestOutcome outcome) in /_/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FrameworkHandlerAdapter.cs:line 122
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 178
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 85
Error processing Test(4) event for Tests.Test(4)
System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at Microsoft.Testing.Extensions.VSTestBridge.ObjectModel.FrameworkHandlerAdapter.RecordEnd(TestCase testCase, TestOutcome outcome) in /_/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FrameworkHandlerAdapter.cs:line 122
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 178
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 85
Error processing Test(5) event for Tests.Test(5)
System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at Microsoft.Testing.Extensions.VSTestBridge.ObjectModel.FrameworkHandlerAdapter.RecordEnd(TestCase testCase, TestOutcome outcome) in /_/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FrameworkHandlerAdapter.cs:line 122
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 178
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 85
Error processing Test(6) event for Tests.Test(6)
System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at Microsoft.Testing.Extensions.VSTestBridge.ObjectModel.FrameworkHandlerAdapter.RecordEnd(TestCase testCase, TestOutcome outcome) in /_/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FrameworkHandlerAdapter.cs:line 122
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 178
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 85
Error processing Test(7) event for Tests.Test(7)
System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at Microsoft.Testing.Extensions.VSTestBridge.ObjectModel.FrameworkHandlerAdapter.RecordEnd(TestCase testCase, TestOutcome outcome) in /_/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/FrameworkHandlerAdapter.cs:line 122
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.TestFinished(INUnitTestEventTestCase resultNode) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 178
at NUnit.VisualStudio.TestAdapter.NUnitEventListener.OnTestEvent(String report) in C:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEventListener.cs:line 85
Canceling the test session...
NUnit Adapter 5.1.0.0: Test execution complete
Test run summary: Aborted - MTPIssues.dll (net9.0|x64)
total: 3
failed: 0
succeeded: 3
skipped: 0
duration: 8s 119ms
Summary is printed, but process is still running (unterminated rogue thread without IsBackground=true?)
@Youssef1313 Comments ?
Issue mentions that Ctrl+C works when parallelization is disabled, but not with enabled. This is likely suggesting an NUnit bug and probably unlikely to be an issue on MTP side as cancellation is most likely propagated to NUnit.
@MaceWindu So this works without MTP, and not with MTP ?
Do you have a csproj for this?
Attached. It works without parallelzation, but not with. In vstest mode it also works.