Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix memory leaks in CertificateManager by improving certificate disposal patterns #63321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Copilot wants to merge 6 commits into main
base: main
Choose a base branch
Loading
from copilot/fix-59538

Conversation

Copy link
Contributor

Copilot AI commented Aug 18, 2025
edited
Loading

Fixed memory leaks in the CertificateManager class where X509Certificate2 objects were not being properly disposed in several code paths, as identified by static analysis.

Issues Fixed

ImportCertificate method:

  • Certificate objects were not disposed when validation failed (subject mismatch or not a development certificate)
  • Certificate objects were not disposed when SaveCertificate threw an exception
  • Certificate objects were not disposed on successful completion
  • Certificate objects were not disposed when certificate loading failed but a certificate object may have been partially created

EnsureAspNetCoreHttpsDevelopmentCertificate method:

  • Certificate objects were not disposed before early returns in error cases during save, export, and trust operations
  • The certificates collection containing all loaded certificates was not being disposed in early exit cases, causing memory leaks

Changes Made

  1. Refactored ImportCertificate to use try-finally pattern: Replaced scattered disposal calls with a cleaner, more robust try-finally pattern that ensures the certificate is always disposed regardless of the exit path.

  2. Fixed SaveCertificate usage pattern: Updated to assign the return value of SaveCertificate back to the certificate variable, matching the pattern used elsewhere in the codebase.

  3. Added certificate disposal in EnsureAspNetCoreHttpsDevelopmentCertificate: Added proper certificate disposal before early returns in error cases for SaveCertificate, ExportCertificate, and TrustCertificate operations.

  4. Fixed certificate collection disposal pattern: Updated the certificate loading and disposal pattern to follow the same approach as ListCertificates:

    • Materialize certificate collections early with .ToList() to prevent LINQ deferred execution issues
    • Dispose unused certificates immediately after filtering
    • Dispose remaining certificates in all early exit cases
  5. Corrected certificate disposal logic: Fixed disposal logic to only dispose the certificate if it's a new certificate (isNewCertificate == true). If the certificate came from the existing certificates collection, it will be disposed as part of DisposeCertificates(certificates), following the original disposal pattern: DisposeCertificates(!isNewCertificate ? certificates : certificates.Append(certificate)).

Verification

  • All certificate manager tests continue to pass (18/18)
  • All shared library tests pass
  • Reviewed other certificate creation sites in the codebase and confirmed they handle disposal correctly
  • Fixed specific test failure caused by accessing disposed certificate properties during LINQ enumeration

The fix ensures proper resource cleanup in all code paths while maintaining existing functionality and following established patterns in the codebase.

Fixes #59538


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

...9Certificate2
Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
Copilot AI changed the title (削除) [WIP] possible memleak in internal CertificateManager (削除ここまで) (追記) Fix memory leak in CertificateManager.ImportCertificate method (追記ここまで) Aug 18, 2025
Copilot AI requested a review from danmoseley August 18, 2025 23:40
Copilot finished work on behalf of danmoseley August 18, 2025 23:40
...te method
Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
Copilot finished work on behalf of danmoseley August 19, 2025 00:20
Copilot AI requested a review from danmoseley August 19, 2025 00:20
@danmoseley danmoseley marked this pull request as ready for review August 19, 2025 17:58
Copilot AI review requested due to automatic review settings August 19, 2025 17:58
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 PR fixes a memory leak in the ImportCertificate method by adding proper disposal of X509Certificate2 objects in all code paths. The changes ensure that certificate resources are cleaned up whether the method succeeds, fails validation, or encounters exceptions.

Key Changes

  • Added proper disposal on validation failure and save failure scenarios
  • Added disposal after successful completion
  • Added disposal in exception handling for certificate loading failures
  • Updated SaveCertificate usage to match established patterns in the codebase

Copilot AI requested a review from danmoseley August 19, 2025 21:06
Copilot finished work on behalf of danmoseley August 19, 2025 21:06
Copilot finished work on behalf of BrennanConroy August 19, 2025 21:23
Copilot finished work on behalf of danmoseley August 19, 2025 22:53
...sal issues in EnsureAspNetCoreHttpsDevelopmentCertificate
Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Copilot AI changed the title (削除) Fix memory leak in CertificateManager.ImportCertificate method (削除ここまで) (追記) Fix memory leaks in CertificateManager by improving certificate disposal patterns (追記ここまで) Aug 19, 2025
Copilot finished work on behalf of BrennanConroy August 19, 2025 23:16
Copilot AI requested a review from BrennanConroy August 19, 2025 23:16
catch (Exception e)
{
Log.SaveCertificateInStoreError(e.ToString());
certificate?.Dispose();
Copy link
Member

@BrennanConroy BrennanConroy Aug 21, 2025

Choose a reason for hiding this comment

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

@copilot The certificates variable contains a list of certificates and should be disposed in all early exit cases. See the use of DisposeCertificates at the bottom of this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're absolutely right. Fixed the certificate collection disposal pattern to properly dispose all loaded certificates in early exit cases. The issue was that the certificates variable contains the full list of certificates loaded from both stores, and they weren't being disposed in early return paths. I also had to materialize the LINQ expressions with .ToList() to prevent issues with accessing disposed certificate properties during enumeration. All tests now pass. Fixed in commit e2b7b68.

Copy link
Member

@BrennanConroy BrennanConroy Aug 21, 2025

Choose a reason for hiding this comment

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

Ok, this is close, but we shouldn't dispose certificate unless it's a new certificate. See original disposal code:
DisposeCertificates(!isNewCertificate ? certificates : certificates.Append(certificate));

Copy link
Member

@BrennanConroy BrennanConroy Aug 21, 2025

Choose a reason for hiding this comment

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

@copilot See previous comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed the certificate disposal logic to only dispose the certificate if it's a new certificate (isNewCertificate == true). If the certificate came from the existing certificates collection, it will be disposed as part of DisposeCertificates(certificates). This now matches the original disposal pattern at the end of the method. Fixed in commit e9c4aad.

...ttpsDevelopmentCertificate
Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
...ng ones from collection
Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Copilot finished work on behalf of BrennanConroy August 21, 2025 22:15
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Aug 29, 2025
Copy link
Member

/azp run

@dotnet-policy-service dotnet-policy-service bot removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 27, 2025
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

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

Reviewers

Copilot code review Copilot Copilot left review comments

@BrennanConroy BrennanConroy Awaiting requested review from BrennanConroy

+1 more reviewer

@danmoseley danmoseley danmoseley approved these changes

Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

possible memleak in internal CertificateManager

AltStyle によって変換されたページ (->オリジナル) /