Skip to content

Memory leak in RestClient.ExecuteInternal #1685

Closed
@DanWatkins

Description

@DanWatkins

There appears to be a small memory leak in RestClient.ExecuteInternal caused by the following code:

var timeoutCts = new CancellationTokenSource(request.Timeout > 0 ? request.Timeout : int.MaxValue);
var cts        = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);

CancellationTokenSource does not have a finalizer, so my understanding is it needs to be explicitly disposed. I have made a custom build of RestSharp that adds using statements as shown below. This change fixes the leak.

using var timeoutCts = new CancellationTokenSource(request.Timeout > 0 ? request.Timeout : int.MaxValue);
using var cts        = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);

My application is making a high volume of requests, so this leak causes the system to run out of memory in about 2 hours.

Expected Behavior

No leaked memory

Actual Behavior

Leaked memory

Steps to Reproduce the Problem

  1. Execute a request using RestClient
  2. Profile using dotMemory by JetBrains

Specifications

  • Version: 107.0.0-preview.17
  • Platform: Windows 10, Ubuntu 20.04 (.NET 6)

Metadata

Metadata

Assignees

No one assigned

    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