Skip to content

Commit c3cc41e

Browse files
authored
Fix DataLoader ThreadPool Starvation (#8827)
1 parent 0922e36 commit c3cc41e

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

src/HotChocolate/Core/src/Fetching/BatchDispatcher.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private async Task EvaluateAndDispatchAsync(
149149

150150
// Spin-wait briefly to give executing resolvers time to add more
151151
// data requirements to the open batches.
152-
WaitForMoreBatchActivity(lastModified);
152+
await WaitForMoreBatchActivityAsync(lastModified);
153153
}
154154
}
155155

@@ -201,9 +201,9 @@ private void EvaluateOpenBatches(
201201
}
202202
}
203203

204-
private void WaitForMoreBatchActivity(long lastModified)
204+
private async Task WaitForMoreBatchActivityAsync(long lastModified)
205205
{
206-
const int maxSpinUs = 12;
206+
const int maxSpinUs = 50;
207207

208208
var lastSubscribed = Volatile.Read(ref _lastSubscribed);
209209
var lastEnqueued = Volatile.Read(ref _lastEnqueued);
@@ -219,19 +219,9 @@ private void WaitForMoreBatchActivity(long lastModified)
219219
}
220220

221221
var ageUs = TicksToUs(Stopwatch.GetTimestamp() - lastModified);
222-
if (ageUs <= maxSpinUs && ThreadPoolHasHeadroom())
222+
if (ageUs <= maxSpinUs)
223223
{
224-
var sw = new SpinWait();
225-
var start = Stopwatch.GetTimestamp();
226-
227-
while (TicksToUs(Stopwatch.GetTimestamp() - start) < maxSpinUs)
228-
{
229-
sw.SpinOnce();
230-
if (sw.Count >= 8)
231-
{
232-
Thread.Yield();
233-
}
234-
}
224+
await Task.Yield();
235225
}
236226
}
237227

0 commit comments

Comments
 (0)