Skip to content

Commit 6e46db8

Browse files
authored
Make changing concurrency threadsafe (#1536)
1 parent b14e2fb commit 6e46db8

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/NServiceBus.Transport.SqlServer/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

9696
public async Task ChangeConcurrency(PushRuntimeSettings newLimitations, 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
{
@@ -279,5 +285,7 @@ async Task PurgeExpiredMessages(CancellationToken cancellationToken)
279285
public ISubscriptionManager Subscriptions { get; }
280286
public string Id { get; }
281287
public string ReceiveAddress { get; }
288+
289+
public static object lockObject = new();
282290
}
283291
}

0 commit comments

Comments
 (0)