dotnet / dotnet/wcf

Memory leak in single InstanceContext, WmiChannels property

Open
#5,171 8 comments 0 reactions 2 assignees Assigned to @mconnew View on GitHub
triaged
Dominant language
C#
Stars
1.8k
Forks
576
Avg merge
6d 9h
Merged PRs (30d)
2

Description

**Describe the bug**
The `System.ServiceModel.InstanceContext` type has an internal `WmiChannels` property, which holds a list of `IChannel` instances.
We observed increased memory usage, and upon analysis of memory dump, we found about 50gb of memory retained by that list. All observed channels are in faulted state, and our application automatically recycles a channel when it detects that a channel became faulty. Browsing the code in this repository, it appears that the list only grows. Channels are added in [this line of code](https://github.com/dotnet/wcf/blob/2b8970208eb739ce50dfdc74f2bd0e5f83e17a6d/src/System.ServiceModel.Primitives/src/System/ServiceModel/Channels/ServiceChannelFactory.cs#L357), but I could not find any place where channels are removed.

**To Reproduce**
1. Create a service that uses a single `InstanceContext` for the entire application, as documented [here](https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/sessions-instancing-and-concurrency).
2. Create some WCF endpoint that times out whenever it receives a call (make it sleep for 10s). This is required to put the client channel in faulted state.
3. Create an infinite loop that creates a channel with a client timeout of 2s, calls the above endpoint it and ignores the timeout exception. Make sure your channel creation options trigger [this line of code](https://github.com/dotnet/wcf/blob/2b8970208eb739ce50dfdc74f2bd0e5f83e17a6d/src/System.ServiceModel.Primitives/src/System/ServiceModel/Channels/ServiceChannelFactory.cs#L357).

The bug should repro even if the faulted channel is disposed. Just make sure the reference is cleared (i.e. reuse the reference on each iteration of the loop).

**Expected behavior**
As the loop executes, memory usage should eventually stabilize. It must not grow forever.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.