Skip to content

Commit 8f448d2

Browse files
Prevent application crashes from MAUI ScreenshotImplementation (#4069)
1 parent b6e0112 commit 8f448d2

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Target `net9.0` on Sentry.Google.Cloud.Functions to avoid conflict with Sentry.AspNetCore ([#4039](https://github.com/getsentry/sentry-dotnet/pull/4039))
99
- Changed default value for `SentryOptions.EnableAppHangTrackingV2` to `false` ([#4042](https://github.com/getsentry/sentry-dotnet/pull/4042))
1010
- Missing MAUI `Shell` navigation breadcrumbs on iOS ([#4006](https://github.com/getsentry/sentry-dotnet/pull/4006))
11+
- Prevent application crashes when capturing screenshots on iOS ([#4069](https://github.com/getsentry/sentry-dotnet/pull/4069))
1112

1213
### Features
1314

src/Sentry.Maui/Internal/ScreenshotAttachment.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ public ScreenshotAttachmentContent(SentryMauiOptions options)
3939
}
4040

4141
public Stream GetStream()
42+
{
43+
try
44+
{
45+
return GetStreamInternal();
46+
}
47+
catch (Exception ex)
48+
{
49+
// See https://github.com/getsentry/sentry-dotnet/issues/3880#issuecomment-2640159466
50+
_options.LogError("Failed to capture screenshot", ex);
51+
// Return empty stream since calling code may assume a non-null return value
52+
// E.g. https://github.com/getsentry/sentry-dotnet/blob/db5606833a4b0662c6bea0663cca10cb05fb5157/src/Sentry/Protocol/Envelopes/EnvelopeItem.cs#L332-L333
53+
return new MemoryStream();
54+
}
55+
}
56+
57+
private Stream GetStreamInternal()
4258
{
4359
var stream = Stream.Null;
4460
// Not including this on Windows specific build because on WinUI this can deadlock.

test/Sentry.Maui.Tests/SentryMauiScreenshotTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,20 @@ public Fixture()
3434
private readonly Fixture _fixture = new();
3535

3636
#if __MOBILE__
37+
// TODO: This test doesn't work on Android or iOS, so isn't much use. We should consider replacing it with some
38+
// more granular tests that test SentryMauiOptionsSetup, SentryMauiScreenshotProcessor and ScreenshotAttachment
39+
// in isolation. It's generally hard to test any of this functionality since ScreenshotImplementation relies of
40+
// various static members like ActivityStateManager.Default:
41+
// https://github.com/dotnet/maui/blob/3c7b65264d2f341a48db32263a271fd8718cfd23/src/Essentials/src/Screenshot/Screenshot.android.cs#L28
3742
[SkippableFact]
3843
public async Task CaptureException_WhenAttachScreenshots_ContainsScreenshotAttachmentAsync()
3944
{
4045
#if __IOS__
4146
Skip.If(true, "Flaky on iOS");
4247
#endif
48+
#if ANDROID
49+
Skip.If(true, "Doesn't work on Android");
50+
#endif
4351

4452
// Arrange
4553
var builder = _fixture.Builder.UseSentry();
@@ -133,12 +141,20 @@ public async Task CaptureException_BeforeCaptureScreenshot_DisableCaptureAsync()
133141
envelopeItem.Should().BeNull();
134142
}
135143

144+
// TODO: This test doesn't work on Android or iOS, so isn't much use. We should consider replacing it with some
145+
// more granular tests that test SentryMauiOptionsSetup, SentryMauiScreenshotProcessor and ScreenshotAttachment
146+
// in isolation. It's generally hard to test any of this functionality since ScreenshotImplementation relies of
147+
// various static members like ActivityStateManager.Default:
148+
// https://github.com/dotnet/maui/blob/3c7b65264d2f341a48db32263a271fd8718cfd23/src/Essentials/src/Screenshot/Screenshot.android.cs#L28
136149
[SkippableFact]
137150
public async Task CaptureException_BeforeCaptureScreenshot_DefaultAsync()
138151
{
139152
#if __IOS__
140153
Skip.If(true, "Flaky on iOS");
141154
#endif
155+
#if ANDROID
156+
Skip.If(true, "Doesn't work on Android");
157+
#endif
142158

143159
// Arrange
144160
var builder = _fixture.Builder.UseSentry(options => options.SetBeforeScreenshotCapture((e, hint) =>

0 commit comments

Comments
 (0)