diff --git a/CHANGELOG.md b/CHANGELOG.md index a85e84ff3..95f980a9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixes - iOS builds no longer break when native support disabled or not available ([#592](https://github.com/getsentry/sentry-unity/pull/592)) +- Close sentry instance when quitting the app ([#608](https://github.com/getsentry/sentry-unity/pull/608)) ## 0.11.0 diff --git a/package-dev/Plugins/iOS/SentryNativeBridge.m b/package-dev/Plugins/iOS/SentryNativeBridge.m index e8ee40550..8c99f796e 100644 --- a/package-dev/Plugins/iOS/SentryNativeBridge.m +++ b/package-dev/Plugins/iOS/SentryNativeBridge.m @@ -6,6 +6,10 @@ bool CrashedLastRun() { return[SentrySDK crashedLastRun]; } +void Close() { + [SentrySDK close]; +} + void SentryNativeBridgeAddBreadcrumb(const char* timestamp, const char* message, const char* type, const char* category, int level) { if (timestamp == NULL && message == NULL && type == NULL && category == NULL) { return; diff --git a/src/Sentry.Unity.Android/SentryJava.cs b/src/Sentry.Unity.Android/SentryJava.cs index 4a7b44c01..73bc0ecd7 100644 --- a/src/Sentry.Unity.Android/SentryJava.cs +++ b/src/Sentry.Unity.Android/SentryJava.cs @@ -28,5 +28,11 @@ public static class SentryJava return jo.CallStatic("isCrashedLastRun") ?.Call("booleanValue"); } + + public static void Close() + { + using var jo = new AndroidJavaObject("io.sentry.Sentry"); + jo.CallStatic("close"); + } } } diff --git a/src/Sentry.Unity.Android/SentryNativeAndroid.cs b/src/Sentry.Unity.Android/SentryNativeAndroid.cs index 59ce2d952..4142f494f 100644 --- a/src/Sentry.Unity.Android/SentryNativeAndroid.cs +++ b/src/Sentry.Unity.Android/SentryNativeAndroid.cs @@ -1,4 +1,5 @@ using Sentry.Extensibility; +using Sentry.Unity.Integrations; namespace Sentry.Unity.Android { @@ -44,6 +45,12 @@ public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentry // So we register our backend once more to make sure user-defined data is available in the crash report. SentryNative.ReinstallBackend(); } + ApplicationAdapter.Instance.Quitting += () => + { + // Sentry Native is initialized and closed by the Java SDK, no need to call into it directly + options.DiagnosticLogger?.LogDebug("Closing the sentry-java SDK"); + SentryJava.Close(); + }; } } } diff --git a/src/Sentry.Unity.Native/SentryNative.cs b/src/Sentry.Unity.Native/SentryNative.cs index 2e1cec61f..a5005a404 100644 --- a/src/Sentry.Unity.Native/SentryNative.cs +++ b/src/Sentry.Unity.Native/SentryNative.cs @@ -1,4 +1,5 @@ using Sentry.Extensibility; +using Sentry.Unity.Integrations; namespace Sentry.Unity.Native { @@ -16,6 +17,11 @@ public static void Configure(SentryUnityOptions options) if (options.WindowsNativeSupportEnabled) { SentryNativeBridge.Init(options); + ApplicationAdapter.Instance.Quitting += () => + { + options.DiagnosticLogger?.LogDebug("Closing the sentry-native SDK"); + SentryNativeBridge.Close(); + }; options.ScopeObserver = new NativeScopeObserver(options); options.EnableScopeSync = true; // options.CrashedLastRun = () => diff --git a/src/Sentry.Unity.Native/SentryNativeBridge.cs b/src/Sentry.Unity.Native/SentryNativeBridge.cs index 1942fb1fd..224911c88 100644 --- a/src/Sentry.Unity.Native/SentryNativeBridge.cs +++ b/src/Sentry.Unity.Native/SentryNativeBridge.cs @@ -82,6 +82,8 @@ public static void Init(SentryUnityOptions options) sentry_init(cOptions); } + public static void Close() => sentry_close(); + // libsentry.so [DllImport("sentry")] private static extern IntPtr sentry_options_new(); @@ -167,6 +169,9 @@ private static void nativeLog(int cLevel, string message, IntPtr args, IntPtr us [DllImport("sentry")] private static extern void sentry_init(IntPtr options); + [DllImport("sentry")] + private static extern int sentry_close(); + /// /// Re-installs the sentry-native backend essentially retaking the signal handlers. /// diff --git a/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs b/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs index f14bcc278..45cee2766 100644 --- a/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs +++ b/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs @@ -14,6 +14,9 @@ internal static class SentryCocoaBridgeProxy [DllImport("__Internal")] public static extern bool CrashedLastRun(); + [DllImport("__Internal")] + public static extern void Close(); + [DllImport("__Internal")] public static extern void SentryNativeBridgeAddBreadcrumb(string timestamp, string? message, string? type, string? category, int level); diff --git a/src/Sentry.Unity.iOS/SentryNativeIos.cs b/src/Sentry.Unity.iOS/SentryNativeIos.cs index 949a400da..32d19075f 100644 --- a/src/Sentry.Unity.iOS/SentryNativeIos.cs +++ b/src/Sentry.Unity.iOS/SentryNativeIos.cs @@ -1,4 +1,5 @@ using Sentry.Extensibility; +using Sentry.Unity.Integrations; namespace Sentry.Unity.iOS { @@ -25,6 +26,11 @@ public static void Configure(SentryUnityOptions options) return crashedLastRun; }; + ApplicationAdapter.Instance.Quitting += () => + { + options.DiagnosticLogger?.LogDebug("Closing the sentry-cocoa SDK"); + SentryCocoaBridgeProxy.Close(); + }; } } } diff --git a/src/Sentry.Unity/Integrations/IApplication.cs b/src/Sentry.Unity/Integrations/IApplication.cs index 7df8a90d8..b078d507e 100644 --- a/src/Sentry.Unity/Integrations/IApplication.cs +++ b/src/Sentry.Unity/Integrations/IApplication.cs @@ -16,7 +16,8 @@ internal interface IApplication RuntimePlatform Platform { get; } } - internal sealed class ApplicationAdapter : IApplication + /// Semi-internal class to be used by other Sentry.Unity assemblies + public sealed class ApplicationAdapter : IApplication { public static readonly ApplicationAdapter Instance = new(); diff --git a/src/Sentry.Unity/SentryUnity.cs b/src/Sentry.Unity/SentryUnity.cs index e7dc73b19..f83eae333 100644 --- a/src/Sentry.Unity/SentryUnity.cs +++ b/src/Sentry.Unity/SentryUnity.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using Sentry.Extensibility; +using Sentry.Unity.Integrations; namespace Sentry.Unity { @@ -31,7 +32,12 @@ public static void Init(SentryUnityOptions sentryUnityOptions) if (sentryUnityOptions.ShouldInitializeSdk()) { sentryUnityOptions.DiagnosticLogger?.LogDebug(sentryUnityOptions.ToString()); - SentrySdk.Init(sentryUnityOptions); + var sentryDotNet = SentrySdk.Init(sentryUnityOptions); + ApplicationAdapter.Instance.Quitting += () => + { + sentryUnityOptions.DiagnosticLogger?.LogDebug("Closing the sentry-dotnet SDK"); + sentryDotNet.Dispose(); + }; } } }