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

Ensure screenshots and view hierarchies are captured on the main thread #2712

Merged
merged 6 commits into from
May 17, 2023

Conversation

markushi
Copy link
Member

@markushi markushi commented May 16, 2023

📜 Description

Right now screenshots/VH are created on the calling thread. According to the docs it's not safe to modify or just access the UI toolkit from a background thread.

Jetpack Compose specifically may makes things worse, as View.draw() might initiates a layout pass, causing modifications on the calling thread.

💡 Motivation and Context

Fixes #2674

💚 How did you test it?

Add unit tests

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

@github-actions
Copy link
Contributor

github-actions bot commented May 16, 2023

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 343.02 ms 394.59 ms 51.57 ms
Size 1.72 MiB 2.28 MiB 570.45 KiB

Previous results on branch: fix/screenshots-viewhierarchy-main-thread

Startup times

Revision Plain With Sentry Diff
8ac2eae 300.22 ms 353.71 ms 53.49 ms
4187d3a 358.60 ms 372.38 ms 13.78 ms

App size

Revision Plain With Sentry Diff
8ac2eae 1.72 MiB 2.28 MiB 570.45 KiB
4187d3a 1.72 MiB 2.28 MiB 570.45 KiB

@codecov
Copy link

codecov bot commented May 16, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (2b0a22b) 81.11% compared to head (f472319) 81.11%.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #2712   +/-   ##
=========================================
  Coverage     81.11%   81.11%           
  Complexity     4445     4445           
=========================================
  Files           345      345           
  Lines         16387    16387           
  Branches       2226     2226           
=========================================
  Hits          13293    13293           
  Misses         2166     2166           
  Partials        928      928           

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

} else {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<ViewHierarchy> viewHierarchy = new AtomicReference<>(null);
activity.runOnUiThread(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: potentially we could do handler.postAtFrontOfQueue instead to capture the VH/SS as soon as possible, but I don't know what could be the potential side effects of this. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm that's a good one. Reading up on the docs, I wouldn't do it - at least not within this PR - maybe as a follow up investigation, as it could be relevant for other parts within our code as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid activity.runOnUiThread in favor of MainLooperHandler for testability.
See the previous comment related to this issue as well:
#2281 (comment)
Added a new comment here #2515 (comment)

Copy link
Member

@romtsn romtsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but it's a breaking change for react native. @krystofwoldrich is that fine for you to just adapt it with the new Android SDK version, or do we have to take a less invasive path (e.g. introducing another method overload to avoid the breaking change)?

@markushi
Copy link
Member Author

Looks good, but it's a breaking change for react native. @krystofwoldrich is that fine for you to just adapt it with the new Android SDK version, or do we have to take a less invasive path (e.g. introducing another method overload to avoid the breaking change)?

I brought back the old signatures relevant for RN, as the new signatures were mainly relevant for testing. Should be a non-breaking change for RN now.

@markushi markushi changed the title Fix ensure screenshots and view hierarchy are captured on the main thread Ensure screenshots and view hierarchies are captured on the main thread May 17, 2023
@markushi markushi merged commit e9bec6e into main May 17, 2023
22 checks passed
@markushi markushi deleted the fix/screenshots-viewhierarchy-main-thread branch May 17, 2023 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sentry screenshotting seems to cause crashes with Compose
4 participants