FubarDevelopment / FtpServer

Portable FTP server written in .NET
http://fubardevelopment.github.io/FtpServer/
MIT License
472 stars 161 forks source link

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

Open davideberli opened 1 year ago

davideberli commented 1 year ago

Task.Delay(-1, token) is creating a Task for every client that is running forever: https://github.com/FubarDevelopment/FtpServer/blob/edfe7a7b2ad4fc7bc98cead9b80880300ad3c716/src/FubarDev.FtpServer/MultiBindingTcpListener.cs#L121

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();
}
michi927 commented 1 year ago

An update with a fix for this memory leak whould be very nice. I occur the same issue...

Is there already a date when this package gets updated @fubar-coder ? :)

AndreasKue commented 1 year ago

Push

LodiAleardo commented 10 months ago

U P 💯