Skip to content

Commit 7673311

Browse files
authored
fix: Collect MainThreadData first thing in Init (#2281)
1 parent f60c66d commit 7673311

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

package-dev/Runtime/SentryInitialization.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ internal static class SentryInitialization
5757
#endif
5858
private static void Init()
5959
{
60-
// We're setting up `UnityInfo` and the platform specific configure callbacks as the very first thing.
61-
// These are required to be available during initialization.
60+
// We're setting up platform specific configuration as the very first thing.
61+
// These are required to be available during initialization, i.e. UnityInfo and MainThreadData.
6262
SetUpPlatformServices();
6363

6464
// Loading the options invokes the ScriptableOption`Configure` callback. Users can disable the SDK via code.
@@ -81,6 +81,7 @@ private static void Init()
8181

8282
private static void SetUpPlatformServices()
8383
{
84+
SentryPlatformServices.CollectMainThreadData();
8485
SentryPlatformServices.UnityInfo = new SentryUnityInfo();
8586

8687
#if SENTRY_NATIVE_COCOA

src/Sentry.Unity.Android/SentryNativeAndroid.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public static class SentryNativeAndroid
2222
/// <param name="options">The Sentry Unity options to use.</param>
2323
public static void Configure(SentryUnityOptions options)
2424
{
25-
MainThreadData.CollectData();
26-
2725
options.DiagnosticLogger?.LogInfo("Attempting to configure native support via the Android SDK");
2826

2927
if (!options.AndroidNativeSupportEnabled)

src/Sentry.Unity/NativeUtils/SentryPlatformServices.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ namespace Sentry.Unity.NativeUtils;
44

55
/// <summary>
66
/// These are SDK's services that are only available at runtime and cannot be baked into the SDK. The
7-
/// <c>SentryInitialization.cs</c> is provided as <c>.cs</c> and gets compiled with the game. It sets <c>IUnityInfo</c>
8-
/// and the <c>PlatformConfiguration</c> callback during the game's startup so that they are available during initializtion.
7+
/// <c>SentryInitialization.cs</c> is provided as <c>.cs</c> and gets compiled with the game.
8+
/// It sets <c>IUnityInfo</c> and the <c>PlatformConfiguration</c> callback during the game's startup, as well as collecting
9+
/// the MainThreadData. These are then available during both auto and manual initialization.
910
/// </summary>
1011
/// <remarks>Consider this <c>internal</c>.</remarks>
1112
public static class SentryPlatformServices
@@ -31,4 +32,10 @@ public static ISentryUnityInfo UnityInfo
3132
/// The PlatformConfiguration callback is responsible for configuring the native SDK and setting up scope sync.
3233
/// </summary>
3334
public static Action<SentryUnityOptions>? PlatformConfiguration { get; set; }
35+
36+
/// <summary>
37+
/// Collects comprehensive system and device information that must be accessed on Unity's main thread.
38+
/// This includes hardware specs (CPU, GPU, memory), device details, graphics capabilities,
39+
/// </summary>
40+
public static void CollectMainThreadData() => MainThreadData.CollectData();
3441
}

src/Sentry.Unity/SentryUnitySdk.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ private SentryUnitySdk(SentryUnityOptions options)
2828
return null;
2929
}
3030

31-
MainThreadData.CollectData();
32-
3331
// Some integrations are controlled through a flag and opt-in. Adding these integrations late so we have equal
3432
// behaviour whether the options got created through the ScriptableObject or the SDK gets manually initialized
3533
AddIntegrations(options);

test/Sentry.Unity.Tests/ContextWriterTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public void Arguments()
6363
CopyTextureSupport = new(() => "CopyTextureSupport"),
6464
RenderingThreadingMode = new(() => "RenderingThreadingMode"),
6565
StartTime = new(() => DateTimeOffset.UtcNow),
66-
6766
};
6867
var context = new MockContextWriter();
6968
var options = new SentryUnityOptions(application: _testApplication, behaviour: _sentryMonoBehaviour)
@@ -79,6 +78,8 @@ public void Arguments()
7978

8079
// act
8180
MainThreadData.SentrySystemInfo = sysInfo;
81+
MainThreadData.CollectData(); // In an actual game, this happens outside and before Init()
82+
8283
SentrySdk.Init(options);
8384
Assert.IsTrue(context.SyncFinished.WaitOne(TimeSpan.FromSeconds(10)));
8485

0 commit comments

Comments
 (0)