infobip / infobip-api-csharp-client

Infobip API client library in C#, distributed as a NuGet package.
https://www.infobip.com/docs/api
MIT License
11 stars 17 forks source link

Fix memory leak #20

Closed m0j0 closed 1 year ago

m0j0 commented 1 year ago

If cancellation token used for many request (for example, long living queue consumer), memory will leak because LinkedCancellationTokenSource is not disposed.

Code to reproduce:

using Infobip.Api.Client.Api;
using Infobip.Api.Client;
using Infobip.Api.Client.Model;

var configuration = new Configuration()
{
    BasePath = "https://api.infobip.com",
    UserAgent = "Test",
    Username = "",
    Password = ""
};

var client = new SendSmsApi(configuration);

var smsRequest = new SmsAdvancedTextualRequest
{
    Messages = new List<SmsTextualMessage>
    {
        new()
        {
            From = "From",
            Destinations = new List<SmsDestination>
            {
                new(to: "79112223344")
            },
            Text = "Text"
        }
    }
};

var cts = new CancellationTokenSource();

for (int i = 0; ; i++)
{
    try
    {
        var smsResponse = await client.SendSmsMessageAsync(smsRequest, cts.Token);
    }
    catch
    {
    }
}

Before changes after ~5 minutes of work: Screenshot 2022-12-09 165914

After: after

m0j0 commented 1 year ago

Hi @mateanticevic, could you please upload new version to nuget.org?

mateanticevic commented 1 year ago

Sure, will do it tomorrow.

mateanticevic commented 1 year ago

Hi @m0j0, we've published a new version of the package on nuget.org. Thanks for your contribution.

m0j0 commented 1 year ago

@mateanticevic thank you!