-
-
Notifications
You must be signed in to change notification settings - Fork 230
fix: memory leak when profiling is enabled #5133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1a77379
ba35729
a16638f
1bb8430
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| namespace Sentry.Internal; | ||
|
|
||
| /// <summary> | ||
| /// A minimal replacement for <see cref="ConcurrentBag{T}"/>. | ||
| /// | ||
| /// We're using this to avoid the same class of memory leak that <see cref="ConcurrentQueueLite{T}"/> | ||
| /// was introduced to avoid. See https://github.com/getsentry/sentry-dotnet/issues/5113 | ||
| /// </summary> | ||
| internal class ConcurrentBagLite<T> : IReadOnlyCollection<T> | ||
| { | ||
| private readonly List<T> _items; | ||
|
|
||
| public ConcurrentBagLite() | ||
| { | ||
| _items = new List<T>(); | ||
| } | ||
|
|
||
| public ConcurrentBagLite(IEnumerable<T> collection) | ||
| { | ||
| _items = new List<T>(collection); | ||
| } | ||
|
|
||
| public void Add(T item) | ||
| { | ||
| lock (_items) | ||
| { | ||
| _items.Add(item); | ||
| } | ||
| } | ||
|
|
||
| public int Count | ||
| { | ||
| get | ||
| { | ||
| lock (_items) | ||
| { | ||
| return _items.Count; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public bool IsEmpty => Count == 0; | ||
|
|
||
| public void Clear() | ||
| { | ||
| lock (_items) | ||
| { | ||
| _items.Clear(); | ||
| } | ||
| } | ||
|
|
||
| public T[] ToArray() | ||
| { | ||
| lock (_items) | ||
| { | ||
| return _items.ToArray(); | ||
| } | ||
| } | ||
|
|
||
| public IEnumerator<T> GetEnumerator() | ||
| { | ||
| // Return a snapshot to avoid holding the lock during iteration | ||
| // and to prevent InvalidOperationException if the collection is modified. | ||
| T[] snapshot; | ||
| lock (_items) | ||
| { | ||
| snapshot = _items.ToArray(); | ||
| } | ||
| return ((IEnumerable<T>)snapshot).GetEnumerator(); | ||
| } | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
| } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SQL listener test had a bug that the LIFO mask would hide. The test did |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth checking whether we have a hotspot where
add(IEnumearble<T> items)would be useful.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call... we have these:
sentry-dotnet/src/Sentry/Scope.cs
Lines 530 to 534 in a16638f
sentry-dotnet/src/Sentry/Scope.cs
Lines 555 to 568 in a16638f
sentry-dotnet/src/Sentry/Scope.cs
Lines 687 to 690 in a16638f
sentry-dotnet/src/Sentry/Scope.cs
Lines 713 to 716 in a16638f
sentry-dotnet/src/Sentry/Scope.cs
Lines 739 to 742 in a16638f
I don't think I'd describe any of those as a hot path though... with the exception of the first one (attachments) it's all one off stuff that happens at init for a very constrained number of items - and even attachments is likely to be only a handful of items.
I think I'll leave it for now then, in the interests of expediting the fix to the memory leak.