Skip to content

Fix InvalidStateError crash in MSE updateend handler#938

Open
vinnybad wants to merge 2 commits intoAlexxIT:masterfrom
vinnybad:fix/sourcebuffer-invalidstate-error
Open

Fix InvalidStateError crash in MSE updateend handler#938
vinnybad wants to merge 2 commits intoAlexxIT:masterfrom
vinnybad:fix/sourcebuffer-invalidstate-error

Conversation

@vinnybad
Copy link
Copy Markdown

@vinnybad vinnybad commented Mar 14, 2026

Summary

  • Wraps the buffer management block in the MSE updateend handler with try-catch to prevent InvalidStateError crashes when the SourceBuffer has been removed from its parent MediaSource

Problem

The sb.buffered property getter at line 452 throws InvalidStateError when the SourceBuffer has been detached from the MediaSource (e.g., during stream teardown or mode transitions):

InvalidStateError: Failed to read the 'buffered' property from 'SourceBuffer':
This SourceBuffer has been removed from the parent media source.
SourceBuffer.<anonymous> (/webrtc/video-rtc.js:452:40)

This causes the video stream to crash and reload in a loop, making multi-camera dashboards unstable.

Root Cause

The updateend event fires asynchronously. Between the event dispatch and handler execution, the SourceBuffer can be removed from the MediaSource (race condition). The existing check sb.buffered && sb.buffered.length does not prevent the crash because sb.buffered is 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 appendBuffer call 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:

  • Before fix: InvalidStateError every ~10 seconds, causing continuous stream reloads
  • After fix: 2-minute stability test — all 4 cameras stable, zero browser errors, all streams playing continuously

Fixes #901

Test plan

  • Load a multi-camera dashboard with 4+ cameras
  • Verify streams play continuously without reloading
  • Check browser console for absence of InvalidStateError
  • Test with both WebRTC and MSE streaming modes

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Constant Buffering / Freezing in Home Assistant - Fine in go2rtc

1 participant