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
A possible race condition exists in the ModelObjectPool implementation when multiple threads concurrently call the Return method. The issue arises due to the non-atomic _availableObjects.Count check and the subsequent _availableObjects.Add().
In my specific use case, this issue has resulted in RabbitMQ channel leaks.
I fixed it by replacing the _availableObjects.Count logic with an atomic counter (_currentCount) managed using Interlocked. Here's the sample:
private int _currentCount = 0;
public void Return(IModel model)
{
if (Interlocked.Increment(ref _currentCount) > _maxEntries)
{
Interlocked.Decrement(ref _currentCount);
model.SafeDrop();
}
else
{
_availableObjects.Add(model);
}
}
public IModel Get()
{
if (_availableObjects.TryTake(out var model))
{
Interlocked.Decrement(ref _currentCount);
return model;
}
return _policy.Create();
}
The text was updated successfully, but these errors were encountered:
Oh! I appreciate your report and the fact that you submitted the code.... but how about submitting it as a PR? Then you'll get your name in the list of commits 🤠 and you will be a Rebus contributor 4ever 🤩
A possible race condition exists in the ModelObjectPool implementation when multiple threads concurrently call the Return method. The issue arises due to the non-atomic _availableObjects.Count check and the subsequent _availableObjects.Add().
In my specific use case, this issue has resulted in RabbitMQ channel leaks.
I fixed it by replacing the _availableObjects.Count logic with an atomic counter (_currentCount) managed using Interlocked. Here's the sample:
private int _currentCount = 0;
The text was updated successfully, but these errors were encountered: