Skip to content

Commit

Permalink
Suppress flow when queueing background threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeegaan authored and bergmania committed Dec 11, 2023
1 parent cb76554 commit b5544aa
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ public virtual void RebuildIndex(string indexName, TimeSpan? delay = null, bool
_logger.LogInformation("Starting async background thread for rebuilding index {indexName}.", indexName);

_backgroundTaskQueue.QueueBackgroundWorkItem(
cancellationToken => Task.Run(() => RebuildIndex(indexName, delay.Value, cancellationToken)));
cancellationToken =>
{
// Do not flow AsyncLocal to the child thread
using (ExecutionContext.SuppressFlow())
{
Task.Run(() => RebuildIndex(indexName, delay.Value, cancellationToken));

// immediately return so the queue isn't waiting.
return Task.CompletedTask;
}
});
}
else
{
Expand All @@ -93,12 +103,16 @@ public virtual void RebuildIndexes(bool onlyEmptyIndexes, TimeSpan? delay = null
_backgroundTaskQueue.QueueBackgroundWorkItem(
cancellationToken =>
{
// This is a fire/forget task spawned by the background thread queue (which means we
// don't need to worry about ExecutionContext flowing).
Task.Run(() => RebuildIndexes(onlyEmptyIndexes, delay.Value, cancellationToken));
// Do not flow AsyncLocal to the child thread
using (ExecutionContext.SuppressFlow())
{
// This is a fire/forget task spawned by the background thread queue (which means we
// don't need to worry about ExecutionContext flowing).
Task.Run(() => RebuildIndexes(onlyEmptyIndexes, delay.Value, cancellationToken));

// immediately return so the queue isn't waiting.
return Task.CompletedTask;
// immediately return so the queue isn't waiting.
return Task.CompletedTask;
}
});
}
else
Expand Down

0 comments on commit b5544aa

Please sign in to comment.