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

Close sentry instance when quitting the app #608

Merged
merged 6 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions package-dev/Plugins/iOS/SentryNativeBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/Sentry.Unity.Android/SentryJava.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@ public static class SentryJava
return jo.CallStatic<AndroidJavaObject>("isCrashedLastRun")
?.Call<bool>("booleanValue");
}

public static void Close()
{
using var jo = new AndroidJavaObject("io.sentry.Sentry");
jo.CallStatic<AndroidJavaObject>("close");
}
}
}
5 changes: 5 additions & 0 deletions src/Sentry.Unity.Android/SentryNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Sentry.Unity.Android
/// <see href="https://github.com/getsentry/sentry-native"/>
public static class SentryNative
{
public static void Close() => sentry_close();

/// <summary>
/// Re-installs the sentry-native backend essentially retaking the signal handlers.
/// </summary>
Expand All @@ -30,6 +32,9 @@ public static class SentryNative
[DllImport("sentry")]
private static extern void sentry_reinstall_backend();

[DllImport("sentry")]
private static extern void sentry_close();

// Testing
internal static Action ReinstallSentryNativeBackendStrategy = sentry_reinstall_backend;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Sentry.Unity.Android/SentryNativeAndroid.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sentry.Extensibility;
using Sentry.Unity.Integrations;

namespace Sentry.Unity.Android
{
Expand Down Expand Up @@ -44,6 +45,13 @@ 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 += () =>
{
options.DiagnosticLogger?.LogDebug("Closing the sentry-java SDK");
SentryJava.Close();
options.DiagnosticLogger?.LogDebug("Closing the sentry-native SDK");
SentryNative.Close();
vaind marked this conversation as resolved.
Show resolved Hide resolved
vaind marked this conversation as resolved.
Show resolved Hide resolved
};
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Sentry.Unity.Native/SentryNative.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sentry.Extensibility;
using Sentry.Unity.Integrations;

namespace Sentry.Unity.Native
{
Expand All @@ -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 = () =>
Expand Down
5 changes: 5 additions & 0 deletions src/Sentry.Unity.Native/SentryNativeBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();

/// <summary>
/// Re-installs the sentry-native backend essentially retaking the signal handlers.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions src/Sentry.Unity.iOS/SentryNativeIos.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sentry.Extensibility;
using Sentry.Unity.Integrations;

namespace Sentry.Unity.iOS
{
Expand All @@ -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();
};
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Sentry.Unity/Integrations/IApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 7 additions & 1 deletion src/Sentry.Unity/SentryUnity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;

namespace Sentry.Unity
{
Expand Down Expand Up @@ -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();
};
}
}
}
Expand Down