Repository navigation
fix(maui): capture the error replay for exceptions captured in C# #6001
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
Changes from all commits
4f86e96
9513c02
d6590dd
43ae1a5
32464ff
09a6455
b9044c5
c1d0ae3
b0cb4dc
0e2ec9c
31fabca
da93e23
5c22bb9
f180071
ee9f93c
e890e49
3f9f702
015e0ce
04e9d5a
d53042e
ad239f4
9906f75
1072c63
689fd9e
72e83e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| #if __ANDROID__ | ||
| using Sentry.Android.Extensions; | ||
| using System.Runtime.ExceptionServices; | ||
| using Sentry.Extensibility; | ||
| #if __IOS__ | ||
| using CoreFoundation; | ||
| #endif | ||
|
|
||
| namespace Sentry.Internal; | ||
|
|
@@ -19,6 +21,15 @@ internal class ReplaySession : IReplaySession | |
| { | ||
| public static readonly IReplaySession Instance = new ReplaySession(); | ||
|
|
||
| internal static IReplaySession? PlatformDefault => | ||
| #if __ANDROID__ || __IOS__ | ||
| Instance; | ||
| #else | ||
| null; | ||
| #endif | ||
|
|
||
| internal static readonly TimeSpan MainThreadCaptureTimeout = TimeSpan.FromSeconds(1); | ||
|
|
||
| private ReplaySession() | ||
| { | ||
| } | ||
|
|
@@ -27,19 +38,142 @@ public SentryId? ActiveReplayId | |
| { | ||
| get | ||
| { | ||
| #if __ANDROID__ || __IOS__ | ||
| if (SentrySdk.CurrentOptions?.Native.ExperimentalOptions.SessionReplay.IsSessionReplayEnabled != true) | ||
| { | ||
| return null; | ||
| } | ||
| #endif | ||
| #if __ANDROID__ | ||
| // Check to see if a Replay ID is available | ||
| var replayId = JavaSdk.ScopesAdapter.Instance?.Options?.ReplayController?.ReplayId?.ToSentryId(); | ||
| return (replayId is { } id && id != SentryId.Empty) ? id : null; | ||
| try | ||
| { | ||
| return ReadJavaScopeReplayId(); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| SentrySdk.CurrentOptions?.LogError(e, "Failed to read the replay id from the Java scope."); | ||
| return null; | ||
| } | ||
| #elif __IOS__ | ||
| string? nativeId = null; | ||
| Sentry.Cocoa.CocoaScope.Configure(scope => nativeId = scope.ReplayId); | ||
|
jamescrosswell marked this conversation as resolved.
Resolved
|
||
| return (nativeId is { } id) ? SentryId.Parse(id) : null; | ||
| return ToReplayId(nativeId); | ||
| #else | ||
| return null; | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
| #if __ANDROID__ | ||
| public SentryId? CaptureReplay(SentryEvent @event) | ||
| { | ||
| if (SentrySdk.CurrentOptions?.Native.ExperimentalOptions.SessionReplay.IsSessionReplayEnabled != true) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var capturedId = JavaSdk.ScopesAdapter.Instance?.Options?.ReplayController?.CaptureReplay(JavaBoolean.ValueOf(IsCrash(@event))); | ||
|
jamescrosswell marked this conversation as resolved.
Resolved
|
||
| return ToReplayId(capturedId?.ToString()) ?? ActiveReplayId; | ||
| } | ||
| #elif __IOS__ | ||
| public SentryId? CaptureReplay(SentryEvent @event) => | ||
| CaptureCocoaReplay(SentrySdk.CurrentOptions, NSThread.IsMain, work => DispatchQueue.MainQueue.DispatchAsync(work)); | ||
| #else | ||
| public SentryId? CaptureReplay(SentryEvent @event) => null; | ||
| #endif | ||
|
|
||
| internal static SentryId? ToReplayId(string? nativeId) => | ||
| Guid.TryParse(nativeId, out var guid) && guid != Guid.Empty ? new SentryId(guid) : null; | ||
|
|
||
| // Narrower than HasUnhandledTerminalException(): Java defers a terminating capture's replay to the next launch and stops the replay from ever switching to session mode. | ||
| internal static bool IsCrash(SentryEvent @event) => | ||
|
jamescrosswell marked this conversation as resolved.
Resolved
|
||
| @event.SentryExceptions?.Any(exception => exception.Mechanism is { } mechanism && ( | ||
| mechanism.Terminal == true || | ||
| mechanism is { Handled: false, Type: "AppDomain.UnhandledException" or "UnhandledExceptionRaiser" })) == true; | ||
|
|
||
| internal static SentryId? CaptureOnMainThread( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For Cocoa,
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My vote would be to leave it where it is - device tests are expensive and consequently late discovery (leads to longer cycle times on fixing things). |
||
| Func<SentryId?> capture, | ||
| Func<SentryId?> timedOutFallback, | ||
| bool isMainThread, | ||
| Action<Action> dispatchToMainThread, | ||
| TimeSpan timeout, | ||
| SentryOptions? options) | ||
| { | ||
| if (isMainThread) | ||
| { | ||
| return capture(); | ||
| } | ||
|
|
||
| SentryId? replayId = null; | ||
| Exception? error = null; | ||
| // Not disposed: after a timeout the dispatched capture still runs and sets it. | ||
| var captured = new ManualResetEventSlim(); | ||
| dispatchToMainThread(() => | ||
| { | ||
| try | ||
| { | ||
| replayId = capture(); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| error = e; | ||
| } | ||
| finally | ||
| { | ||
| captured.Set(); | ||
| } | ||
| }); | ||
|
|
||
| if (!captured.Wait(timeout)) | ||
| { | ||
| options?.LogWarning("Capturing the replay on the main thread timed out after {0}. The event is sent with the active replay id, if any.", timeout); | ||
| return timedOutFallback(); | ||
| } | ||
|
|
||
| if (error is not null) | ||
| { | ||
| ExceptionDispatchInfo.Capture(error).Throw(); | ||
| } | ||
|
|
||
| return replayId; | ||
| } | ||
|
|
||
| #if __IOS__ | ||
| internal SentryId? CaptureCocoaReplay(SentryOptions? options, bool isMainThread, Action<Action> dispatchToMainThread) | ||
| { | ||
| if (options?.Native.ExperimentalOptions.SessionReplay.IsSessionReplayEnabled != true) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return CaptureOnMainThread( | ||
| CaptureNativeReplay, | ||
| () => ActiveReplayId, | ||
| isMainThread, | ||
| dispatchToMainThread, | ||
| MainThreadCaptureTimeout, | ||
| options); | ||
| } | ||
|
|
||
| private static SentryId? CaptureNativeReplay() | ||
| { | ||
| var replay = SentryCocoaHybridSdk.Internal.Replay; | ||
| return replay.Capture() ? ToReplayId(replay.ReplayId) : null; | ||
| } | ||
| #endif | ||
|
|
||
| #if __ANDROID__ | ||
| // In the order Java's CombinedScopeView reads them. | ||
| internal static SentryId? ReadJavaScopeReplayId() | ||
| { | ||
| if (JavaSdk.ScopesAdapter.Instance is not { } scopes) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return ToReplayId(scopes.Scope.ReplayId.ToString()) | ||
| ?? ToReplayId(scopes.IsolationScope.ReplayId.ToString()) | ||
| ?? ToReplayId(scopes.GlobalScope.ReplayId.ToString()); | ||
| } | ||
| #endif | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #if ANDROID | ||
| namespace Sentry.Tests.Platforms.Android; | ||
|
|
||
| [CollectionDefinition(nameof(ReplaySessionTests), DisableParallelization = true)] | ||
| public class ReplaySessionTestsCollection; | ||
|
|
||
| [Collection(nameof(ReplaySessionTests))] | ||
| public class ReplaySessionTests | ||
| { | ||
| [Fact] | ||
| public void ReadJavaScopeReplayId_GlobalScopeHasReplayId_ReturnsIt() | ||
| { | ||
| var scopes = JavaSdk.ScopesAdapter.Instance!; | ||
| var empty = JavaSdk.Protocol.SentryId.EmptyId!; | ||
| var current = scopes.Scope.ReplayId; | ||
| var isolation = scopes.IsolationScope.ReplayId; | ||
| var global = scopes.GlobalScope.ReplayId; | ||
| var replayId = SentryId.Create(); | ||
| try | ||
| { | ||
| scopes.Scope.ReplayId = empty; | ||
| scopes.IsolationScope.ReplayId = empty; | ||
| scopes.GlobalScope.ReplayId = new JavaSdk.Protocol.SentryId(replayId.ToString()); | ||
|
|
||
| ReplaySession.ReadJavaScopeReplayId().Should().Be(replayId); | ||
| } | ||
| finally | ||
| { | ||
| scopes.Scope.ReplayId = current; | ||
| scopes.IsolationScope.ReplayId = isolation; | ||
| scopes.GlobalScope.ReplayId = global; | ||
| } | ||
| } | ||
| } | ||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #if IOS | ||
| namespace Sentry.Tests.Platforms.iOS; | ||
|
|
||
| public class ReplaySessionTests | ||
| { | ||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public void CaptureCocoaReplay_ReplayOff_ReturnsNullWithoutDispatch(bool withOptions) | ||
| { | ||
| var options = withOptions ? new SentryOptions() : null; | ||
| var dispatched = false; | ||
|
|
||
| var result = ((ReplaySession)ReplaySession.Instance).CaptureCocoaReplay(options, isMainThread: false, | ||
| _ => dispatched = true); | ||
|
|
||
| result.Should().BeNull(); | ||
| dispatched.Should().BeFalse(); | ||
| } | ||
| } | ||
| #endif |
Uh oh!
There was an error while loading. Please reload this page.