Skip to content

Commit fa3babd

Browse files
authored
Ensure Parallel.ForAsync unregisters from CancellationToken (#93672)
1 parent d3bf287 commit fa3babd

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ static bool CompareExchange(ref T location, T value, T comparand) =>
186186
// If we're the last worker to complete, complete the operation.
187187
if (state.SignalWorkerCompletedIterating())
188188
{
189+
state.Dispose();
189190
state.Complete();
190191
}
191192
}
@@ -745,7 +746,7 @@ public ValueTask DisposeAsync()
745746

746747
/// <summary>Stores the state associated with an IAsyncEnumerable ForEachAsync operation, shared between all its workers.</summary>
747748
/// <typeparam name="T">Specifies the type of data being enumerated.</typeparam>
748-
private sealed class ForEachState<T> : ForEachAsyncState<T>
749+
private sealed class ForEachState<T> : ForEachAsyncState<T>, IDisposable
749750
{
750751
public T NextAvailable;
751752
public readonly T ToExclusive;
@@ -759,6 +760,8 @@ public ForEachState(
759760
NextAvailable = fromExclusive;
760761
ToExclusive = toExclusive;
761762
}
763+
764+
public void Dispose() => _registration.Dispose();
762765
}
763766
}
764767
}

0 commit comments

Comments
 (0)