You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ConcurrentBag is an unordered collection therefore attempting to remove spesific item can cause infinite loop and ConcurrentBag<>.TryTake can return null value.
var bag = new ConcurrentBag<string>();
bag.Add("hi");
bag.Add("123");
var time = DateTime.Now;
// works
Console.WriteLine($"Attempting to remove item: {bag.First()}");
Remove(bag, bag.ElementAt(0));
Console.WriteLine($"Deltatime is : {(DateTime.Now - time).TotalMilliseconds}ms");
// fails
Console.WriteLine($"Attempting to remove item: {bag.First()}");
Remove(bag, bag.ElementAt(1));
Console.WriteLine($"Deltatime is : {(DateTime.Now - time).TotalMilliseconds}ms");
void Remove<T>(ConcurrentBag<T> bag, T item)
{
while (bag.Count > 0)
{
bag.TryTake(out T result);
if (result.Equals(item))
{
break;
}
bag.Add(result);
}
}
The text was updated successfully, but these errors were encountered:
Toemsel
added
the
bug
Marks an issue as a critical bug that needs to be fixed ASAP.
label
Mar 27, 2023
ConcurrentBag is an unordered collection therefore attempting to remove spesific item can cause infinite loop and ConcurrentBag<>.TryTake can return null value.
Network/Network/Extensions/ConcurrentBagExtensions.cs
Lines 21 to 34 in a3201d9
Test case (.NET 6.0);
The text was updated successfully, but these errors were encountered: