Skip to content
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

Screenshot capture failing intermittently #19450

Open
Giorgi opened this issue Dec 15, 2023 · 4 comments
Open

Screenshot capture failing intermittently #19450

Giorgi opened this issue Dec 15, 2023 · 4 comments
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/android 🤖 t/bug Something isn't working
Milestone

Comments

@Giorgi
Copy link

Giorgi commented Dec 15, 2023

Description

In the sentry-dotnet I'm adding a feature to capture screenshots when some event is logged. In the test Android application screen capture fails intermittently with the following error:

NullReferenceException: The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class.

I added Platform.Init(this, savedInstanceState); as well as Microsoft.Maui.ApplicationModel.ActivityStateManager.Default.Init(this, savedInstanceState); to the MainActivity but still get the error.

The code to capture screen:

var stream = MainThread.InvokeOnMainThreadAsync(async () =>
{
    try
    {
        if (Screenshot.Default.IsCaptureSupported)
        {
            var screen = await Screenshot.Default.CaptureAsync().ConfigureAwait(true);

            var stream = await screen.OpenReadAsync(ScreenshotFormat.Jpeg).ConfigureAwait(true);

            return stream;
        }
        else
        {
            _options.DiagnosticLogger?.Log(SentryLevel.Warning, "Capturing screenshot not supported");
            return Stream.Null;
        }
    }
    //In some cases screen capture can throw, for example on Android if the activity is marked as secure.
    catch (Exception ex)
    {
        _options.DiagnosticLogger?.Log(SentryLevel.Error, "Error capturing screenshot", ex);
        return Stream.Null;
    }
}).ConfigureAwait(false).GetAwaiter().GetResult();

Steps to Reproduce

Run Sentry.Maui.Device.TestApp from the linked repository. Run tests from Sentry.Maui.Tests collection and observe that CaptureException_WhenAttachScreenshots_ContainsScreenshotAttachmentAsync test will sometimes fail.

If you put a breakpoint inside ScreenshotAttachmentContent.GetStream in the catch clause you will see the following exception:

System.NullReferenceException: The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class.
   at Microsoft.Maui.ApplicationModel.ActivityStateManagerExtensions.GetCurrentActivity(IActivityStateManager manager, Boolean throwOnNull) in D:\a\_work\1\s\src\Essentials\src\Platform\ActivityStateManager.android.cs:line 95
   at Microsoft.Maui.Media.ScreenshotImplementation.CaptureAsync() in D:\a\_work\1\s\src\Essentials\src\Screenshot\Screenshot.android.cs:line 28
   at Sentry.Maui.Internal.ScreenshotAttachmentContent.<GetStream>b__2_0() in S:\src\Sentry\sentry-dotnet\src\Sentry.Maui\Internal\ScreenshotAttachment.cs:line 43

Link to public reproduction project repository

https://github.com/getsentry/sentry-dotnet/tree/feat/Maui-Screenshot

Version with bug

7.0.101

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Happens on Android 27, 31 and 32, haven't tested others.

Did you find any workaround?

No

Relevant log output

No response

@Giorgi Giorgi added the t/bug Something isn't working label Dec 15, 2023
@jsuarezruiz jsuarezruiz added area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/android 🤖 labels Dec 18, 2023
@PureWeen PureWeen added this to the Backlog milestone Dec 18, 2023
@ghost
Copy link

ghost commented Dec 18, 2023

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@MauiUIui
Copy link

Having the same issue here I guess. await screen.OpenReadAsync(); seems to cause it.

@jamescrosswell
Copy link

Having the same issue here I guess. await screen.OpenReadAsync(); seems to cause it.

@MauiUIui do you have a simpler way to reproduce this issue? I suspect the Sentry repo is a bit too complicated for any of the dotnet/maui maintainers to be able to pick this up and action it...

@michalkoch
Copy link

Any update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/android 🤖 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants