BlockingCollection leaks the last seen enumerated value
Description
When using GetConsumingEnumerable the last element added to the collection will be hold indefinitely even though we consumed all elements from the collection and should release all references to it.
For elements that are quite big and holds lots of other references and arrays, that behavior could impact memory consumption, as GC would never* release these objects and memory
(*) until next element comes
Reproduction Steps
var bc = new BlockingCollection<Rec>();
var t = new Thread(
() =>
{
foreach (var item in bc.GetConsumingEnumerable())
{
OnNextRec(item);
}
})
{
IsBackground = true,
};
t.Start();
Log("Producing...");
Produce(bc, 1);
Produce(bc, 2);
Produce(bc, 3);
Produce(bc, 4);
Log("Wait...");
Thread.Sleep(1000);
Console.ReadLine(); // take memory dump
GC.KeepAlive(t);
static void Produce(BlockingCollection<Rec> bc, int value)
{
bc.Add(new Rec(value));
}
static void OnNextRec(Rec rec)
{
Log(rec.ToString());
}
record Rec(int Val);
Expected behavior
There should be no Rec references in the memory, as all elements were consumed.
Actual behavior
There will be always one reference to Rec left... always the last element that was added/consumed.
Regression?
No response
Known Workarounds
No.
The only workaround would be not to use yield return pattern.
Configuration
No response
Other information
The underlying issue lies with how the yield return pattern is lowered.
As the value of the last seen element will be stored in the <>2__current field... but will never be replaced or cleared before new loop come around and blocks the blockingCollection.TryTakeWithNoTimeValidation.
Issue was reported to the https://github.com/dotnet/roslyn/issues/77131, but the team is not looking forward to fix the underlying issue...
Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.
This is not actionable at library side. It has to depend on the compiler behavior of yield return.
The example is not valid either. There's no GC run before the snapshot. Everything used, especially the IEnumerator<T> were still left there, reported as references.
There's no GC run before the snapshot.
Depends how you will take a snapshot. I used the dotMemory, which is doing GC.Collect before doing snapshot. Didn't want to put GC.Collect or finalizers to illustrate the issue, as that is "not real example" (not how the real world service would behave).
Everything used, especially the
IEnumerator<T>were still left there, reported as references.
That will be there, as there is still thread running and trying to consume the collection. So that is to be expected.
The issue is that there should not be any references to the Rec objects... because all of them should "run out of scope".
But one will be hold by the <GetConsumingEnumerable>d__68.<>2__current.
This is not actionable at library side. It has to depend on the compiler behavior of
yield return.
I know... that's why I was first reported it in https://github.com/dotnet/roslyn/issues/77131, but the team is reluctant to fix it.
So the only way for the BlockingCollection to not hold the memory is for library side to escalate and deal with the bug, or just not use the yield return pattern in case of GetConsumingEnumerable method...
So the only way for the
BlockingCollectionto not hold the memory is for library side to escalate and deal with the bug
This is not actionable at all. The code isn't doing anything about the reference. The only choice is to not use yield return, which is unwanted.
The real problem here is that BlockingCollection.GetConsumingEnumerable is a very unusual usage of IEnumerable<T>. Unlike IAsyncEnumerable<T>, IEnumerable<T> is usually non-blocking at each access. Introducing zeroing will regress performance for all usual usages.
IAsyncEnumerable<> is relatively "new", since dawn of time (C# 2) we had the yield statement and this was the only way to do "async evaluation".
Don't know if that is unusual usage, as I've seen this pattern use few times...
Agree, use of IAsyncEnumerable<> would be probably better, but as it was stated in other topic, that would not happen as by design this class should block, and alternative is to use Channels...
So basically it would render GetConsumingEnumerable method unusable - as it's known to have bug, but there is no way it will be fixed (as it cannot be fixed in the lowered operation, we don't want to get rig of yield return which is the cause of the bug), and no async alternative - as it's by design...
If you are not planning to fix it (which would be sad), at least we should put the waring in the documentation that the implementation of GetConsumingEnumerable is leaking the memory, and consumers should use it with cautious - especially if the they will be waiting for the elements for long time.
@eiriktsarpalis Why was the ticket closed?
If that ticket is closed as "not planned", would at least documentation could be change to contain the warning about potential memory leaks?
This issue cause us a lot of pain, and it was tricky to find - so at least we could warn others if they will come across it and they will not have to spend too much time on this...