Skip to content

Add experimental session replay on iOS - #2951

Draft
bitsandfoxes wants to merge 13 commits into
spire/session-replay-for-unityfrom
spire/session-replay-for-unity-on-ios
Draft

bitsandfoxes wants to merge 13 commits into
spire/session-replay-for-unityfrom
spire/session-replay-for-unity-on-ios

Conversation

@bitsandfoxes

@bitsandfoxes bitsandfoxes commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Adds an experimental, opt-in session replay for iOS, alongside the Android one, and stacks on the Android session replay change. It uses the same options.Experimental.SessionReplay settings (sample rates, Quality) and needs runtime initialization of the iOS SDK.

Capture

Unity captures the replay's frames, and sentry-cocoa encodes and sends them. UnityReplayRecorder grabs the frame at the end of Unity's frame and reads it back with AsyncGPUReadback. It then hands the frame to sentry-cocoa through the screenshot provider on cocoa's hybrid replay API (imageWithView:onComplete:). This costs under 1 ms of main-thread time per frame, where sentry-cocoa's own capture takes about 60 ms per frame in a Unity scene.

Every frame request sentry-cocoa makes completes exactly once, because cocoa captures no further frames while a request is open. The request is either delivered, or cancelled with the current replay's last frame of the same size, or with a black frame. After five capture failures in a row, Unity capture stops until the app restarts. sentry-cocoa keeps the provider only until the SDK closes, so Unity sets it again on every SDK start, before the replay starts.

When Unity can't capture, no replay is recorded, and errors and sessions are reported as usual:

  • On a device without async GPU readback, an error is logged and sentry-cocoa starts without replay options.
  • When the frame provider doesn't install, an error is logged and no replay starts.

Background and foreground

Diagram

Diagram source
flowchart LR
    A([App loses focus]) --> B[Replay paused]
    B --> C{Back within the session interval?}
    C -->|yes| D([Same replay continues])
    C -->|no| E([Replay ends at the pause, a new one is sampled and started]):::warn

The session interval is AutoSessionTrackingInterval. No segment covers the time spent away. C# samples and starts the replay. C# events carry the replay id, which is read from the native scope, so a C# error links to its replay.

Dependencies

Measured on an iPhone 18 Pro

At 1 fps, in the same scene:

Capture Avg frame Frames per 10 s Process CPU Memory
Replay off 16.67 ms 600 ~13% 188 MB
sentry-cocoa's own capture (not used) 17.48 ms ~572 ~20% 216 MB
Unity capture 16.67 ms 600 ~14% 215 MB
Unity-captured replay frames before and after a short trip home, in the same replay

Unity-captured replay frames before (left) and after (right) a short trip home, in the same replay.

Sample

unity-of-bugs sends a replay with each error on iOS, as it does on Android.

Masking, editor UI and Unity as a Library are not part of this change.

sentry-cocoa records the game, sampled and started at runtime initialization,
and paused and resumed with Unity's lifecycle. Errors captured in C# capture
the replay and carry its id. The replay capture option's SentryJava value is
now Native, for the native SDK on either platform.
With options.Experimental.SessionReplay.Capture set to Unity, the bridge
installs a screenshot provider in sentry-cocoa before the replay starts.
sentry-cocoa asks it for each frame on the main thread; Unity takes the
request, captures the screen at the end of the frame, reads it back from
the GPU at the window's size in points and delivers the pixels, which the
bridge turns into an image on a background queue. Every request completes
exactly once: a failed capture, a pause, a stop or a replaced request
completes it with the last frame, since sentry-cocoa takes no further
frame until it does.

The sample's ReplayBenchmark runs on iOS too, and reports the process's
CPU use and memory footprint there.
…rames

The bridge forgets the last delivered frame whenever a replay starts or
stops, keeps only frames delivered for the pending request, and falls back
to a blank frame when the last one has another size, so a cancelled
request never shows another replay's screen. DeliverFrame cancels the
request when its arguments are invalid.

The recorder runs on its own component, which cancels the request when it
is disabled or destroyed and resumes capture when enabled again. A failure
to take a request cancels it and stops capture, a request without a size
is cancelled without capturing, and five capture failures in a row stop
capture instead of filling the replay with repeated frames.
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against e7b4748

Point sentry-cocoa at getsentry/sentry-cocoa#9739, which adds the
screenshot provider for hybrid SDKs on top of cocoa's main. The provider
method is imageWithView:onComplete:, and cocoa keeps the provider only
until the SDK closes, so Unity capture installs it after every SDK start.
@bitsandfoxes bitsandfoxes mentioned this pull request Nov 25, 2026
1 of 4 tasks
getsentry/sentry-cocoa#9739 now sits on sentry-cocoa 9.35.0. Its API is
unchanged, so the bridge builds against it as before.
… iOS replays

Brings in the Android session replay work: the .NET SDK 7.1 update, the sample
facelift, and session replay with no Capture option, where Unity always
captures the frames.

On iOS, Unity now always captures the replay's frames and hands them to
sentry-cocoa. There's no fallback to sentry-cocoa's own capture: without async
GPU readback, sentry-cocoa gets no replay options, and when Unity's frame
provider doesn't install, no replay starts. Both log an error. The sample
turns on error replays for iOS next to Android's setting.
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.

1 participant