Great Cancellation Spinoff: Using Cancellation Tokens with Async Enumerators#1649
Great Cancellation Spinoff: Using Cancellation Tokens with Async Enumerators#1649
Conversation
There was a problem hiding this comment.
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)toawait foreachloops - Modified helper method signatures in Search, FunctionApp, and Foundry services to accept and pass through
CancellationTokenparameters - 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)) |
There was a problem hiding this comment.
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).
| await foreach (var account in subscriptionResource.GetCosmosDBAccountsAsync(cancellationToken)) | |
| await foreach (var account in subscriptionResource.GetCosmosDBAccountsAsync(cancellationToken).WithCancellation(cancellationToken)) |
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
servers/Azure.Mcp.Server/CHANGELOG.mdand/orservers/Fabric.Mcp.Server/CHANGELOG.mdfor product changes (features, bug fixes, UI/UX, updated dependencies)servers/Azure.Mcp.Server/README.mdand/orservers/Fabric.Mcp.Server/README.mddocumentationeng/scripts/Process-PackageReadMe.ps1. See Package README/servers/Azure.Mcp.Server/docs/azmcp-commands.mdand/or/docs/fabric-commands.md.\eng\scripts\Update-AzCommandsMetadata.ps1to update tool metadata in azmcp-commands.md (required for CI)ToolDescriptionEvaluatorand obtained a score of0.4or more and a top 3 ranking for all related test promptsconsolidated-tools.json/servers/Azure.Mcp.Server/docs/e2eTestPrompts.mdcrypto mining, spam, data exfiltration, etc.)/azp run mcp - pullrequest - liveto run Live Test Pipeline