Skip to content

Possible leaks in AsyncLock #142

@RyanYANG52

Description

@RyanYANG52

Hi, @kekyo , I'm looking through your code,

ct.Register(() => tcs.TrySetCanceled());

here it registered a cancel callback, but it never dispose even when lock is acquired, ct would hold on to all the tcs until it's cancelled. In my case I create one CancellationTokenSource, only Cancel when the app is closed, if any race condition happen it would add a registration to the linked list inside cts

maybe add something like this:

        if (ct != default)
        {
            var registration = ct.Register(() => tcs.TrySetCanceled());
            tcs.Task.ContinueWith(_ => registration.Dispose(), TaskContinuationOptions.ExecuteSynchronously); // dispose registration after task completion
        }

I see it was done in AsyncEx lib

https://github.com/StephenCleary/AsyncEx/blob/master/src/Nito.AsyncEx.Coordination/AsyncWaitQueue.cs#L82

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions