Fix InvalidStateError crash in MSE updateend handler#938
Open
vinnybad wants to merge 2 commits intoAlexxIT:masterfrom
Open
Fix InvalidStateError crash in MSE updateend handler#938vinnybad wants to merge 2 commits intoAlexxIT:masterfrom
vinnybad wants to merge 2 commits intoAlexxIT:masterfrom
Conversation
Wrap the buffer management block in the MSE updateend handler with try-catch to handle the case where the SourceBuffer has been removed from the parent MediaSource before the handler executes. The sb.buffered getter throws InvalidStateError when the SourceBuffer is detached, which causes the stream to crash and reload in a loop. This matches the existing error handling pattern already used for the appendBuffer call in the same handler. Fixes AlexxIT#901
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.
Summary
updateendhandler with try-catch to preventInvalidStateErrorcrashes when theSourceBufferhas been removed from its parentMediaSourceProblem
The
sb.bufferedproperty getter at line 452 throwsInvalidStateErrorwhen theSourceBufferhas been detached from theMediaSource(e.g., during stream teardown or mode transitions):This causes the video stream to crash and reload in a loop, making multi-camera dashboards unstable.
Root Cause
The
updateendevent fires asynchronously. Between the event dispatch and handler execution, theSourceBuffercan be removed from theMediaSource(race condition). The existing checksb.buffered && sb.buffered.lengthdoes not prevent the crash becausesb.bufferedis a getter property — the truthiness check itself throws the exception.Fix
Wrap the buffer management block in a try-catch, matching the existing error handling pattern already used for the
appendBuffercall in the same handler (lines 443-449).Test Results
Tested with 4 simultaneous UniFi Protect camera streams (3x G6 Instant + G4 Doorbell Pro) on Home Assistant 2026.1.0:
InvalidStateErrorevery ~10 seconds, causing continuous stream reloadsFixes #901
Test plan
InvalidStateError