Skip to content

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

Open

Description

@dhedey

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().

Activity

  1. wing328 commented on Jan 19, 2022

    @wing328
    Member

    @dhedey thanks for reporting the issue and suggesting 2 possible fixes. Which one do you prefer? Do you mind filing a PR with a fix you prefer to start with?

  2. Havunen commented on Aug 30, 2022

    @Havunen
    Contributor

    Any news about this issue?

  3. dhedey commented on Aug 30, 2022

    @dhedey
    Author

    @Havunen - it was fixed with the merge of this PR: #13049 which is due to be released in 6.1.0: https://github.com/OpenAPITools/openapi-generator/milestone/43

  4. Havunen commented on Sep 21, 2022

    @Havunen
    Contributor

    6.1.0 has been released so this can be now closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions