Title: Session Replay keeps encoding segments while rate-limited and leaks MediaCodec/MediaMuxer file descriptors (EMFILE) · Issue #5529 · getsentry/sentry-java · GitHub
Open Graph Title: Session Replay keeps encoding segments while rate-limited and leaks MediaCodec/MediaMuxer file descriptors (EMFILE) · Issue #5529 · getsentry/sentry-java
X Title: Session Replay keeps encoding segments while rate-limited and leaks MediaCodec/MediaMuxer file descriptors (EMFILE) · Issue #5529 · getsentry/sentry-java
Description: Description Integration: other (sentry-android-replay) Build System: Gradle AGP Version: N/A (reported downstream via sentry-flutter, see getsentry/sentry-dart#3528) Proguard: Enabled Other Error Monitoring Solution: No Version: 8.48.3 S...
Open Graph Description: Description Integration: other (sentry-android-replay) Build System: Gradle AGP Version: N/A (reported downstream via sentry-flutter, see getsentry/sentry-dart#3528) Proguard: Enabled Other Error M...
X Description: Description Integration: other (sentry-android-replay) Build System: Gradle AGP Version: N/A (reported downstream via sentry-flutter, see getsentry/sentry-dart#3528) Proguard: Enabled Other Error M...
Opengraph URL: https://github.com/getsentry/sentry-java/issues/5529
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Session Replay keeps encoding segments while rate-limited and leaks MediaCodec/MediaMuxer file descriptors (EMFILE)","articleBody":"### Description\n\n**Integration:** other (`sentry-android-replay`)\n\n**Build System:** Gradle\n\n**AGP Version:** N/A (reported downstream via sentry-flutter, see getsentry/sentry-dart#3528)\n\n**Proguard:** Enabled\n\n**Other Error Monitoring Solution:** No\n\n**Version:** 8.48.3\n\n---\n\n## Steps to Reproduce\n\nDownstream report: getsentry/sentry-dart#3528 (Flutter app on Android, session mode replay, org under replay quota/rate limit).\n\n1. Enable Session Replay in session mode (`sessionSampleRate = 1.0`).\n2. Run the app against a DSN whose org is rate-limited for replay (server returns HTTP 429 on replay envelopes).\n3. Leave the app in the foreground for ~10+ minutes.\n4. Watch logcat: `c2.android.avc.encoder` allocate/encode/release cycles continue the whole time, each followed by `Envelope discarded due all items rate limited.` Eventually the process exhausts its FD limit (EMFILE / errno 24) and Flutter's Impeller/Vulkan rendering dies (`Could not acquire next swapchain image: ErrorOutOfHostMemory`).\n\n## Expected Result\n\nWhile rate-limited for `replay`/`all`, no segments are created or encoded, and failed/empty encodes never leak codec, surface, muxer, or file handles.\n\n## Actual Result\n\nEncoder cycles continue while the client-side rate limit is active — each segment is encoded, then immediately discarded by `RateLimiter.filter()`. Eventually the process hits its FD limit (EMFILE) and rendering dies (`ErrorOutOfHostMemory`, see downstream issue for full logs):\n\n```\nE/Sentry: ... response code 429\nD/CCodec: allocate(c2.android.avc.encoder)\n... full encode cycle ...\nW/Sentry: Envelope discarded due all items rate limited.\n```\n\n### Root cause\n\nThe rate-limit pause ([#3854](\u003chttps://github.com/getsentry/sentry-java/issues/3854>)) only stops the *recorder*; segment creation is never gated:\n\n1. `SessionCaptureStrategy.pause()` (`SessionCaptureStrategy.kt:40`) encodes a segment as part of pausing — which is then discarded client-side by definition.\n2. `RateLimiter` re-notifies observers when the limit expires (`RateLimiter.java:320`) → replay resumes → next segment is sent → 429 → pause again. This flap loops forever with no backoff, allocating encoders each round.\n3. `SessionCaptureStrategy.onScreenshotRecorded()` (`SessionCaptureStrategy.kt:74`) has no paused/rate-limited check. For hybrid SDKs (Flutter/RN), `recorder.pause()` is an async best-effort bridge call with exceptions swallowed — if it fails, a segment is encoded and discarded every 5s indefinitely.\n\n### Leaks per wasted cycle\n\n1. `ReplayCache.createVideoOf` (`ReplayCache.kt:200`): the `frameCount == 0` path returns without releasing the encoder → leaks MediaCodec, input `Surface`, and the MediaMuxer's open FD.\n2. `SimpleVideoEncoder.release()` (`SimpleVideoEncoder.kt:285`): single try block — if `drainCodec`/`stop()` throws, codec/surface/muxer are never released; exception swallowed at DEBUG.\n3. `SimpleMp4FrameMuxer.release()` (`SimpleMp4FrameMuxer.kt:69`): `muxer.stop()` throws when zero samples were written (matches `E/MPEG4Writer: Stop() called but track is not started or stopped` in the log) → `muxer.release()` skipped → leaked FD.\n4. `SentryEnvelopeItem.fromReplay` (`SentryEnvelopeItem.java:469`): the mp4 is only deleted during serialization — envelopes dropped by the rate limiter never serialize, so the replay folder grows for the whole session.\n\n### Proposed fixes\n\n1. Gate segment creation on `rateLimiter.isActiveForCategory(All/Replay)` in `SessionCaptureStrategy` before encoding. The gate must also drop/rotate incoming frames (`rotate()` only runs inside `createVideoOf`), otherwise this trades FDs for unbounded disk growth.\n2. Skip the \"pause\" segment when pausing because of rate limiting.\n3. Leak-proof teardown: release encoder on the `frameCount == 0` path; per-resource try/catch in `SimpleVideoEncoder.release()`; `try { stop() } finally { release() }` in the muxer.\n4. Delete `replayEvent.videoFile` when the envelope is dropped client-side.","author":{"url":"https://github.com/buenaflor","@type":"Person","name":"buenaflor"},"datePublished":"2026-06-11T13:14:46Z","url":"https://github.com/getsentry/sentry-java/issues/5529"}
| 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:ebb0cecb-9d79-dfa1-447a-c3f3743a5eed |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE4E:301CBB:7C97FA:AB7F5E:6B04AACD |
| html-safe-nonce | 860a73f74c01ab8a5e980d7a905d4e05484e296a97e362cdf963e7c2fe9b1f29 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTRFOjMwMUNCQjo3Qzk3RkE6QUI3RjVFOjZCMDRBQUNEIiwidmlzaXRvcl9pZCI6IjYzNjI0ODAxMzIyMTI2OTc4MDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0a151b58f9f997dc8f3e7b44f4e3541423b44dbfe3fc9dca7ac6d5478336007e |
| hovercard-subject-tag | issue:4640804294 |
| 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/5529/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3b8688cc87136304cd40feb713b49ef3efb5c751e6666b2a3c06216ed8339ed1/getsentry/sentry-java/issues/5529 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3b8688cc87136304cd40feb713b49ef3efb5c751e6666b2a3c06216ed8339ed1/getsentry/sentry-java/issues/5529 |
| og:image:alt | Description Integration: other (sentry-android-replay) Build System: Gradle AGP Version: N/A (reported downstream via sentry-flutter, see getsentry/sentry-dart#3528) Proguard: Enabled Other Error M... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | buenaflor |
| hostname | github.com |
| expected-hostname | github.com |
| None | f1d110ea058a0915c1d8aa3fc2069fa15801e128f591e5762525abe77490ecb1 |
| 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 | ffa22a65e4c46d376940fe34b05e6a7e9c7fdb9b |
| 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