Skip to content

Fix race condition in AutomaticInputRequiredStatusTests polling logic#1223

Merged
stephentoub merged 5 commits intomainfrom
copilot/fix-intermittent-test-failure
Jan 30, 2026
Merged

Fix race condition in AutomaticInputRequiredStatusTests polling logic#1223
stephentoub merged 5 commits intomainfrom
copilot/fix-intermittent-test-failure

Conversation

Copy link
Contributor

Copilot AI commented Jan 30, 2026

Fix intermittent test failure in AutomaticInputRequiredStatusTests

Root Cause

The test TaskStatus_TransitionsToInputRequired_DuringElicitAsync (and its sibling TaskStatus_TransitionsToInputRequired_DuringSampleAsync) has a race condition in its polling loop. The loop continues only while the task status is Working, but the expected behavior is that the status should transition from WorkingInputRequiredWorkingCompleted.

When the test polls the task store while the status is temporarily in the InputRequired state, the loop exits early and the test fails because it expects Completed but finds InputRequired.

Solution

Updated the polling condition in both tests to continue while the status is not Completed. This allows the tests to correctly wait for the task to reach completion regardless of the timing of the polling.

Changes Made

  • Modified TaskStatus_TransitionsToInputRequired_DuringSampleAsync polling loop condition (line 122)
  • Modified TaskStatus_TransitionsToInputRequired_DuringElicitAsync polling loop condition (line 221)
  • Simplified condition to: finalStatus?.Status is not McpTaskStatus.Completed

Verification

  • Understand the issue and root cause
  • Fix the polling logic in both failing tests to continue while status is not completed
  • Run the specific failing tests to validate the fix - both tests now pass consistently
  • Run the full test suite to ensure no regressions - all tests pass
  • Apply feedback to use simpler pattern matching syntax
Original prompt

This section details on the original issue you should resolve

<issue_title>AutomaticInputRequiredStatusTests.TaskStatus_TransitionsToInputRequired_DuringElicitAsync intermittently fails (timing/race)</issue_title>
<issue_description>https://github.com/modelcontextprotocol/csharp-sdk/actions/runs/21503213903/job/61953813945?pr=1070

  Failed ModelContextProtocol.Tests.Server.AutomaticInputRequiredStatusTests.TaskStatus_TransitionsToInputRequired_DuringElicitAsync [550 ms]
  Error Message:
   Assert.Equal() Failure: Values differ
Expected: Completed
Actual:   InputRequired
  Stack Trace:
     at ModelContextProtocol.Tests.Server.AutomaticInputRequiredStatusTests.TaskStatus_TransitionsToInputRequired_DuringElicitAsync() in /_/tests/ModelContextProtocol.Tests/Server/AutomaticInputRequiredStatusTests.cs:line 228
   at ModelContextProtocol.Tests.Server.AutomaticInputRequiredStatusTests.TaskStatus_TransitionsToInputRequired_DuringElicitAsync() in /_/tests/ModelContextProtocol.Tests/Server/AutomaticInputRequiredStatusTests.cs:line 228
--- End of stack trace from previous location ---

do
{
await Task.Delay(100, TestContext.Current.CancellationToken);
finalStatus = await taskStore.GetTaskAsync(mcpTask.TaskId, cancellationToken: TestContext.Current.CancellationToken);
maxAttempts--;
}
while (finalStatus?.Status == McpTaskStatus.Working && maxAttempts > 0);
// Assert - Status should have been InputRequired during elicitation
Assert.Contains(McpTaskStatus.InputRequired, statusesDuringElicitation);
// Final status should be Completed
Assert.NotNull(finalStatus);
Assert.Equal(McpTaskStatus.Completed, finalStatus.Status);

It looks to me like the task briefly transitioned status from Working to InputRequired and this caused the test to stop polling. The test then failed when it expected the test to transition to Completed.</issue_description>

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


💡 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.

Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix intermittent failure in TaskStatus transitions test Fix race condition in AutomaticInputRequiredStatusTests polling logic Jan 30, 2026
Copilot AI requested a review from ericstj January 30, 2026 17:02
@ericstj ericstj marked this pull request as ready for review January 30, 2026 17:10
Copilot AI and others added 2 commits January 30, 2026 17:46
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@ericstj ericstj requested review from stephentoub and removed request for stephentoub January 30, 2026 20:11
@stephentoub stephentoub merged commit 283caf9 into main Jan 30, 2026
10 checks passed
@stephentoub stephentoub deleted the copilot/fix-intermittent-test-failure branch January 30, 2026 20:21
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.

AutomaticInputRequiredStatusTests.TaskStatus_TransitionsToInputRequired_DuringElicitAsync intermittently fails (timing/race)

3 participants