Commit 1cdf1b6
authored
[Android] Fix Screenshot.CaptureAsync deadlock for synchronous UI-thread callers (#37680)
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
### Root Cause
The regression was introduced when Android screenshot capture switched
from synchronous Canvas rendering to asynchronous `PixelCopy` to support
hardware-rendered content such as WebView. #35384
`PixelCopy` delivered its completion callback on `Looper.MainLooper`,
while Sentry synchronously waited for
`Screenshot.Default.CaptureAsync().GetAwaiter().GetResult()` on the UI
thread. This created a circular dependency: the UI thread blocked
waiting for the screenshot task, and the `PixelCopy` callback could not
execute because it also required the UI thread, resulting in a deadlock.
### Description of Change
The fix retains `PixelCopy` but moves its completion callback from
`Looper.MainLooper` to a shared, process-wide `HandlerThread`, allowing
the screenshot task to complete even when the UI thread is synchronously
blocked.
Additionally, the internal await chain now uses `ConfigureAwait(false)`
to avoid resuming on the UI thread. The legacy Canvas/DrawingCache
fallback is guarded to run only on the main thread; if `PixelCopy` fails
asynchronously on a worker thread, screenshot capture safely returns
failure instead of risking off-thread Android View access or recreating
the deadlock.
### Regressed By
#35384
### Issues Fixed
Fixes #37638
### Platforms Tested
- [ ] iOS
- [ ] MacCatalyst
- [x] Android
- [ ] Windows
### Note
The regression test is **Android-only** because the deadlock originates
in Android’s PixelCopy callback and main-looper implementation. Other
platforms use different screenshot implementations and cannot exercise
this code path. The test models Sentry’s relevant synchronous behavior
directly to avoid adding a third-party dependency to the shared HostApp.
### Screenshots
| Before Issue Fix | After Issue Fix |
|------------------|-----------------|
| <video width="350" alt="withoutfix"
src="https://github.com/user-attachments/assets/72e74130-9f1b-4751-8d36-a777f3fc256c"
/> | <video width="350" alt="withfix"
src="https://github.com/user-attachments/assets/d4e9c8d2-cfa5-4f03-a8cc-f67fd5fa004f"
/> |1 parent 90afade commit 1cdf1b6
3 files changed
Lines changed: 119 additions & 5 deletions
File tree
- src
- Controls/tests
- TestCases.HostApp/Issues
- Essentials/src/Screenshot
Lines changed: 68 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
Lines changed: 29 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
| |||
60 | 64 | | |
61 | 65 | | |
62 | 66 | | |
63 | | - | |
| 67 | + | |
64 | 68 | | |
65 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
66 | 74 | | |
67 | 75 | | |
68 | 76 | | |
| |||
93 | 101 | | |
94 | 102 | | |
95 | 103 | | |
96 | | - | |
97 | | - | |
98 | | - | |
| 104 | + | |
99 | 105 | | |
100 | 106 | | |
101 | 107 | | |
102 | | - | |
| 108 | + | |
103 | 109 | | |
104 | 110 | | |
105 | 111 | | |
| |||
113 | 119 | | |
114 | 120 | | |
115 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
116 | 133 | | |
117 | 134 | | |
118 | 135 | | |
| |||
0 commit comments