dotnet / dotnet/orleans

Memory leak when using GrainCancellationToken

Open
#8,030 0 comments 0 reactions 1 assignee Claimed by @ReubenBond View on GitHub
area-grains Needs: investigation 🔍
Dominant language
C#
Stars
10.9k
Forks
2.1k
Avg merge
14h 42m
Merged PRs (30d)
354

Description

We use Orleans 3.6.0. Memory leak also is reproducing in 3.6.5

We encountered a memory leak while using `GrainCancellationTokenSource` when single long-running `GrainCancellationToken` was passed at many Grains.

After some research we found that GrainCancellationToken work on `CancellationSourcesExtension`. This extension creates for every Grain where GrainCancellationToken passed to method.

`CancellationSourcesExtension` uses `Interner` which holds `WeakRerences` to all `GrainCancellationToken`. `Interner` has a `System.Threading.Timer` that clears dictionary. This timer references to `Interner` instance by callback method.

`Interner` implements `IDisposable`. His dispose method stops Timer. But `CancellationSourcesExtension` never calls it.

**`CancellationSourcesExtension` is cleaned up by the garbage collector:**

![image](https://user-images.githubusercontent.com/4726930/196683834-31815e65-01e8-41cb-99a6-c24579098847.png)

**But `Interner` and its `Timer` not:**
![image](https://user-images.githubusercontent.com/4726930/196686889-a455c7aa-9955-42b5-a6c4-901762acf9d4.png)
![image](https://user-images.githubusercontent.com/4726930/196683274-25892408-0880-43a7-bc02-901c9c4baeb7.png)

**This is object hierarch:**
![image](https://user-images.githubusercontent.com/4726930/196687124-3f4ed74e-db2a-418b-a087-096aca378f94.png)

P.S. We check source code of this classes at 3.6.5 and do not found any changes.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.