Skip to content

Commit f821abb

Browse files
committed
Fix compilation error, add a lock because GetOrAdd is not atomic.
1 parent 1b628e3 commit f821abb

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

projects/RabbitMQ.Client/client/impl/AsyncConsumerWorkService.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,29 @@ namespace RabbitMQ.Client.Impl
99
internal sealed class AsyncConsumerWorkService : ConsumerWorkService
1010
{
1111
private readonly ConcurrentDictionary<IModel, WorkPool> _workPools;
12-
private readonly Func<IModel, WorkPool> _startNewWorkPoolAction;
12+
private readonly Func<IModel, WorkPool> _startNewWorkPoolFunc;
1313

1414
public AsyncConsumerWorkService()
1515
{
1616
_workPools = new ConcurrentDictionary<IModel, WorkPool>();
17-
_startNewWorkPoolAction = model => StartNewWorkPool(model);
17+
_startNewWorkPoolFunc = model => StartNewWorkPool(model);
1818
}
1919

2020
public void Schedule<TWork>(ModelBase model, TWork work) where TWork : Work
2121
{
22-
_workPools.GetOrAdd(model, _startNewWorkPoolAction).Enqueue(work);
22+
/*
23+
* rabbitmq/rabbitmq-dotnet-client#841
24+
* https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd
25+
*
26+
* The lock is necessary because calling the value delegate is not atomic.
27+
*/
28+
lock (_workPools)
29+
{
30+
_workPools.GetOrAdd(model, _startNewWorkPoolFunc).Enqueue(work);
31+
}
2332
}
2433

25-
private WorkPool StartNewWorkPool(IModel model)
34+
private static WorkPool StartNewWorkPool(IModel model)
2635
{
2736
var newWorkPool = new WorkPool(model as ModelBase);
2837
newWorkPool.Start();

0 commit comments

Comments
 (0)