Skip to content

Memory leak because of Task.Delay(-1, token) #149

Description

@davideberli

Task.Delay(-1, token) is creating a Task for every client that is running forever:

var cancellationTask = Task.Delay(-1, token);

A quick and dirty workaround would be this:

private async Task<bool> DelayTask(CancellationToken token1, CancellationToken token2)
{
       while (!token1.IsCancellationRequested && !token2.IsCancellationRequested)
       {
            await Task.Delay(100);
       }

        return true;
}

public async Task<TcpClient> WaitAnyTcpClientAsync(CancellationToken token)
{
      var tokenToDispose = new CancellationTokenSource();
      var cancellationTask = Task.Run(() => DelayTask(token, tokenToDispose.Token));
      
      do
       {
          ...
       }
       while (result == null);
       
       tokenToDispose.Cancel();
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions