Skip to content

Parallel.ForAsync memory leak when used with a CancellationToken #93664

@antoniofreire

Description

@antoniofreire

Description

When calling Parallel.ForAsync overload with cancellation support in a long running loop, the memory usage grows indefinitely.

Reproduction Steps

using CancellationTokenSource cts = new();

var cancellationToken = cts.Token;

while (true)
{
    await Parallel.ForAsync(0, 10, cancellationToken,  (_, _) => ValueTask.CompletedTask);
}

Expected behavior

No memory leak

Actual behavior

Memory usage grows indefinitely, with lots of objects going to gen2.

Regression?

No response

Known Workarounds

Don't pass a CancellationToken or periodically cancels it

Configuration

.NET 8.0.100-rc.2.23479.10, Windows x64

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions