Skip to content

Commit a27bb31

Browse files
authored
fix: Make fetching scene name run on IL2CPP only (#2206)
1 parent 1aad1a5 commit a27bb31

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- The SDK no longer attaches screenshots when capturing errors in the Unity Editor. ([#2163](https://github.com/getsentry/sentry-unity/pull/2163))
88

9+
### Fixes
10+
11+
- The SDK no longer causes crashes with `EXCEPTION_ACCESS_VIOLATION_READ` when using the Mono scripting backend. The SDK now adds the active scene name to the context in IL2CPP builds only ([#2206](https://github.com/getsentry/sentry-unity/pull/2206))
12+
913
### Dependencies
1014

1115
- Bump .NET SDK from v5.7.0-beta.0 to v5.11.1 ([#2154](https://github.com/getsentry/sentry-unity/pull/2154), [#2188](https://github.com/getsentry/sentry-unity/pull/2188), [#2207](https://github.com/getsentry/sentry-unity/pull/2207))

src/Sentry.Unity/Integrations/UnityScopeIntegration.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ internal static class UnitySdkInfo
1717
internal class UnityScopeIntegration : ISdkIntegration
1818
{
1919
private readonly IApplication _application;
20+
private readonly ISentryUnityInfo? _unityInfo;
2021

21-
public UnityScopeIntegration(IApplication application)
22+
public UnityScopeIntegration(IApplication application, ISentryUnityInfo? unityInfo)
2223
{
2324
_application = application;
25+
_unityInfo = unityInfo;
2426
}
2527

2628
public void Register(IHub hub, SentryOptions options)
2729
{
28-
var scopeUpdater = new UnityScopeUpdater((SentryUnityOptions)options, _application);
30+
var scopeUpdater = new UnityScopeUpdater((SentryUnityOptions)options, _application, _unityInfo);
2931
hub.ConfigureScope(scopeUpdater.ConfigureScope);
3032
}
3133
}
@@ -34,12 +36,14 @@ internal class UnityScopeUpdater
3436
{
3537
private readonly SentryUnityOptions _options;
3638
private readonly IApplication _application;
39+
private readonly ISentryUnityInfo? _unityInfo;
3740
private readonly ISceneManager _sceneManager;
3841

39-
public UnityScopeUpdater(SentryUnityOptions options, IApplication application, ISceneManager? sceneManager = null)
42+
public UnityScopeUpdater(SentryUnityOptions options, IApplication application, ISentryUnityInfo? unityInfo = null, ISceneManager? sceneManager = null)
4043
{
4144
_options = options;
4245
_application = application;
46+
_unityInfo = unityInfo;
4347
_sceneManager = sceneManager ?? SceneManagerAdapter.Instance;
4448
}
4549

@@ -142,7 +146,12 @@ private void PopulateUnity(Protocol.Unity unity)
142146
unity.TargetFrameRate = MainThreadData.TargetFrameRate;
143147
unity.CopyTextureSupport = MainThreadData.CopyTextureSupport;
144148
unity.RenderingThreadingMode = MainThreadData.RenderingThreadingMode;
145-
unity.ActiveSceneName = _sceneManager.GetActiveScene().Name;
149+
150+
if (_unityInfo?.IL2CPP is true)
151+
{
152+
// Currently an IL2CPP only feature: see https://github.com/getsentry/sentry-unity/issues/2181
153+
unity.ActiveSceneName = _sceneManager.GetActiveScene().Name;
154+
}
146155
}
147156

148157
private void PopulateTags(Action<string, string> setTag)

src/Sentry.Unity/ScriptableSentryUnityOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ internal SentryUnityOptions ToSentryUnityOptions(bool isBuilding, ISentryUnityIn
135135
{
136136
application ??= ApplicationAdapter.Instance;
137137

138-
var options = new SentryUnityOptions(isBuilding, application)
138+
var options = new SentryUnityOptions(isBuilding, application, unityInfo)
139139
{
140140
Enabled = Enabled,
141141
Dsn = Dsn,

src/Sentry.Unity/SentryUnityOptions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,12 @@ internal string? DefaultUserId
296296

297297
public SentryUnityOptions() : this(false, ApplicationAdapter.Instance) { }
298298

299-
internal SentryUnityOptions(bool isBuilding, IApplication application) :
300-
this(SentryMonoBehaviour.Instance, application, isBuilding)
299+
internal SentryUnityOptions(bool isBuilding, IApplication application, ISentryUnityInfo? unityInfo = null) :
300+
this(SentryMonoBehaviour.Instance, application, isBuilding, unityInfo)
301301
{ }
302302

303-
internal SentryUnityOptions(SentryMonoBehaviour behaviour, IApplication application, bool isBuilding)
303+
// For testing
304+
internal SentryUnityOptions(SentryMonoBehaviour behaviour, IApplication application, bool isBuilding, ISentryUnityInfo? unityInfo = null)
304305
{
305306
// IL2CPP doesn't support Process.GetCurrentProcess().StartupTime
306307
DetectStartupTime = StartupTimeDetectionMode.Fast;
@@ -315,7 +316,7 @@ internal SentryUnityOptions(SentryMonoBehaviour behaviour, IApplication applicat
315316
this.AddIntegration(new UnityLogHandlerIntegration(this));
316317
this.AddIntegration(new UnityApplicationLoggingIntegration());
317318
this.AddIntegration(new AnrIntegration(behaviour));
318-
this.AddIntegration(new UnityScopeIntegration(application));
319+
this.AddIntegration(new UnityScopeIntegration(application, unityInfo));
319320
this.AddIntegration(new UnityBeforeSceneLoadIntegration());
320321
this.AddIntegration(new SceneManagerIntegration());
321322
this.AddIntegration(new SessionIntegration(behaviour));

test/Sentry.Unity.Tests/UnityEventScopeTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void SentrySdkCaptureEvent(bool captureOnUiThread)
109109
RenderingThreadingMode = new Lazy<string>(() => "MultiThreaded"),
110110
StartTime = new(() => DateTimeOffset.UtcNow),
111111
};
112-
var options = new SentryUnityOptions(_sentryMonoBehaviour, _testApplication, false)
112+
var options = new SentryUnityOptions(_sentryMonoBehaviour, _testApplication, false, new TestUnityInfo { IL2CPP = true })
113113
{
114114
Dsn = "https://b8fd848b31444e80aa102e96d2a6a648@o510466.ingest.sentry.io/5606182",
115115
Enabled = true,
@@ -430,7 +430,9 @@ public void DeviceProtocol_Assigned()
430430
}
431431

432432
[Test]
433-
public void UnityProtocol_Assigned()
433+
[TestCase(true)]
434+
[TestCase(false)]
435+
public void UnityProtocol_Assigned(bool isIL2CPP)
434436
{
435437
var sceneManager = new SceneManagerIntegrationTests.FakeSceneManager { ActiveSceneName = "TestScene" };
436438
var systemInfo = new TestSentrySystemInfo
@@ -444,7 +446,7 @@ public void UnityProtocol_Assigned()
444446
MainThreadData.SentrySystemInfo = systemInfo;
445447
MainThreadData.CollectData();
446448

447-
var sut = new UnityScopeUpdater(_sentryOptions, _testApplication, sceneManager);
449+
var sut = new UnityScopeUpdater(_sentryOptions, _testApplication, new TestUnityInfo { IL2CPP = isIL2CPP }, sceneManager);
448450
var scope = new Scope(_sentryOptions);
449451

450452
// act
@@ -459,7 +461,7 @@ public void UnityProtocol_Assigned()
459461
Assert.AreEqual(systemInfo.TargetFrameRate!.Value, unityProtocol.TargetFrameRate);
460462
Assert.AreEqual(systemInfo.CopyTextureSupport!.Value, unityProtocol.CopyTextureSupport);
461463
Assert.AreEqual(systemInfo.RenderingThreadingMode!.Value, unityProtocol.RenderingThreadingMode);
462-
Assert.AreEqual(sceneManager.GetActiveScene().Name, unityProtocol.ActiveSceneName);
464+
Assert.AreEqual(isIL2CPP ? sceneManager.GetActiveScene().Name : null, unityProtocol.ActiveSceneName);
463465
}
464466

465467
[Test]

0 commit comments

Comments
 (0)