OpenAPITools / OpenAPITools/openapi-generator

[BUG][csharp-netcore] Memory leak in ApiClient

Open
#11,344 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • [N/A] Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • [N/A] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

In csharp-netcore, in ApiClient, if the token source is long-lived, then this line is a memory leak due to the un-disposed LinkedTokenSource.

finalToken = CancellationTokenSource.CreateLinkedTokenSource(finalToken, tokenSource.Token).Token;

This has been causing memory leaks on production systems.

The finalToken isn't even used in the method - so this line can be removed, or the code fixed (to ensure the linked token source is disposed)

openapi-generator version

5.3.0

OpenAPI declaration file content or url

N/A

Generation Details

N/A

Steps to reproduce
  • Compile a client
  • Create a method which:
    ** Creates a CancellationTokenSource (var cts = new CancellationTokenSource(); var cancellationToken = cts.Token;)
    ** In a loop, runs / awaits 100 requests in series, passing in the cancellationToken
    ** Stick a break point at the end of the loop
  • Debug the program. At the break point, investigate cts._registrations.Callbacks - this is a linked list. If you follow the Next properties, observe that the linked list is 100 items long, from all the callbacks registered by the linked cancellation token sources.
Related issues/PRs

Can't see any...

Suggest a fix

In these files:

Either remove the whole if (configuration.Timeout > 0) block (as finalToken isn't used anyway!). Or fix it so that finalToken is used instead of cancellationToken later in the method, but be sure to wrap the dispose of the CancellationTokenSource - either via a using block, or via a finally statement with something like linkedCts?.Dispose().

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache and compare the timeout block with samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs. Verify how the linked cancellation source is handled and whether finalToken is used; done means the generated client no longer retains registrations after repeated requests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.