fix(capture): serialize muxer emits under stdin backpressure#816
Closed
cursor[bot] wants to merge 1 commit into
Closed
fix(capture): serialize muxer emits under stdin backpressure#816cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
Screenshot polling does not await muxer.write() promises. After #812 introduced backpressure-aware emits, a second write or flush() could start while the previous emit was still waiting on drain, interleaving cluster headers and payloads and corrupting the Matroska stream fed to ffmpeg. Queue subsequent emits only while one is awaiting backpressure so the screencast sync-ack fast path is preserved when writes complete synchronously.
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.
Bug and impact
Screenshot capture mode can produce corrupted video when ffmpeg's stdin applies backpressure.
PR #812 made
createFrameMuxerbackpressure-aware:emit()now returns a promise whenstdin.write()signals drain is needed. Screencast mode correctly defers CDP frame acks by returning that promise fromonFrame, but screenshot polling never awaitsmuxer.write().Concrete trigger:
@browserless/capture/screenshotat a frame rate ffmpeg cannot keep up with (slow encoder, large resolution).muxer.write()returns a pending drain promise after emitting frame N.muxer.write()again, orflush()runs infinallywhile the emit is still in flight.emit()calls interleave theirstdin.write()calls → cluster headers and JPEG payloads splice together → corrupted Matroska stream → broken/unplayable output or ffmpeg failure.Root cause
Backpressure serialization was added for screencast CDP ack pacing but not enforced inside the muxer. Screenshot (and
flush()racing an in-flight write) could start a new emit before the previous one finished draining.Fix
Serialize subsequent emits only while one is awaiting backpressure (
pendingAsyncqueue). When stdin accepts writes synchronously,write()still returnsundefined, preserving the screencast sync-ack fast path from #814.Validation
muxer.write()calls under backpressure stay ordered (2 writes before drain, not 4).pnpm --filter @browserless/capture test— 43/43 passpnpm --filter @browserless/screencast test— 10/10 pass