Skip to content

Commit c8eff71

Browse files
authored
Fix integer increment race condition (#6389)
1 parent 029a0f7 commit c8eff71

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

tests/Aspire.Hosting.Tests/Eventing/DistributedApplicationBuilderEventingTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public async Task EventsCanBePublishedBlockSequential()
2323
builder.Eventing.Subscribe<DummyEvent>(async (@event, ct) =>
2424
{
2525
blockAssertionTcs.SetResult();
26-
hitCount++;
26+
Interlocked.Increment(ref hitCount);
2727
await blockFirstSubscriptionTcs.Task;
2828
});
2929

3030
builder.Eventing.Subscribe<DummyEvent>((@event, ct) =>
3131
{
32-
hitCount++;
32+
Interlocked.Increment(ref hitCount);
3333
return Task.CompletedTask;
3434
});
3535

@@ -54,14 +54,14 @@ public async Task EventsCanBePublishedBlockConcurrent()
5454

5555
builder.Eventing.Subscribe<DummyEvent>(async (@event, ct) =>
5656
{
57-
hitCount++;
57+
Interlocked.Increment(ref hitCount);
5858
blockAssertionSub1.SetResult();
5959
await blockSubscriptionCompletion.Task;
6060
});
6161

6262
builder.Eventing.Subscribe<DummyEvent>(async (@event, ct) =>
6363
{
64-
hitCount++;
64+
Interlocked.Increment(ref hitCount);
6565
blockAssertionSub2.SetResult();
6666
await blockSubscriptionCompletion.Task;
6767
});
@@ -87,14 +87,14 @@ public async Task EventsCanBePublishedNonBlockingConcurrent()
8787
builder.Eventing.Subscribe<DummyEvent>(async (@event, ct) =>
8888
{
8989
await blockSubscriptionExecution.Task;
90-
hitCount++;
90+
Interlocked.Increment(ref hitCount);
9191
blockAssertionSub1.SetResult();
9292
});
9393

9494
builder.Eventing.Subscribe<DummyEvent>(async (@event, ct) =>
9595
{
9696
await blockSubscriptionExecution.Task;
97-
hitCount++;
97+
Interlocked.Increment(ref hitCount);
9898
blockAssertionSub2.SetResult();
9999
});
100100

@@ -122,14 +122,14 @@ public async Task EventsCanBePublishedNonBlockingSequential()
122122
{
123123
blockAssert1.SetResult();
124124
await blockEventSub1.Task;
125-
hitCount++;
125+
Interlocked.Increment(ref hitCount);
126126
blockAssert2.SetResult();
127127
await blockEventSub2.Task;
128128
});
129129

130130
builder.Eventing.Subscribe<DummyEvent>((@event, ct) =>
131131
{
132-
hitCount++;
132+
Interlocked.Increment(ref hitCount);
133133
blockAssert3.SetResult();
134134
return Task.CompletedTask;
135135
});

tests/Aspire.Hosting.Tests/Health/ResourceHealthCheckServiceTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public async Task PoorlyImplementedHealthChecksDontCauseMonitoringLoopToCrashout
209209
var hitCount = 0;
210210
builder.Services.AddHealthChecks().AddCheck("resource_check", (check) =>
211211
{
212-
hitCount++;
212+
Interlocked.Increment(ref hitCount);
213213
throw new InvalidOperationException("Random failure instead of result!");
214214
});
215215

@@ -250,7 +250,7 @@ public async Task ResourceHealthCheckServiceDoesNotRunHealthChecksUnlessResource
250250
var checkStatus = HealthCheckResult.Unhealthy();
251251
builder.Services.AddHealthChecks().AddCheck("parent_test", () =>
252252
{
253-
hitCount++;
253+
Interlocked.Increment(ref hitCount);
254254
return checkStatus;
255255
});
256256

@@ -314,7 +314,7 @@ public async Task ResourceHealthCheckServiceOnlyRaisesResourceReadyOnce()
314314
var healthCheckHits = 0;
315315
builder.Services.AddHealthChecks().AddCheck("parent_test", () =>
316316
{
317-
healthCheckHits++;
317+
Interlocked.Increment(ref healthCheckHits);
318318
return HealthCheckResult.Healthy();
319319
});
320320

@@ -327,7 +327,7 @@ public async Task ResourceHealthCheckServiceOnlyRaisesResourceReadyOnce()
327327
var resourceReadyEventFired = new TaskCompletionSource<ResourceReadyEvent>();
328328
builder.Eventing.Subscribe<ResourceReadyEvent>(parent.Resource, (@event, ct) =>
329329
{
330-
eventHits++;
330+
Interlocked.Increment(ref eventHits);
331331
return Task.CompletedTask;
332332
});
333333

0 commit comments

Comments
 (0)