Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9239635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:02:04+00:00 2026-06-18T08:02:04+00:00

I have a .NET (C#) application that makes extensive use of async/await. I feel

  • 0

I have a .NET (C#) application that makes extensive use of async/await. I feel like I’ve got my head around async/await, but I’m trying to use a library (RestSharp) that has an older (or perhaps I should just say different) programming model that uses callbacks for asynchronous operations.

RestSharp’s RestClient class has an ExecuteAsync method that takes a callback parameter, and I wanted to be able to put a wrapper around that which would allow me to await the whole operation. The ExecuteAsync method looks something like this:

public RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse> callback);

I thought I had it all working nicely. I used TaskCompletionSource to wrap the ExecuteAsync call in something that I could await, as follows:

public static async Task<T> ExecuteRequestAsync<T>(RestRequest request, CancellationToken cancellationToken) where T : new()
{
    var response = await ExecuteTaskAsync(request, cancellationToken);

    cancellationToken.ThrowIfCancellationRequested();

    return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(response.Content);
}

private static async Task<IRestResponse> ExecuteTaskAsync(RestRequest request, CancellationToken cancellationToken)
{
    var taskCompletionSource = new TaskCompletionSource<IRestResponse>();

    var asyncHandle = _restClient.ExecuteAsync(request, r => 
    {
        taskCompletionSource.SetResult(r); 
    });

    cancellationToken.Register(() => asyncHandle.Abort());

    return await taskCompletionSource.Task;
}

This has been working fine for most of my application.

However, I have one part of the application that does hundreds of calls to my ExecuteRequestAsync as part of a single operation, and that operation shows a progress dialog with a cancel button. You’ll see that in the code above that I’m passing a CancellationToken to ExecuteRequestAsync; for this long-running operation, the token is associated with a CancellationTokenSource “belonging” to the dialog, whose Cancel method is called if the use clicks the cancel button. So far so good (the cancel button does work).

My problem is that my application’s memory usage shoots up during the long-running application, to the extent that it runs out of memory before the operation completes.

I’ve run a memory profiler on it, and discovered that I have lots of RestResponse objects still in memory, even after garbage collection. (They in turn have huge amounts of data, because I’m sending multi-megabyte files across the wire).

According to the profiler, those RestResponse objects are being kept alive because they’re referred to by the TaskCompletionSource (via the Task), which in turn is being kept alive because it’s referenced from the CancellationTokenSource, via its list of registered callbacks.

From all this, I gather that registering the cancellation callback for each request means that the whole graph of objects that is associated with all those requests will live on until the entire operation is completed. No wonder it runs out of memory 🙂

So I guess my question is not so much “why does it leak”, but “how do I stop it”. I can’t un-register the callback, so what can I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:02:05+00:00Added an answer on June 18, 2026 at 8:02 am

    I can’t un-register the callback

    Actually, you can. The return value of Register() is:

    The CancellationTokenRegistration instance that can be used to deregister the callback.

    To actually deregister the callback, call Dispose() on the returned value.

    In your case, you could do it like this:

    private static async Task<IRestResponse> ExecuteTaskAsync(
        RestRequest request, CancellationToken cancellationToken)
    {
        var taskCompletionSource = new TaskCompletionSource<IRestResponse>();
    
        var asyncHandle = _restClient.ExecuteAsync(
            request, r => taskCompletionSource.SetResult(r));
    
        using (cancellationToken.Register(() => asyncHandle.Abort()))
        {
            return await taskCompletionSource.Task;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET application that makes extensive use of ASP.NET cache API for
We have a .net application that we didn't develop, but use. I can tell
I have a .NET MVC application which makes extensive use of AJAX and modal
I have an ASP .NET Web Forms application that makes use of the 'WebMethod'
I have a .NET application that I want to use as a client to
I have an ASP.NET MVC web application that makes REST style web service calls
I have a .NET application that makes calls to a native Win32 DLL using
I have an asp.net web application that makes calls to several WCF services. The
I have .NET application that is hosted on IIS 6.1 It is impossible to
I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.