-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
base: main
Are you sure you want to change the base?
Conversation
Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo! |
…9Certificate2 Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
…te method Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
There was a problem hiding this 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
…sal issues in EnsureAspNetCoreHttpsDevelopmentCertificate Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com>
Fixed memory leaks in the
CertificateManager
class whereX509Certificate2
objects were not being properly disposed in several code paths, as identified by static analysis.Issues Fixed
ImportCertificate method:
SaveCertificate
threw an exceptionEnsureAspNetCoreHttpsDevelopmentCertificate method:
Changes 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
SaveCertificate
back 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.
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.