Skip to content

Fix memory leak in IIS StartupHook by properly disposing PhysicalFileProvider #63322

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

Merged
merged 2 commits into from
Aug 19, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 18, 2025

The SVACE static analyzer identified a potential handle leak in the IIS StartupHook class where a PhysicalFileProvider was created but not properly disposed.

Problem

In src/Servers/IIS/IIS/src/StartupHook.cs, the unhandled exception handler creates a PhysicalFileProvider to generate error page content but doesn't dispose it:

var model = ErrorPageModelBuilder.CreateErrorPageModel(
    new PhysicalFileProvider(contentRoot), // ❌ Not disposed
    logger: null,
    showDetailedErrors: true,
    exception);

Since PhysicalFileProvider implements IDisposable and manages file system resources, this could lead to handle leaks.

Solution

Wrapped the PhysicalFileProvider creation in a using statement to ensure proper disposal:

using var fileProvider = new PhysicalFileProvider(contentRoot);

var model = ErrorPageModelBuilder.CreateErrorPageModel(
    fileProvider, // ✅ Properly disposed at end of scope
    logger: null,
    showDetailedErrors: true,
    exception);

This approach follows existing patterns in the codebase where PhysicalFileProvider is used with using statements in similar scenarios (e.g., in diagnostic and static file middleware tests).

The fix is safe because ErrorPageModelBuilder.CreateErrorPageModel() only uses the file provider during its execution and doesn't retain any references to it.

Fixes #59524.


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

@Copilot Copilot AI changed the title [WIP] Possible memory leak in IIS when StartupHook initializes Fix memory leak in IIS StartupHook by properly disposing PhysicalFileProvider Aug 18, 2025
@Copilot Copilot AI requested a review from danmoseley August 18, 2025 23:39
Copilot finished work on behalf of danmoseley August 18, 2025 23:39
@danmoseley
Copy link
Member

@BrennanConroy any opinion on whether ErrorPageModelBuilder.CreateErrorPageModel() ought to do the dispose here? Indeed it is not used after that call so the fix as is is reasonable.

@BrennanConroy
Copy link
Member

The CreateErrorPageModel method shouldn't dispose the file provider. It's used by other code that might continue using the file provider later.

@danmoseley
Copy link
Member

OK. Are you comfortable with the dispose in this particular context? ie this change.

@danmoseley danmoseley marked this pull request as ready for review August 19, 2025 00:29
@Copilot Copilot AI review requested due to automatic review settings August 19, 2025 00:29
@danmoseley danmoseley enabled auto-merge (squash) August 19, 2025 00:29
Copy link
Contributor

@Copilot 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 potential memory leak in the IIS StartupHook class by ensuring proper disposal of a PhysicalFileProvider instance used in unhandled exception handling.

  • Wrapped PhysicalFileProvider creation in a using statement to ensure automatic disposal
  • Replaced inline instantiation with a properly scoped variable reference
  • Applied existing codebase patterns for resource management with file providers

@@ -49,8 +49,10 @@ public static void Initialize()
var iisConfigData = NativeMethods.HttpGetApplicationProperties();
var contentRoot = iisConfigData.pwzFullApplicationPath.TrimEnd(Path.DirectorySeparatorChar);

using var fileProvider = new PhysicalFileProvider(contentRoot);
Copy link
Preview

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

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

Consider adding a blank line after the using statement declaration to improve readability and follow the formatting guidelines that specify inserting newlines before code blocks.

Copilot uses AI. Check for mistakes.

@danmoseley danmoseley merged commit 1939339 into main Aug 19, 2025
29 checks passed
@danmoseley danmoseley deleted the copilot/fix-59524 branch August 19, 2025 17:57
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible memory leak in IIS when StartupHook initializes
3 participants