Skip to content

Fix HostingListener memory leak from static AsyncLocal#65426

Closed
Copilot wants to merge 4 commits intomainfrom
copilot/fix-memory-leak-async-local
Closed

Fix HostingListener memory leak from static AsyncLocal#65426
Copilot wants to merge 4 commits intomainfrom
copilot/fix-memory-leak-async-local

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Fix memory leak in WebApplicationTests HostingListener

Description

HostingListener._currentListener was declared as static readonly AsyncLocal<HostingListener>, causing the static AsyncLocal instance to retain references to HostingListener instances beyond their disposal, preventing garbage collection.

Changes:

  • Made _currentListener an instance field instead of static (one-line change)

The instance-level AsyncLocal correctly scopes to each listener and gets collected with its owner. The check if (_currentListener.Value != this) remains functionally correct as each instance now validates against its own AsyncLocal.

Note: This fix applies to internal test-only code. The production code fix is tracked in dotnet/runtime#124391.


💡 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 AI and others added 2 commits February 13, 2026 16:59
Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Feb 13, 2026
Copilot AI changed the title [WIP] Fix memory leak caused by static AsyncLocal Fix HostingListener memory leak from static AsyncLocal Feb 13, 2026
Copilot AI requested a review from halter73 February 13, 2026 17:13
@halter73
Copy link
Member

halter73 commented Feb 13, 2026

I forgot the real code for this is in the runtime repo, and we only use the AsyncLocal modified by this PR for our own internal tests. The real PR to fix this is now at dotnet/runtime#124391.

There's no reason not to fix this for our own internal usage, but I don't think we need to test our internal-test-only code. @copilot Can you remove the HostingListener_DoesNotLeakMemory test? This PR should just be a one-line change removing the static keyword.

Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

I forgot the real code for this is in the runtime repo, and we only use the AsyncLocal modified by this PR for our own internal tests. The real PR to fix this is now at https://github.com/dotnet/runti...

Removed the test. The PR now contains only the one-line change removing the static keyword (commit a08916f).

@halter73 halter73 marked this pull request as ready for review February 13, 2026 20:06
Copilot AI review requested due to automatic review settings February 13, 2026 20:06
@halter73 halter73 enabled auto-merge (squash) February 13, 2026 20:06
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 addresses a memory retention issue in the DefaultBuilder test infrastructure by avoiding a static AsyncLocal that could keep HostingListener instances alive longer than intended.

Changes:

  • Changed HostingListener._currentListener from a static readonly AsyncLocal<HostingListener> to an instance readonly AsyncLocal<HostingListener>.

@halter73
Copy link
Member

I'm going to close this based on the discussion at dotnet/runtime#124391 (comment)

@halter73 halter73 closed this Feb 14, 2026
auto-merge was automatically disabled February 14, 2026 01:03

Pull request was closed

@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview2 milestone Feb 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants