Skip to content

Centralize test timeout constants to fix sporadic CI failures#1210

Merged
stephentoub merged 4 commits intomainfrom
copilot/fix-cancel-task-async-test
Jan 30, 2026
Merged

Centralize test timeout constants to fix sporadic CI failures#1210
stephentoub merged 4 commits intomainfrom
copilot/fix-cancel-task-async-test

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

Tests were timing out sporadically in CI with hardcoded 5-10 second WaitAsync timeouts insufficient for overloaded machines.

Changes

  • Created TestTimeouts.DefaultTimeout (60s) in tests/Common/Utils/TestTimeouts.cs
  • Replaced 29 hardcoded timeout instances across 10 test files with centralized constant
  • Added missing using ModelContextProtocol.Tests.Utils; where needed

Example change:

// Before
await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);

// After  
await tcs.Task.WaitAsync(TestTimeouts.DefaultTimeout, cancellationToken);

Files updated:

  • McpServerTaskMethodsTests.cs (2), AutomaticInputRequiredStatusTests.cs (4), McpServerTests.cs (4)
  • TaskCancellationIntegrationTests.cs (6), McpClientTaskSamplingElicitationTests.cs (8)
  • UrlElicitationTests.cs (1), ServerConformanceTests.cs (1), SseIntegrationTests.cs (1)
  • StreamableHttpClientConformanceTests.cs (1), MapMcpStreamableHttpTests.cs (1)

Not modified: StreamClientSessionTransport.cs production timeout left unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>CancelTaskAsync_SendsRequest_AndReturnsCancelledTask failing sporadically in CI</issue_title>
<issue_description>```
[xUnit.net 00:00:19.26] ModelContextProtocol.Tests.Server.McpServerTaskMethodsTests.CancelTaskAsync_SendsRequest_AndReturnsCancelledTask [FAIL]
Failed ModelContextProtocol.Tests.Server.McpServerTaskMethodsTests.CancelTaskAsync_SendsRequest_AndReturnsCancelledTask [5 s]
Error Message:
System.TimeoutException : The operation has timed out.
Stack Trace:
at ModelContextProtocol.Tests.Server.McpServerTaskMethodsTests.InitializeServerAsync(TestServerTransport transport, ClientCapabilities capabilities, CancellationToken cancellationToken) in //tests/ModelContextProtocol.Tests/Server/McpServerTaskMethodsTests.cs:line 758
at ModelContextProtocol.Tests.Server.McpServerTaskMethodsTests.CancelTaskAsync_SendsRequest_AndReturnsCancelledTask() in /
/tests/ModelContextProtocol.Tests/Server/McpServerTaskMethodsTests.cs:line 522
at ModelContextProtocol.Tests.Server.McpServerTaskMethodsTests.CancelTaskAsync_SendsRequest_AndReturnsCancelledTask() in //tests/ModelContextProtocol.Tests/Server/McpServerTaskMethodsTests.cs:line 544
at ModelContextProtocol.Tests.Server.McpServerTaskMethodsTests.CancelTaskAsync_SendsRequest_AndReturnsCancelledTask() in /
/tests/ModelContextProtocol.Tests/Server/McpServerTaskMethodsTests.cs:line 544
--- End of stack trace from previous location ---


## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@ericstj</author><body>
I'm noticing many tests like this are failing in CI with TimeoutException.  All of these tests are doing things like `WaitAsync(TimeSpan.FromSeconds(5)`.  It's possible that during CI the machine is overloaded and these timeouts are insufficient.

Can we look at all tests that have the pattern `WaitAsync(TimeSpan.FromSeconds` and replace the hardcoded constant with a centrally defined TimeSpan used for all test timeouts.  This can be placed on a appropriate type under tests/Common and shared by all tests.  Let's start with a new value of `TimeSpan.FromSeconds(60)` and see if these issues go away.</body></comment_new>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix sporadic failure in CancelTaskAsync test case Centralize test timeout constants to fix sporadic CI failures Jan 29, 2026
Copilot AI requested a review from ericstj January 29, 2026 22:06
@ericstj ericstj marked this pull request as ready for review January 29, 2026 22:15
stephentoub
stephentoub previously approved these changes Jan 29, 2026
@ericstj
Copy link
Contributor

ericstj commented Jan 29, 2026

I'm aiming to rerun the validation build on this a few times to be sure it's worth doing. If any of those still see the same timeout then we likely have a race-condition or hang.

@ericstj
Copy link
Contributor

ericstj commented Jan 29, 2026

Didn't hit the test wait timeout, but we did see a hang or crash repro on linux. The logged information is tough to decode

Passed!  - Failed:     0, Passed:  1327, Skipped:    10, Total:  1337, Duration: 22 h 20 m - ModelContextProtocol.Tests.exe (net472)
Blame: Dumping this process tree (from bottom):
Blame:  >  5808 - dotnet
Blame:  + - 6048 - ModelContextProtocol.AspNetCore.Tests
The active test run was aborted. Reason: Test host process crashed
Data collector 'Blame' message: The specified inactivity time of 7 minutes has elapsed. Collecting hang dumps from testhost and its child processes.
Results File: /home/runner/work/csharp-sdk/csharp-sdk/artifacts/testresults/_runnervmkj6or_2026-01-29_22_17_00.trx

Test Run Aborted.
Passed!  - Failed:     0, Passed:   228, Skipped:     8, Total:   236, Duration: 1 m 4 s - ModelContextProtocol.AspNetCore.Tests.dll (net8.0)

The active Test Run was aborted because the host process exited unexpectedly. Please inspect the call stack above, if available, to get more information about where the exception originated from.
The test running when the crash occurred: 
ModelContextProtocol.AspNetCore.Tests.StreamableHttpServerConformanceTests.GetRequest_Receives_UnsolicitedNotifications

This test may, or may not be the source of the crash.

Duration: 22 h 20 m -- So this test ran for 22 hours... really?
net472 -- not actually -- I've learned it's running on mono.
Test host process crashed -- did it crash or did The specified inactivity time of 7 minutes has elapsed?

I'll see if I can find anything from the dump. I'm considering extending the blame-hang-timeout to see if this might just be too low. We've also discussed disabling the mono testing.

@ericstj
Copy link
Contributor

ericstj commented Jan 29, 2026

So the results are interleaved - the dump in the results is from a net8.0 process, which appears to be hung. https://gist.github.com/ericstj/66698983a21264de205577adb330d185

My manual run of the tests is also hitting this. https://github.com/modelcontextprotocol/csharp-sdk/actions/runs/21497010662/job/61934307575

@ericstj
Copy link
Contributor

ericstj commented Jan 29, 2026

Both this dump, and the dump I looked at this morning have the following test executing while the hang occurs:

public async Task GetRequest_Receives_UnsolicitedNotifications()

<< Awaiting: 00007f66f6767068 00007fa70b7d04e0 System.Runtime.CompilerServices.ValueTaskAwaiter<System.Boolean> >>
  00007f66f6767750 00007fa70f59f330 (0) ModelContextProtocol.AspNetCore.Tests.StreamableHttpServerConformanceTests+<>c__DisplayClass22_0+<<GetRequest_Receives_UnsolicitedNotifications>g__GetFirstNotificationAsync|1>d
    00007f66f6743da0 00007fa70f50d808 (4) ModelContextProtocol.AspNetCore.Tests.StreamableHttpServerConformanceTests+<GetRequest_Receives_UnsolicitedNotifications>d__22
      00007f66f6743df0 00007fa70dd57958 (0) Xunit.v3.TestRunner<Xunit.v3.XunitTestRunnerContext, Xunit.v3.IXunitTest>+<>c__DisplayClass5_0+<<InvokeTest>b__1>d
        00007f66f6743e60 00007fa70dd5e640 (0) Xunit.v3.ExceptionAggregator+<RunAsync>d__13
          00007f66f6743ed8 00007fa70dd5eb10 (0) Xunit.v3.ExecutionTimer+<MeasureAsync>d__2
            00007f66f6743f58 00007fa70dd5f4a8 (0) Xunit.v3.ExceptionAggregator+<RunAsync>d__14<System.TimeSpan>
              00007f66f6743fe8 00007fa70dd85450 (0) Xunit.v3.TestRunner<Xunit.v3.XunitTestRunnerContext, Xunit.v3.IXunitTest>+<>c__DisplayClass15_1+<<RunTest>g__runTest|0>d

I might just see about disabling this test to see if that resolves things.

@ericstj
Copy link
Contributor

ericstj commented Jan 29, 2026

Confirmed that my manual run's dump has the exact same test hanging.

<< Awaiting: 00007ee35e6b42f8 00007f2378dd04e0 System.Runtime.CompilerServices.ValueTaskAwaiter<System.Boolean> >>
  00007ee35e6b49e0 00007f237c4703d8 (0) ModelContextProtocol.AspNetCore.Tests.StreamableHttpServerConformanceTests+<>c__DisplayClass22_0+<<GetRequest_Receives_UnsolicitedNotifications>g__GetFirstNotificationAsync|1>d
    00007ee35e699fb8 00007f237c454da0 (4) ModelContextProtocol.AspNetCore.Tests.StreamableHttpServerConformanceTests+<GetRequest_Receives_UnsolicitedNotifications>d__22
      00007ee35e69a008 00007f237aeee0f8 (0) Xunit.v3.TestRunner<Xunit.v3.XunitTestRunnerContext, Xunit.v3.IXunitTest>+<>c__DisplayClass5_0+<<InvokeTest>b__1>d
        00007ee35e69a078 00007f237aeee4b0 (0) Xunit.v3.ExceptionAggregator+<RunAsync>d__13
          00007ee35e69a0f0 00007f237aeee980 (0) Xunit.v3.ExecutionTimer+<MeasureAsync>d__2
            00007ee35e69a170 00007f237aef8558 (0) Xunit.v3.ExceptionAggregator+<RunAsync>d__14<System.TimeSpan>
              00007ee35e69a200 00007f237aef9260 (0) Xunit.v3.TestRunner<Xunit.v3.XunitTestRunnerContext, Xunit.v3.IXunitTest>+<>c__DisplayClass15_1+<<RunTest>g__runTest|0>d

@ericstj
Copy link
Contributor

ericstj commented Jan 30, 2026

Failure is #1199

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.

CancelTaskAsync_SendsRequest_AndReturnsCancelledTask failing sporadically in CI

3 participants