dotnet-eventsource icon indicating copy to clipboard operation
dotnet-eventsource copied to clipboard

CancellationTokenSource memory leak

Open keith-mason-conning opened this issue 5 years ago • 7 comments

Hello,

In using your library to process SSE events, I'm finding that as millions of events are received there can be a significant buildup in RAM. I ran it through dotMemory to profile and found that the leak appears to be due to the combined token in DoWithTimeout (AsyncHelpers.cs). The combined token is used but never disposed. Adding a combinedCancellation.Dispose() after the await task seems to cure the problem.

Thanks!

keith-mason-conning avatar May 15 '21 13:05 keith-mason-conning

Since you mentioned AsyncHelpers.cs, I'm assuming that you're using version 4.0.0 or later, but can you confirm what version you're using?

eli-darkly avatar May 17 '21 17:05 eli-darkly

Also— just to be clear, do you have reason to think this is a leak in the sense of leaking resources that remain in use, or more of a case of "under heavy usage, these ephemeral objects build up before the garbage collector gets around to cleaning them up"? The latter seems more likely as I don't see how a reference to this CancellationTokenSource instance would be retained, but it's hard to be sure without any more details on what you observed.

eli-darkly avatar May 17 '21 17:05 eli-darkly

I see no reason not to add an explicit dispose here (or a using) in any case, but just wanted to get a little more clarity on the issue.

eli-darkly avatar May 17 '21 17:05 eli-darkly

I was originally using 4.0.0, but I did upgrade to 4.1.1 and was still able to reproduce the issue. I further tested by downloading the source and building myself out of master. It still reproduced.

It's a proper leak; the GC never cleans the tokens up. I've seen my process get up to 2GB worth of combinedCancellation token objects over a course of an hour or two, all sitting in Generation 2 heap. Once the dispose is added, the objects never even make it to Generation 1.

Thanks!

keith-mason-conning avatar May 17 '21 17:05 keith-mason-conning

Thanks. Yes, we should definitely fix this and the fix is straightforward, so I should have a patch out shortly. I'm still not sure why CreatedLinkedTokenSource behaves this way (I see some mentions online of a similar finding, but no details on where the leaked reference is being retained or why) but the .NET API docs do say specifically that one ought to call Dispose, implying that Microsoft is aware of some reason why GC won't work as desired in this case.

eli-darkly avatar May 17 '21 18:05 eli-darkly

Note for LaunchDarkly SDK users: if I understand correctly, this issue does not affect any existing GA releases of the LaunchDarkly .NET SDK or LaunchDarkly Xamarin SDK, which use versions 3.x and earlier of LaunchDarkly.EventSource. The 3.x library used a different timeout mechanism based on Task.Delay and Task.WhenAny... which I believe is less efficient than the CancellationTokenSource-based mechanism in most ways, but doesn't have the potential to leak a token. LaunchDarkly.EventSource 4.x is used by the upcoming LaunchDarkly .NET SDK 6.0, which is still prerelease.

eli-darkly avatar May 17 '21 18:05 eli-darkly

Released version 4.1.2 with a patch for this. Thanks again for pointing it out.

eli-darkly avatar May 17 '21 22:05 eli-darkly