Skip to content

fix: Collect MainThreadData first thing in Init #2281

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions package-dev/Runtime/SentryInitialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -81,6 +81,7 @@ private static void Init()

private static void SetUpPlatformServices()
{
SentryPlatformServices.CollectMainThreadData();
SentryPlatformServices.UnityInfo = new SentryUnityInfo();

#if SENTRY_NATIVE_COCOA
Expand Down
2 changes: 0 additions & 2 deletions src/Sentry.Unity.Android/SentryNativeAndroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public static class SentryNativeAndroid
/// <param name="options">The Sentry Unity options to use.</param>
public static void Configure(SentryUnityOptions options)
{
MainThreadData.CollectData();

options.DiagnosticLogger?.LogInfo("Attempting to configure native support via the Android SDK");

if (!options.AndroidNativeSupportEnabled)
Expand Down
11 changes: 9 additions & 2 deletions src/Sentry.Unity/NativeUtils/SentryPlatformServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ namespace Sentry.Unity.NativeUtils;

/// <summary>
/// These are SDK's services that are only available at runtime and cannot be baked into the SDK. The
/// <c>SentryInitialization.cs</c> is provided as <c>.cs</c> and gets compiled with the game. It sets <c>IUnityInfo</c>
/// and the <c>PlatformConfiguration</c> callback during the game's startup so that they are available during initializtion.
/// <c>SentryInitialization.cs</c> is provided as <c>.cs</c> and gets compiled with the game.
/// It sets <c>IUnityInfo</c> and the <c>PlatformConfiguration</c> callback during the game's startup, as well as collecting
/// the MainThreadData. These are then available during both auto and manual initialization.
/// </summary>
/// <remarks>Consider this <c>internal</c>.</remarks>
public static class SentryPlatformServices
Expand All @@ -31,4 +32,10 @@ public static ISentryUnityInfo UnityInfo
/// The PlatformConfiguration callback is responsible for configuring the native SDK and setting up scope sync.
/// </summary>
public static Action<SentryUnityOptions>? PlatformConfiguration { get; set; }

/// <summary>
/// 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,
/// </summary>
public static void CollectMainThreadData() => MainThreadData.CollectData();
}
2 changes: 0 additions & 2 deletions src/Sentry.Unity/SentryUnitySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion test/Sentry.Unity.Tests/ContextWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)));

Expand Down
Loading