Skip to content

Commit

Permalink
Fix AsyncLocal in Framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
timcassell committed Nov 6, 2023
1 parent 7b53797 commit 966b309
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Package/Core/Promises/Internal/ParallelInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ private void ExecuteWorkerAndLaunchNext()
}
else
{
ExecutionContext.Run(_executionContext, obj => obj.UnsafeAs<PromiseParallelForEach<TEnumerator, TParallelBody, TSource>>().ExecuteWorker(true), this);
// .Net Framework doesn't allow us to re-use a captured context, so we have to copy it for each invocation.
// .Net Core's implementation of CreateCopy returns itself, so this is always as efficient as it can be.
ExecutionContext.Run(_executionContext.CreateCopy(), obj => obj.UnsafeAs<PromiseParallelForEach<TEnumerator, TParallelBody, TSource>>().ExecuteWorker(true), this);
}
}

Expand All @@ -315,7 +317,9 @@ private void ExecuteWorkerWithoutLaunchNext()
}
else
{
ExecutionContext.Run(_executionContext, obj => obj.UnsafeAs<PromiseParallelForEach<TEnumerator, TParallelBody, TSource>>().ExecuteWorker(false), this);
// .Net Framework doesn't allow us to re-use a captured context, so we have to copy it for each invocation.
// .Net Core's implementation of CreateCopy returns itself, so this is always as efficient as it can be.
ExecutionContext.Run(_executionContext.CreateCopy(), obj => obj.UnsafeAs<PromiseParallelForEach<TEnumerator, TParallelBody, TSource>>().ExecuteWorker(false), this);
}
}

Expand Down

0 comments on commit 966b309

Please sign in to comment.