Fix memory leaks in CertificateManager by improving certificate disposal patterns#63321
Merged
Conversation
…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
danmoseley
reviewed
Aug 19, 2025
…te method Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
danmoseley
approved these changes
Aug 19, 2025
Contributor
There was a problem hiding this comment.
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
SaveCertificateusage to match established patterns in the codebase
…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
danmoseley
approved these changes
Aug 19, 2025
…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>
Member
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
BrennanConroy
approved these changes
Feb 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixed memory leaks in the
CertificateManagerclass whereX509Certificate2objects were not being properly disposed in several code paths, as identified by static analysis.Issues Fixed
ImportCertificate method:
SaveCertificatethrew an exceptionEnsureAspNetCoreHttpsDevelopmentCertificate method:
certificatescollection containing all loaded certificates was not being disposed in early exit cases, causing memory leaksChanges Made
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.
Fixed SaveCertificate usage pattern: Updated to assign the return value of
SaveCertificateback to the certificate variable, matching the pattern used elsewhere in the codebase.Added certificate disposal in EnsureAspNetCoreHttpsDevelopmentCertificate: Added proper certificate disposal before early returns in error cases for SaveCertificate, ExportCertificate, and TrustCertificate operations.
Fixed certificate collection disposal pattern: Updated the certificate loading and disposal pattern to follow the same approach as
ListCertificates:.ToList()to prevent LINQ deferred execution issuesCorrected certificate disposal logic: Fixed disposal logic to only dispose the
certificateif it's a new certificate (isNewCertificate == true). If the certificate came from the existing certificates collection, it will be disposed as part ofDisposeCertificates(certificates), following the original disposal pattern:DisposeCertificates(!isNewCertificate ? certificates : certificates.Append(certificate)).Verification
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.