Azure / Azure/azure-functions-host
DynamicFunctionAssemblyLoadContext keeps ScriptHosts from GC
- Dominant language
- C#
- Stars
- 2k
- Forks
- 482
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 38
Description
Doing some memory-leak testing and found that the `DyanamicFunctionAssemblyLoadContext` keeps a reference (eventually) to a `ScriptHost`. Since `AssemblyLoadContext`s live for the scope of the process, this means that these ScriptHosts never go away.
We'll need a way to clean up these contexts when a host recycles, or use weak refs to these objects.
Repro:
- In VS, Debug WebHost locally, with Diagnostic Tools running. Point root to the CSharp sample.
- Touch `host.json` file several times, which will cause restarts.
- Take snapshot.
- In snapshot, look for `ScriptHost` instances.
Here's a sample root:

Contributor guide
Research direction
Start by locating DynamicFunctionAssemblyLoadContext and tracing how it retains ScriptHost instances. Reproduce the leak by running the WebHost against the CSharp sample, touching host.json several times, and inspecting Diagnostic Tools snapshots. Done means recycled ScriptHosts are no longer retained by these contexts and can be garbage-collected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100