fix(replay): Release MediaMuxer when no frames are encoded#5583
Merged
Conversation
The MediaMuxer is created when the video encoder is constructed, but its release() was reachable only on the happy path. Two cases leaked it: - createVideoOf returned early when frameCount was 0 without releasing the encoder. - SimpleMp4FrameMuxer.release() called muxer.stop() before muxer.release(). stop() throws if the muxer was never started (no frame ever muxed), so release() was skipped. This surfaced as a CloseGuard "resource was acquired but never released" warning. Guard stop() behind the started flag so release() is always reached, and release the encoder on the no-frames return path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
📲 Install BuildsAndroid
|
Contributor
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 18c0bc2 | 306.73 ms | 349.77 ms | 43.03 ms |
| 0eaac1e | 316.82 ms | 357.34 ms | 40.52 ms |
| d15471f | 303.49 ms | 439.08 ms | 135.59 ms |
| fc5ccaf | 276.52 ms | 370.46 ms | 93.93 ms |
| e2dce0b | 308.96 ms | 360.10 ms | 51.14 ms |
| 5b1a06b | 352.27 ms | 413.70 ms | 61.43 ms |
| 37ec571 | 366.04 ms | 424.28 ms | 58.23 ms |
| 9fbb112 | 361.43 ms | 427.57 ms | 66.14 ms |
| bbc35bb | 324.88 ms | 425.73 ms | 100.85 ms |
| ff8eea4 | 313.42 ms | 337.08 ms | 23.66 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 18c0bc2 | 1.58 MiB | 2.13 MiB | 557.33 KiB |
| 0eaac1e | 1.58 MiB | 2.19 MiB | 619.17 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| fc5ccaf | 1.58 MiB | 2.13 MiB | 557.54 KiB |
| e2dce0b | 0 B | 0 B | 0 B |
| 5b1a06b | 0 B | 0 B | 0 B |
| 37ec571 | 0 B | 0 B | 0 B |
| 9fbb112 | 1.58 MiB | 2.11 MiB | 539.18 KiB |
| bbc35bb | 1.58 MiB | 2.12 MiB | 553.01 KiB |
| ff8eea4 | 1.58 MiB | 2.28 MiB | 718.64 KiB |
This was referenced Jun 22, 2026
Closed
runningcode
marked this pull request as ready for review
June 22, 2026 11:50
runningcode
requested review from
0xadam-brown,
adinauer,
markushi and
romtsn
as code owners
June 22, 2026 11:50
runningcode
enabled auto-merge (squash)
June 22, 2026 11:55
4 tasks
3 tasks
8 tasks
runningcode
added a commit
that referenced
this pull request
Jul 23, 2026
The MediaMuxer is opened eagerly in SimpleVideoEncoder's constructor, but its release() was only reachable on paths that assume start() succeeded. Two cases leaked it: - createVideoOf constructed the encoder and called start() in one expression, so when start() threw the encoder was never assigned and release() could never run. - SimpleVideoEncoder.release() released the muxer as the last statement of the try block, after draining and stopping the codec. Draining a codec that never started throws, skipping the muxer release. Release the encoder if start() throws, and always release the muxer from a finally block so it is freed even when draining/stopping the codec fails. This surfaced as a CloseGuard "resource was acquired but never released" warning. Complements #5583. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
runningcode
added a commit
that referenced
this pull request
Jul 23, 2026
The MediaMuxer is opened eagerly in SimpleVideoEncoder's constructor, but its release() was only reachable on paths that assume start() succeeded. Two cases leaked it: - createVideoOf constructed the encoder and called start() in one expression, so when start() threw the encoder was never assigned and release() could never run. - SimpleVideoEncoder.release() released the muxer as the last statement of the try block, after draining and stopping the codec. Draining a codec that never started throws, skipping the muxer release. Release the encoder if start() throws, and always release the muxer from a finally block so it is freed even when draining/stopping the codec fails. This surfaced as a CloseGuard "resource was acquired but never released" warning. Complements #5583. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
runningcode
added a commit
that referenced
this pull request
Jul 23, 2026
* fix(replay): Release MediaMuxer when the encoder fails to start The MediaMuxer is opened eagerly in SimpleVideoEncoder's constructor, but its release() was only reachable on paths that assume start() succeeded. Two cases leaked it: - createVideoOf constructed the encoder and called start() in one expression, so when start() threw the encoder was never assigned and release() could never run. - SimpleVideoEncoder.release() released the muxer as the last statement of the try block, after draining and stopping the codec. Draining a codec that never started throws, skipping the muxer release. Release the encoder if start() throws, and always release the muxer from a finally block so it is freed even when draining/stopping the codec fails. This surfaced as a CloseGuard "resource was acquired but never released" warning. Complements #5583. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * changelog * fix(replay): Skip MediaMuxer stop when no samples were written MediaMuxer.stop() throws IllegalStateException when the muxer was started but no sample was ever written to its track. SimpleMp4FrameMuxer.release() only guarded against the never-started case, so a started-but-empty muxer made release() throw. Because release() runs from SimpleVideoEncoder's finally block, that throw propagates out to createVideoOf, which treats release() as safe cleanup; the encoder is left dangling and the orphan video file is never deleted. Only call stop() when at least one sample was written; muxer.release() on a started-but-not-stopped muxer is safe. * Format code * fix(replay): Guard each native release so cleanup never propagates The finally block in SimpleVideoEncoder.release() released the codec, surface, and muxer without guards. release() is treated by callers such as createVideoOf as safe cleanup, but MediaMuxer.stop() (reached via frameMuxer.release()) can still throw IllegalStateException on a genuine file-finalization failure even when samples were written. That would skip the remaining releases and propagate out of release(). Guard each release independently so failing to free one resource neither skips the others nor escapes to the caller. --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes JAVA-580
📜 Description
The
MediaMuxerinsideSimpleMp4FrameMuxeris constructed eagerly when the video encoder is created (CloseGuard.openregisters a guard expectingrelease()), butrelease()was only reachable on the happy path. Two cases leaked it:ReplayCache.createVideoOfreturned early whenframeCount == 0(frames exist but none could be encoded) without callingencoder?.release().SimpleMp4FrameMuxer.release()calledmuxer.stop()beforemuxer.release().MediaMuxer.stop()throwsIllegalStateExceptionif the muxer was never started (i.e. no frame was ever muxed, soINFO_OUTPUT_FORMAT_CHANGEDnever fired), somuxer.release()was skipped. The exception was then swallowed bySimpleVideoEncoder.release()'s catch block.The fix guards
stop()behind thestartedflag sorelease()is always reached, and releases the encoder on the no-frames return path.💡 Motivation and Context
Surfaces as a CloseGuard warning:
A resource was acquired at attached stack trace but never released ... Explicit termination method 'release' not called, pointing at theMediaMuxerconstructor inSimpleMp4FrameMuxer. It is a latent resource leak in production whenever a segment ends up with zero encodable frames, and it shows up consistently in Robolectric tests where the encode pipeline doesn't start the muxer.💚 How did you test it?
The existing
ReplayCacheTest."when screenshot is corrupted, deletes it immediately"exercises the frames-present-but-none-encoded path (frameCount == 0after the encoder is created), which previously triggered the leak.📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps