From f3ef866fb2abcee278bdd9d26ec1bf3875d77755 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Tue, 12 Aug 2025 18:36:47 +0200 Subject: [PATCH 1/3] Moved to 'Init' --- src/Sentry.Unity.Android/SentryNativeAndroid.cs | 2 -- src/Sentry.Unity/SentrySdk.cs | 3 +++ src/Sentry.Unity/SentryUnitySdk.cs | 2 -- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Sentry.Unity.Android/SentryNativeAndroid.cs b/src/Sentry.Unity.Android/SentryNativeAndroid.cs index 6c3e6daea..76a0608ea 100644 --- a/src/Sentry.Unity.Android/SentryNativeAndroid.cs +++ b/src/Sentry.Unity.Android/SentryNativeAndroid.cs @@ -22,8 +22,6 @@ public static class SentryNativeAndroid /// The Sentry Unity options to use. public static void Configure(SentryUnityOptions options) { - MainThreadData.CollectData(); - options.DiagnosticLogger?.LogInfo("Attempting to configure native support via the Android SDK"); if (!options.AndroidNativeSupportEnabled) diff --git a/src/Sentry.Unity/SentrySdk.cs b/src/Sentry.Unity/SentrySdk.cs index 55cb951bb..f76ba37ae 100644 --- a/src/Sentry.Unity/SentrySdk.cs +++ b/src/Sentry.Unity/SentrySdk.cs @@ -36,6 +36,9 @@ public static void Init(SentryUnityOptions options) options.LogWarning("The SDK has already been initialized. Skipping initialization."); } + // Some native SDKs (i.e. Android) rely on `MainThreadData` to be collected + MainThreadData.CollectData(); + try { // Since this mutates the options (i.e. adding scope observer) we have to invoke before initializing the SDK diff --git a/src/Sentry.Unity/SentryUnitySdk.cs b/src/Sentry.Unity/SentryUnitySdk.cs index 99252ee3f..4b4654f17 100644 --- a/src/Sentry.Unity/SentryUnitySdk.cs +++ b/src/Sentry.Unity/SentryUnitySdk.cs @@ -28,8 +28,6 @@ private SentryUnitySdk(SentryUnityOptions options) return null; } - MainThreadData.CollectData(); - // Some integrations are controlled through a flag and opt-in. Adding these integrations late so we have equal // behaviour whether the options got created through the ScriptableObject or the SDK gets manually initialized AddIntegrations(options); From c4268632802c7a3f0d3494553e036685fe570e0d Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 13 Aug 2025 11:46:48 +0200 Subject: [PATCH 2/3] Moved MainThreadData collection into PlatformServices --- package-dev/Runtime/SentryInitialization.cs | 5 +++-- .../NativeUtils/SentryPlatformServices.cs | 11 +++++++++-- src/Sentry.Unity/SentrySdk.cs | 3 --- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index 3d5071b1a..b8a428fb7 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -57,8 +57,8 @@ internal static class SentryInitialization #endif private static void Init() { - // We're setting up `UnityInfo` and the platform specific configure callbacks as the very first thing. - // These are required to be available during initialization. + // We're setting up platform specific configuration as the very first thing. + // These are required to be available during initialization, i.e. UnityInfo and MainThreadData. SetUpPlatformServices(); // Loading the options invokes the ScriptableOption`Configure` callback. Users can disable the SDK via code. @@ -81,6 +81,7 @@ private static void Init() private static void SetUpPlatformServices() { + SentryPlatformServices.CollectMainThreadData(); SentryPlatformServices.UnityInfo = new SentryUnityInfo(); #if SENTRY_NATIVE_COCOA diff --git a/src/Sentry.Unity/NativeUtils/SentryPlatformServices.cs b/src/Sentry.Unity/NativeUtils/SentryPlatformServices.cs index ceda6adb1..5f8e9a53b 100644 --- a/src/Sentry.Unity/NativeUtils/SentryPlatformServices.cs +++ b/src/Sentry.Unity/NativeUtils/SentryPlatformServices.cs @@ -4,8 +4,9 @@ namespace Sentry.Unity.NativeUtils; /// /// These are SDK's services that are only available at runtime and cannot be baked into the SDK. The -/// SentryInitialization.cs is provided as .cs and gets compiled with the game. It sets IUnityInfo -/// and the PlatformConfiguration callback during the game's startup so that they are available during initializtion. +/// SentryInitialization.cs is provided as .cs and gets compiled with the game. +/// It sets IUnityInfo and the PlatformConfiguration callback during the game's startup, as well as collecting +/// the MainThreadData. These are then available during both auto and manual initialization. /// /// Consider this internal. public static class SentryPlatformServices @@ -31,4 +32,10 @@ public static ISentryUnityInfo UnityInfo /// The PlatformConfiguration callback is responsible for configuring the native SDK and setting up scope sync. /// public static Action? PlatformConfiguration { get; set; } + + /// + /// Collects comprehensive system and device information that must be accessed on Unity's main thread. + /// This includes hardware specs (CPU, GPU, memory), device details, graphics capabilities, + /// + public static void CollectMainThreadData() => MainThreadData.CollectData(); } diff --git a/src/Sentry.Unity/SentrySdk.cs b/src/Sentry.Unity/SentrySdk.cs index f76ba37ae..55cb951bb 100644 --- a/src/Sentry.Unity/SentrySdk.cs +++ b/src/Sentry.Unity/SentrySdk.cs @@ -36,9 +36,6 @@ public static void Init(SentryUnityOptions options) options.LogWarning("The SDK has already been initialized. Skipping initialization."); } - // Some native SDKs (i.e. Android) rely on `MainThreadData` to be collected - MainThreadData.CollectData(); - try { // Since this mutates the options (i.e. adding scope observer) we have to invoke before initializing the SDK From 4bdb69da48989ec3f3f296440bb24b04f7753fb8 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Thu, 14 Aug 2025 17:48:16 +0200 Subject: [PATCH 3/3] Updated tests --- test/Sentry.Unity.Tests/ContextWriterTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Sentry.Unity.Tests/ContextWriterTests.cs b/test/Sentry.Unity.Tests/ContextWriterTests.cs index eca1dd663..2e08a3895 100644 --- a/test/Sentry.Unity.Tests/ContextWriterTests.cs +++ b/test/Sentry.Unity.Tests/ContextWriterTests.cs @@ -63,7 +63,6 @@ public void Arguments() CopyTextureSupport = new(() => "CopyTextureSupport"), RenderingThreadingMode = new(() => "RenderingThreadingMode"), StartTime = new(() => DateTimeOffset.UtcNow), - }; var context = new MockContextWriter(); var options = new SentryUnityOptions(application: _testApplication, behaviour: _sentryMonoBehaviour) @@ -79,6 +78,8 @@ public void Arguments() // act MainThreadData.SentrySystemInfo = sysInfo; + MainThreadData.CollectData(); // In an actual game, this happens outside and before Init() + SentrySdk.Init(options); Assert.IsTrue(context.SyncFinished.WaitOne(TimeSpan.FromSeconds(10)));