Skip to content

Great Cancellation Spinoff: Using Cancellation Tokens with Async Enumerators#1649

Open
msalaman wants to merge 2 commits intomainfrom
masalama/cancellationTokensForEnumerators
Open

Great Cancellation Spinoff: Using Cancellation Tokens with Async Enumerators#1649
msalaman wants to merge 2 commits intomainfrom
masalama/cancellationTokensForEnumerators

Conversation

@msalaman
Copy link
Contributor

@msalaman msalaman commented Feb 4, 2026

What does this PR do?

Makes sure a cancellationToken is used whenever an Async Enumerator is traversed using the WithCancellation method. Updated New Command instructions to implement this practice in future generated code

GitHub issue number?

#1583

Pre-merge Checklist

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Updated servers/Azure.Mcp.Server/CHANGELOG.md and/or servers/Fabric.Mcp.Server/CHANGELOG.md for product changes (features, bug fixes, UI/UX, updated dependencies)
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Validate README.md changes using script at eng/scripts/Process-PackageReadMe.ps1. See Package README
    • Updated command list in /servers/Azure.Mcp.Server/docs/azmcp-commands.md and/or /docs/fabric-commands.md
    • Run .\eng\scripts\Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
    • For tools with new names, including new tools or renamed tools, update consolidated-tools.json
    • For new tools associated with Azure services or publicly available tools/APIs/products, add URL to documentation in the PR description
  • Extra steps for Azure MCP Server tool changes:
    • Updated test prompts in /servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds .WithCancellation(cancellationToken) to async enumerator iterations across multiple Azure MCP tool services and updates the new command documentation to enforce this pattern as a coding standard. The changes are part of the "Great Cancellation" effort (issue #1583) to ensure proper cancellation token support throughout the codebase.

Changes:

  • Updated 13 service files across various Azure MCP tools to add .WithCancellation(cancellationToken) to await foreach loops
  • Modified helper method signatures in Search, FunctionApp, and Foundry services to accept and pass through CancellationToken parameters
  • Added comprehensive documentation and examples for async enumerable cancellation patterns in the new command guide

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tools/Azure.Mcp.Tools.StorageSync/src/Services/StorageSyncService.cs Added WithCancellation to 5 async foreach loops for storage sync collections
tools/Azure.Mcp.Tools.SignalR/src/Services/SignalRService.cs Added WithCancellation to 2 async foreach loops for SignalR resource enumeration
tools/Azure.Mcp.Tools.Search/src/Services/SearchService.cs Updated ProcessSearchResults helper to accept CancellationToken and use WithCancellation
tools/Azure.Mcp.Tools.Redis/src/Services/RedisService.cs Added WithCancellation to 2 async foreach loops for Redis resource enumeration
tools/Azure.Mcp.Tools.Quota/src/Services/Util/Usage/*.cs Added WithCancellation to usage checking loops across 8 usage checker classes
tools/Azure.Mcp.Tools.Quota/src/Services/Util/AzureRegionChecker.cs Added WithCancellation to 2 region availability checking loops
tools/Azure.Mcp.Tools.LoadTesting/src/Services/LoadTestingService.cs Added WithCancellation to test run enumeration loop
tools/Azure.Mcp.Tools.FunctionApp/src/Services/FunctionAppService.cs Updated RetrieveAndAddFunctionApp helper to accept CancellationToken and use WithCancellation
tools/Azure.Mcp.Tools.Foundry/src/Services/FoundryService.cs Updated BuildResourceInformation helper to accept CancellationToken and added WithCancellation to 3 loops
tools/Azure.Mcp.Tools.FileShares/src/Services/FileSharesService.cs Added WithCancellation to 3 async foreach loops for file share operations
tools/Azure.Mcp.Tools.EventHubs/src/Services/EventHubsService.cs Added WithCancellation to 5 async foreach loops for Event Hubs resource enumeration
tools/Azure.Mcp.Tools.Cosmos/src/Services/CosmosService.cs Updated GetCosmosAccountAsync to accept CancellationToken parameter (but missing WithCancellation - see bug comment)
core/Azure.Mcp.Core/src/Services/Azure/Tenant/TenantService.cs Added WithCancellation to tenant enumeration loop
servers/Azure.Mcp.Server/docs/new-command.md Added comprehensive documentation with examples for async enumerable cancellation pattern

var subscriptionResource = await _subscriptionService.GetSubscription(subscription, tenant, retryPolicy, cancellationToken);

await foreach (var account in subscriptionResource.GetCosmosDBAccountsAsync())
await foreach (var account in subscriptionResource.GetCosmosDBAccountsAsync(cancellationToken))
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing .WithCancellation(cancellationToken) on the async enumerable. The PR's stated purpose is to add .WithCancellation(cancellationToken) to all async enumerator iterations, but this occurrence at line 42 was not updated. According to the new documentation pattern added in this PR (servers/Azure.Mcp.Server/docs/new-command.md lines 919-934), all await foreach loops should use .WithCancellation(cancellationToken).

Suggested change
await foreach (var account in subscriptionResource.GetCosmosDBAccountsAsync(cancellationToken))
await foreach (var account in subscriptionResource.GetCosmosDBAccountsAsync(cancellationToken).WithCancellation(cancellationToken))

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

1 participant