Skip to content

fix(hls-video-element): ignore superseded load() calls to avoid leaking an hls.js instance - #261

Open
RaphaelFakhri wants to merge 1 commit into
muxinc:mainfrom
RaphaelFakhri:fix/hls-video-concurrent-load
Open

RaphaelFakhri wants to merge 1 commit into
muxinc:mainfrom
RaphaelFakhri:fix/hls-video-concurrent-load

Conversation

@RaphaelFakhri

@RaphaelFakhri RaphaelFakhri commented Sep 29, 2026 •

Copy link
Copy Markdown

Fixes the race in <hls-video> where calling load() more than once before the first call finishes creates more than one hls.js instance.

The bug

load() destroys the current instance, then waits one tick with await Promise.resolve() before it creates a new Hls. When src changes twice in the same tick (for example a framework that sets src after the element already has one), both calls destroy nothing, both wait, and both resume. Each one creates an Hls and attaches it to the same media element:

  • The first Hls instance is overwritten in this.api, so nothing can destroy it later. It keeps loading and buffering.
  • Two <source> elements are appended to the native element.

The change

Each load() call takes a generation number from #loadId. After each await, the call returns if a newer load() has started, so only the most recent call creates a player. The native HLS path (Safari) has the same check.

Testing

Added changing src twice in the same tick creates a single hls.js instance to packages/hls-video-element/test/test.js. It counts the Hls instances attached to the element and the <source> elements.

cd packages/hls-video-element
npx wet test --channel chromium
  • Without the fix: 11 passed, 2 failed (2 instances, 2 source elements).
  • With the fix: 13 passed, 0 failed.

eslint *.js is clean.


Note

Low Risk
Targeted concurrency guard around existing load lifecycle; behavior unchanged when src updates are not racing on the same tick.

Overview
Fixes a race in <hls-video> when src changes multiple times before load() finishes its one-tick deferral: overlapping calls could each create an hls.js player, orphan the first instance, and append duplicate <source> elements.

load() now bumps a private #loadId per invocation and bails out after each await if a newer load has started, so only the latest src wins on both the hls.js and native Safari paths.

Adds a test that sets src twice in the same tick and asserts a single attached Hls instance, the final URL, and one <source>.

Reviewed by Cursor Bugbot for commit 05de95b. Bugbot is set up for automated code reviews on this repo. Configure here.

@vercel

vercel Bot commented Sep 29, 2026

Copy link
Copy Markdown

@RaphaelFakhri is attempting to deploy a commit to the Mux Team on Vercel.

A member of the Team first needs to authorize it.

@javierarams javierarams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@RaphaelFakhri
RaphaelFakhri force-pushed the fix/hls-video-concurrent-load branch from 95aaa9b to 05de95b Compare October 22, 2026 12:16
@RaphaelFakhri

Copy link
Copy Markdown
Author

Rebased onto main to resolve the conflict in test/test.js. Both the new template test and the concurrent load() test pass (16/16).

This branch has not been deployed

No deployments
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.

2 participants