fix(replay): Release MediaMuxer when the encoder fails to start#5607
Merged
Conversation
📲 Install BuildsAndroid
|
8 tasks
Contributor
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 22f4345 | 325.23 ms | 454.66 ms | 129.43 ms |
| 22f4345 | 312.78 ms | 347.40 ms | 34.62 ms |
| 8558cac | 306.16 ms | 355.24 ms | 49.09 ms |
| bb0ff41 | 317.76 ms | 384.66 ms | 66.90 ms |
| 7c1a728 | 289.46 ms | 368.15 ms | 78.69 ms |
| d501a7e | 314.55 ms | 343.34 ms | 28.79 ms |
| 4fc476b | 280.63 ms | 363.04 ms | 82.42 ms |
| 6727e14 | 337.22 ms | 373.94 ms | 36.71 ms |
| ae7fed0 | 293.84 ms | 380.22 ms | 86.38 ms |
| ee747ae | 400.46 ms | 423.61 ms | 23.15 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 22f4345 | 1.58 MiB | 2.29 MiB | 719.83 KiB |
| 22f4345 | 1.58 MiB | 2.29 MiB | 719.83 KiB |
| 8558cac | 0 B | 0 B | 0 B |
| bb0ff41 | 0 B | 0 B | 0 B |
| 7c1a728 | 0 B | 0 B | 0 B |
| d501a7e | 0 B | 0 B | 0 B |
| 4fc476b | 0 B | 0 B | 0 B |
| 6727e14 | 1.58 MiB | 2.28 MiB | 718.64 KiB |
| ae7fed0 | 1.58 MiB | 2.12 MiB | 551.77 KiB |
| ee747ae | 1.58 MiB | 2.10 MiB | 530.95 KiB |
romtsn
approved these changes
Jul 8, 2026
romtsn
left a comment
Member
There was a problem hiding this comment.
sorry this slipped, looks great!
romtsn
reviewed
Jul 8, 2026
8 tasks
runningcode
force-pushed
the
no/fix-replay-muxer-leak-on-start-failure
branch
from
July 23, 2026 10:21
5cdf0c4 to
938d944
Compare
runningcode
marked this pull request as ready for review
July 23, 2026 10:21
runningcode
requested review from
0xadam-brown,
adinauer and
markushi
as code owners
July 23, 2026 10:21
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>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 938d944. Configure here.
runningcode
force-pushed
the
no/fix-replay-muxer-leak-on-start-failure
branch
from
July 23, 2026 10:24
938d944 to
45b652d
Compare
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.
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.
runningcode
force-pushed
the
no/fix-replay-muxer-leak-on-start-failure
branch
from
July 23, 2026 10:53
3af58a2 to
3066764
Compare
runningcode
enabled auto-merge (squash)
July 23, 2026 14:00
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.

📜 Description
The
MediaMuxeris opened eagerly inSimpleVideoEncoder's constructor, but itsrelease()was only reachable on paths that assumestart()succeeded. Two cases leaked it when the encoder failed to start:ReplayCache.createVideoOfconstructed the encoder and calledstart()in a single expression (SimpleVideoEncoder(...).also { it.start() }). Ifstart()threw, the assignment to theencoderfield never happened, so the only caller ofrelease()could never run.SimpleVideoEncoder.release()released the muxer as the last statement of thetryblock, after draining and stopping the codec. Draining a codec that never started throws, which jumped to thecatchand skipped the muxer release.The fix releases the encoder if
start()throws (then rethrows), and always releases the muxer from afinallyblock so it is freed even when draining/stopping the codec fails.💡 Motivation and Context
This surfaced as a
CloseGuard"A resource was acquired at attached stack trace but never released" warning pointing at theMediaMuxerallocation inSimpleMp4FrameMuxer. It is a real resource leak on devices where the encoder fails to start, not just a test artifact. Complements #5583, which closed the no-frames and never-started paths.💚 How did you test it?
Added a regression test (
ReplayCacheTest.releases the muxer when the encoder fails to start) that forcesstart()to fail via the testMediaCodecshadow and asserts, throughShadowCloseGuard.getErrors(), that noMediaMuxerleak is reported. Verified the test fails without the fix (with the exactExplicit termination method 'release' not callederror) and passes with it. The fullReplayCacheTestsuite is green.📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
None.