Title: Session Replay: race between PixelCopy.request and executor compositing/masking on shared screenshot bitmap · Issue #5340 · getsentry/sentry-java · GitHub
Open Graph Title: Session Replay: race between PixelCopy.request and executor compositing/masking on shared screenshot bitmap · Issue #5340 · getsentry/sentry-java
X Title: Session Replay: race between PixelCopy.request and executor compositing/masking on shared screenshot bitmap · Issue #5340 · getsentry/sentry-java
Description: Summary The PixelCopy-based replay strategy (PixelCopyStrategy) shares a single screenshot Bitmap between two writers: The system PixelCopy worker on the RenderThread, which writes to screenshot when the next frame's PixelCopy.request(wi...
Open Graph Description: Summary The PixelCopy-based replay strategy (PixelCopyStrategy) shares a single screenshot Bitmap between two writers: The system PixelCopy worker on the RenderThread, which writes to screenshot wh...
X Description: Summary The PixelCopy-based replay strategy (PixelCopyStrategy) shares a single screenshot Bitmap between two writers: The system PixelCopy worker on the RenderThread, which writes to screenshot wh...
Opengraph URL: https://github.com/getsentry/sentry-java/issues/5340
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Session Replay: race between PixelCopy.request and executor compositing/masking on shared screenshot bitmap","articleBody":"## Summary\n\nThe PixelCopy-based replay strategy (`PixelCopyStrategy`) shares a single `screenshot` `Bitmap` between two writers:\n\n1. The system PixelCopy worker on the RenderThread, which writes to `screenshot` when the next frame's `PixelCopy.request(window, screenshot, …)` performs its blit.\n2. The replay executor (single-threaded), which reads/writes `screenshot` inside `applyMaskingAndNotify` (mask render) and `compositeSurfaceViewsAndMask` (SurfaceView composite).\n\nIf a frame's executor work is still in progress when the next recorder cycle fires `PixelCopy.request`, the system PixelCopy worker overwrites parts of `screenshot` while the executor is still touching it. The executor being single-threaded prevents executor↔executor overlap, but does not gate the system PixelCopy worker.\n\nThe same shape exists in the no-SurfaceView mask path; the SurfaceView capture feature ([#5333](\u003chttps://github.com/getsentry/sentry-java/pull/5333>)) just makes it easier to hit because compositing adds a draw pass to the executor side.\n\n## What this looks like in practice\n\nVisual:\n\n* **Torn frames** — emitted `screenshot` ends up with a strip of old composited content (mask + SurfaceView already applied) and a strip of newly captured raw window content. Visually, a horizontal seam in the replay frame.\n* **Mask shift / partial mask** — if the next `PixelCopy.request` overwrites window pixels while `MaskRenderer.renderMasks` is still rendering, masks can land on stale pixels or in the wrong region for one frame.\n* **SurfaceView ghosting** — composited SurfaceView pixels from frame N can persist into frame N+1 in regions the new PixelCopy didn't clear before the executor's recycle ran.\n\nNative crashes (likely related to [#4696](\u003chttps://github.com/getsentry/sentry-java/issues/4696>)):\n\n* **SIGABRT in** `libhwui.so` with the typical `__android_log_assert` → `art::Runtime::Abort` chain. PixelCopy runs on the RenderThread on top of libhwui; when its hardware blit lands on a `Bitmap` whose backing hardware buffer is being concurrently drawn into by a Canvas on the executor, libhwui's `LOG_ALWAYS_FATAL`/`CHECK` invariants fail and the process aborts.\n* `getFrame() called on a context with no surface!` — variant of the above where the Window's surface is torn down (e.g. activity backgrounded, surface recreated) between `PixelCopy.request` and the actual blit. Reported in [#4696](\u003chttps://github.com/getsentry/sentry-java/issues/4696#issuecomment-4132526075>).\n* **Recycle race on** `close()` — `close()` recycles `screenshot` on the executor; we guard with `isClosed` in the PixelCopy callback, but the guard is checked when the callback runs, not at the RenderThread blit moment. If recycle happens between `PixelCopy.request` and the blit, libhwui sees a recycled bitmap and aborts.\n\n## When it manifests\n\n* **Default session replay (1 fps):** rare. Mask render typically takes \\~10s of ms; the next cycle is 1s away. Native crashes correlated with this race appear concentrated on Android backgrounding flows where the surface tears down (see [#4696](\u003chttps://github.com/getsentry/sentry-java/issues/4696>)).\n* **Buffer mode at higher frame rates / heavy view trees / slow devices:** more frequent. The smaller the inter-frame gap, the easier it is for executor work to still be in flight when the next `PixelCopy.request` lands.\n* `isCaptureSurfaceViews = true` (experimental, opt-in via [#5333](\u003chttps://github.com/getsentry/sentry-java/pull/5333>)) widens the executor work window because compositing adds another draw pass before masking.\n* **ExoPlayer / SurfaceView-heavy apps backgrounding** — the classic [#4696](\u003chttps://github.com/getsentry/sentry-java/issues/4696>) repro.\n\n## Proposed fix\n\nA `frameInFlight: AtomicBoolean` set on the main thread immediately before `PixelCopy.request(window, screenshot, …)` fires, and cleared at the end of `applyMaskingAndNotify` on the executor. The next recorder cycle skips its capture if the flag is set (drops the frame, matches the codebase's existing *drop a frame rather than fight* pattern). Single-threaded executor means we only need to coordinate the one main → executor → main handoff.\n\n[Additionally, `close()` should fence against in-flight PixelCopy by either:](\u003chttps://github.com/getsentry/sentry-java/pull/5333>)\n\n* Posting the `screenshot.recycle()` after the executor confirms no work is queued and `frameInFlight` is clear, or\n* Capturing a strong reference to the bitmap in the PixelCopy callback closure and only recycling once we've observed the callback fire.\n\nAlternatives considered:\n\n* Synchronize on `screenshot` — blocks the main thread waiting for the executor.\n* Double-buffer (copy bitmap before handing to executor) — doubles the per-frame allocation footprint.\n\n## Out of scope here\n\nThis is independent of the `windowLocation` / `svLocation` field-vs-local races already addressed in [#5333](\u003chttps://github.com/getsentry/sentry-java/pull/5333>) — those snapshots are already taken into locals before the executor handoff.\n\n## Related\n\n* [#4696](\u003chttps://github.com/getsentry/sentry-java/issues/4696>) — SIGABRT in `libhwui.so` with PixelCopy strategy; likely a manifestation of this race (especially the surface-teardown variant).","author":{"url":"https://github.com/romtsn","@type":"Person","name":"romtsn"},"datePublished":"2026-04-28T13:08:17Z","url":"https://github.com/getsentry/sentry-java/issues/5340"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:517f19e3-9732-233d-0157-621886af090a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A9D0:C24D0:2346A:30A0C:6B046ADF |
| html-safe-nonce | db7b061f9dc05bd0fcef0a3d2bf85d48f6cd42ccedd1fadee1d4bc776c01e4d9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOUQwOkMyNEQwOjIzNDZBOjMwQTBDOjZCMDQ2QURGIiwidmlzaXRvcl9pZCI6IjU3Nzc5MTE5MDc1ODM1NTIyMjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 42cdb1d31e524b2a665b88460a08cc4485984c304af34231219cbfe797d58ac7 |
| hovercard-subject-tag | issue:4343386128 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-java/5340/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9b3c6461f80de8f03c9405e020e7b57a7fdfe4f04ce0b540bedc3a6bfcdb54b3/getsentry/sentry-java/issues/5340 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9b3c6461f80de8f03c9405e020e7b57a7fdfe4f04ce0b540bedc3a6bfcdb54b3/getsentry/sentry-java/issues/5340 |
| og:image:alt | Summary The PixelCopy-based replay strategy (PixelCopyStrategy) shares a single screenshot Bitmap between two writers: The system PixelCopy worker on the RenderThread, which writes to screenshot wh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | romtsn |
| hostname | github.com |
| expected-hostname | github.com |
| None | 83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-java git https://github.com/getsentry/sentry-java.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3368190 |
| octolytics-dimension-repository_nwo | getsentry/sentry-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3368190 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ca4d07a7cef4e4b75f821dc25e7300d80c1450a9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width