Skip to content
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

Fix Issue Where WeakReference Delegate is Garbage Collected While Client is Alive #529

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Mindscape.Raygun4Net.NetCore.Common/RaygunClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public abstract class RaygunClientBase
private readonly ThrottledBackgroundMessageProcessor _backgroundMessageProcessor;
private readonly IRaygunUserProvider _userProvider;
protected internal const string SentKey = "AlreadySentByRaygun";

/// <summary>
/// Store a strong reference to the OnApplicationUnhandledException delegate so it does not get garbage collected while
/// the client is still alive
/// </summary>
private readonly UnhandledExceptionBridge.UnhandledExceptionHandler _onUnhandledExceptionDelegate;


/// <summary>
/// Raised just before a message is sent. This can be used to make final adjustments to the <see cref="RaygunMessage"/>, or to cancel the send.
Expand Down Expand Up @@ -116,8 +123,10 @@ protected RaygunClientBase(RaygunSettingsBase settings, HttpClient client, IRayg
_userProvider = userProvider;

_wrapperExceptions.Add(typeof(TargetInvocationException));

UnhandledExceptionBridge.OnUnhandledException(OnApplicationUnhandledException);

_onUnhandledExceptionDelegate = OnApplicationUnhandledException;

UnhandledExceptionBridge.OnUnhandledException(_onUnhandledExceptionDelegate);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ namespace Mindscape.Raygun4Net.NetCore.Tests
[TestFixture]
public class UnhandledExceptionBridgeTests
{
private RaygunClientBase _strongRef;

[SetUp]
public void Setup()
{
_strongRef = new RaygunClient(new RaygunSettings(){CatchUnhandledExceptions = true, ApiKey = "dummy"});
xenolightning marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// GitHub Issue: 513
/// See https://github.com/MindscapeHQ/raygun4net/issues/513 for the issue report
Expand Down Expand Up @@ -45,5 +53,25 @@ void Callback(Exception e, bool b)

Assert.That(observedException, Is.Null);
}

[Test]
public void UnhandledExceptionBridge_WhenClientStillAlive_DelegateShouldStillBeAlive()
{
string errorMessage = null;

_strongRef.SendingMessage += (sender, args) =>
{
errorMessage = args.Message.Details.Error.Message;
};

// Call GC, this should NOT GC the delegate which the UnhandledExceptionBridge holds for the RaygunClient's
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

UnhandledExceptionBridge.RaiseUnhandledException(new Exception("Alive"), false);

Assert.That(errorMessage, Is.Not.Null);
xenolightning marked this conversation as resolved.
Show resolved Hide resolved
}
}
}