Fix HostingListener memory leak from static AsyncLocal#65426
Fix HostingListener memory leak from static AsyncLocal#65426
Conversation
Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
|
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 |
Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
Removed the test. The PR now contains only the one-line change removing the |
There was a problem hiding this comment.
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._currentListenerfrom astatic readonly AsyncLocal<HostingListener>to an instancereadonly AsyncLocal<HostingListener>.
|
I'm going to close this based on the discussion at dotnet/runtime#124391 (comment) |
Fix memory leak in WebApplicationTests HostingListener
Description
HostingListener._currentListenerwas declared asstatic readonly AsyncLocal<HostingListener>, causing the static AsyncLocal instance to retain references to HostingListener instances beyond their disposal, preventing garbage collection.Changes:
_currentListeneran 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.