Skip to content

Commit

Permalink
Ensure Parallel.ForAsync unregisters from CancellationToken (#93672)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Oct 18, 2023
1 parent d3bf287 commit fa3babd
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ static bool CompareExchange(ref T location, T value, T comparand) =>
// If we're the last worker to complete, complete the operation.
if (state.SignalWorkerCompletedIterating())
{
state.Dispose();
state.Complete();
}
}
Expand Down Expand Up @@ -745,7 +746,7 @@ public ValueTask DisposeAsync()

/// <summary>Stores the state associated with an IAsyncEnumerable ForEachAsync operation, shared between all its workers.</summary>
/// <typeparam name="T">Specifies the type of data being enumerated.</typeparam>
private sealed class ForEachState<T> : ForEachAsyncState<T>
private sealed class ForEachState<T> : ForEachAsyncState<T>, IDisposable
{
public T NextAvailable;
public readonly T ToExclusive;
Expand All @@ -759,6 +760,8 @@ public ForEachState(
NextAvailable = fromExclusive;
ToExclusive = toExclusive;
}

public void Dispose() => _registration.Dispose();
}
}
}

0 comments on commit fa3babd

Please sign in to comment.