Skip to content

Add IAsyncDisposable to CosmosService and fix async disposal pattern#1532

Merged
anuchandy merged 2 commits intomicrosoft:mainfrom
anuchandy:cosmos-idispose
Jan 28, 2026
Merged

Add IAsyncDisposable to CosmosService and fix async disposal pattern#1532
anuchandy merged 2 commits intomicrosoft:mainfrom
anuchandy:cosmos-idispose

Conversation

@anuchandy
Copy link
Copy Markdown
Member

What does this PR do?

[Provide a clear, concise description of the changes]

The .NET DI container prefers DisposeAsync() during shutdown, pr Implement IAsyncDisposable in CosmosService. Following https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync

Fixes: #1148

[Any additional context, screenshots, or information that helps reviewers]

GitHub issue number?

[Link to the GitHub issue this PR addresses]

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
Copy Markdown
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 PR implements IAsyncDisposable in CosmosService to fix an anti-pattern where the Dispose() method was implemented as async void, which prevented callers from awaiting the disposal operation. The implementation follows the Microsoft async disposal pattern guidelines for sealed classes.

Changes:

  • Added IAsyncDisposable to the ICosmosService interface and CosmosService class
  • Replaced the async void Dispose(bool disposing) method with a proper DisposeAsyncCore() implementation
  • Added DisposeAsync() method that properly implements the async disposal pattern
  • Modified synchronous Dispose() to call the async core method synchronously

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
tools/Azure.Mcp.Tools.Cosmos/src/Services/ICosmosService.cs Added IAsyncDisposable interface to ICosmosService
tools/Azure.Mcp.Tools.Cosmos/src/Services/CosmosService.cs Implemented async disposal pattern with DisposeAsyncCore(), DisposeAsync(), and updated Dispose() methods; marked class as sealed
servers/Azure.Mcp.Server/changelog-entries/1768937539777.yaml Added changelog entry documenting the bug fix

Comment thread tools/Azure.Mcp.Tools.Cosmos/src/Services/CosmosService.cs Outdated
Comment thread tools/Azure.Mcp.Tools.Cosmos/src/Services/CosmosService.cs
Comment thread tools/Azure.Mcp.Tools.Cosmos/src/Services/CosmosService.cs Outdated
Comment thread tools/Azure.Mcp.Tools.Cosmos/src/Services/CosmosService.cs
Comment thread tools/Azure.Mcp.Tools.Cosmos/src/Services/CosmosService.cs Outdated
@joshfree joshfree moved this from Untriaged to Not Started in Azure MCP Server Jan 20, 2026
@joshfree joshfree added this to the 2026-01 milestone Jan 20, 2026
@joshfree joshfree moved this from Not Started to In Progress in Azure MCP Server Jan 20, 2026
Copy link
Copy Markdown
Contributor

@g2vinay g2vinay left a comment

Choose a reason for hiding this comment

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

Any unit tests we can add for this ?

@anuchandy anuchandy merged commit 775b067 into microsoft:main Jan 28, 2026
13 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Azure MCP Server Jan 28, 2026
@anuchandy
Copy link
Copy Markdown
Member Author

Hi Vinay - sorry I missed responding to your comment earlier.

This follows Microsoft’s documented DI pattern for IAsyncDisposable: the DI container is responsible for when and how disposal is invoked. Adding a unit test here would mostly end up testing the framework/container behavior rather than our code, which is why I didn’t include a dedicated test for IAsyncDisposable.

colbytimm pushed a commit to colbytimm/microsoft-mcp that referenced this pull request Apr 20, 2026
…icrosoft#1532)

* Add IAsyncDisposable to CosmosService and fix async disposal pattern

* address review feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] ICosmosService should implement IAsyncDisposable instead of IDisposable

4 participants