Skip to content

Commit a816d52

Browse files
committed
fixup! Fix race condition in RequestExecutionQueue
1 parent 5f0c467 commit a816d52

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/Features/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/RequestExecutionQueue.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private async Task ProcessQueueAsync()
233233
await Task.WhenAll(concurrentlyExecutingTasksArray.Select(kvp => kvp.Key)).NoThrowAwaitable(captureContext: false);
234234
}
235235

236-
Debug.Assert(!concurrentlyExecutingTasks.Any(), "The tasks should have all been drained before continuing");
236+
Debug.Assert(!concurrentlyExecutingTasks.Any(t => !t.Key.IsCompleted), "The tasks should have all been drained before continuing");
237237
// Mutating requests block other requests from starting to ensure an up to date snapshot is used.
238238
// Since we're explicitly awaiting exceptions to mutating requests will bubble up here.
239239
await WrapStartRequestTaskAsync(work.StartRequestAsync(context, cancellationToken), rethrowExceptions: true).ConfigureAwait(false);
@@ -254,21 +254,20 @@ private async Task ProcessQueueAsync()
254254
throw new InvalidOperationException($"unexpected null value for {nameof(currentWorkCts)}");
255255
}
256256

257-
// Only mark the task complete when it has removed itself from the executing task list.
258-
currentWorkTask = currentWorkTask.ContinueWith(t =>
257+
if (!concurrentlyExecutingTasks.TryAdd(currentWorkTask, currentWorkCts))
258+
{
259+
throw new InvalidOperationException($"unable to add {nameof(currentWorkTask)} into {nameof(concurrentlyExecutingTasks)}");
260+
}
261+
262+
_ = currentWorkTask.ContinueWith(t =>
259263
{
260-
if (!concurrentlyExecutingTasks.TryRemove(currentWorkTask, out var concurrentlyExecutingTaskCts))
264+
if (!concurrentlyExecutingTasks.TryRemove(t, out var concurrentlyExecutingTaskCts))
261265
{
262266
throw new InvalidOperationException($"unexpected failure to remove task from {nameof(concurrentlyExecutingTasks)}");
263267
}
264268

265269
concurrentlyExecutingTaskCts.Dispose();
266270
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
267-
268-
if (!concurrentlyExecutingTasks.TryAdd(currentWorkTask, currentWorkCts))
269-
{
270-
throw new InvalidOperationException($"unable to add {nameof(currentWorkTask)} into {nameof(concurrentlyExecutingTasks)}");
271-
}
272271
}
273272
}
274273
}

0 commit comments

Comments
 (0)