Skip to content

Commit 53746f5

Browse files
authored
Make changing concurrency threadsafe (#1535)
1 parent e5da1b9 commit 53746f5

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/NServiceBus.Transport.Sql.Shared/Receiving/MessageReceiver.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,17 @@ public Task StartReceive(CancellationToken cancellationToken = default)
9595
public async Task ChangeConcurrency(PushRuntimeSettings newLimitations,
9696
CancellationToken cancellationToken = default)
9797
{
98-
var oldLimiter = concurrencyLimiter;
99-
var oldMaxConcurrency = maxConcurrency;
100-
concurrencyLimiter = new SemaphoreSlim(newLimitations.MaxConcurrency);
101-
limitations = newLimitations;
102-
maxConcurrency = limitations.MaxConcurrency;
98+
SemaphoreSlim oldLimiter;
99+
int oldMaxConcurrency;
100+
101+
lock (lockObject)
102+
{
103+
oldLimiter = concurrencyLimiter;
104+
oldMaxConcurrency = maxConcurrency;
105+
concurrencyLimiter = new SemaphoreSlim(newLimitations.MaxConcurrency);
106+
limitations = newLimitations;
107+
maxConcurrency = limitations.MaxConcurrency;
108+
}
103109

104110
try
105111
{
@@ -274,5 +280,7 @@ await messageProcessingCircuitBreaker.Failure(ex, messageProcessingCancellationT
274280
public ISubscriptionManager Subscriptions { get; }
275281
public string Id { get; }
276282
public string ReceiveAddress { get; }
283+
284+
public static object lockObject = new();
277285
}
278286
}

0 commit comments

Comments
 (0)