Skip to content

Commit

Permalink
Dispose cancellation token source (#51577)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam Harwell <sam@tunnelvisionlabs.com>
  • Loading branch information
davidwengier and sharwell authored Mar 2, 2021
1 parent 38d5805 commit 4eee999
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,12 @@ private async Task ProcessQueueAsync()
{
var work = await _queue.DequeueAsync().ConfigureAwait(false);

// Create a linked cancellation token to cancel any requests in progress when this shuts down
var cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(_cancelSource.Token, work.CancellationToken).Token;

var context = CreateRequestContext(work, out var workspace);

if (work.MutatesSolutionState)
{
// Mutating requests block other requests from starting to ensure an up to date snapshot is used.
await work.CallbackAsync(context, cancellationToken).ConfigureAwait(false);
await ExecuteCallbackAsync(work, context, _cancelSource.Token).ConfigureAwait(false);

// Now that we've mutated our solution, clear out our saved state to ensure it gets recalculated
_lspSolutionCache.Remove(workspace);
Expand All @@ -214,7 +211,7 @@ private async Task ProcessQueueAsync()
// Non mutating are fire-and-forget because they are by definition readonly. Any errors
// will be sent back to the client but we can still capture errors in queue processing
// via NFW, though these errors don't put us into a bad state as far as the rest of the queue goes.
_ = work.CallbackAsync(context, cancellationToken).ReportNonFatalErrorAsync();
_ = ExecuteCallbackAsync(work, context, _cancelSource.Token).ReportNonFatalErrorAsync();
}
}
}
Expand All @@ -229,6 +226,14 @@ private async Task ProcessQueueAsync()
}
}

private static async Task ExecuteCallbackAsync(QueueItem work, RequestContext context, CancellationToken queueCancellationToken)
{
// Create a combined cancellation token to cancel any requests in progress when this shuts down
using var combinedTokenSource = queueCancellationToken.CombineWith(work.CancellationToken);

await work.CallbackAsync(context, combinedTokenSource.Token).ConfigureAwait(false);
}

private void OnRequestServerShutdown(string message)
{
RequestServerShutdown?.Invoke(this, new RequestShutdownEventArgs(message));
Expand Down

0 comments on commit 4eee999

Please sign in to comment.