hls.js icon indicating copy to clipboard operation
hls.js copied to clipboard

Main thread hangs on live playlists with a large EXT-X-DISCONTINUITY-SEQUENCE (initPTS sparse-array scan)

Open melissamcdougall opened this issue 1 month ago • 0 comments

What version of HLS.js are you using?

1.7.4 (the same code is on master: src/controller/stream-controller.ts, onLevelLoaded)

What browser (including version) are you using?

Safari 27.0.1 on iPadOS (iPad mini 6th generation). The cause is engine-independent: the same loop is linear in the index on JavaScriptCore (Bun) and V8 (Node). See the numbers below.

What OS (including version) are you using?

iPadOS 27 (iPad mini 6th generation), and macOS 27 for the timings below

Test stream

A live fMP4 playlist whose EXT-X-DISCONTINUITY-SEQUENCE is large, for example:

#EXTM3U
#EXT-X-VERSION:7
#EXT-X-TARGETDURATION:2
#EXT-X-MEDIA-SEQUENCE:1793570400
#EXT-X-DISCONTINUITY-SEQUENCE:1793559696
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-MAP:URI="init.mp4"
#EXT-X-PROGRAM-DATE-TIME:2026-11-02T01:00:00.000Z
#EXTINF:2.002000,
seg00000.m4s
...

Our server derives the discontinuity sequence from a Unix timestamp after a gap, so the number only ever increases. The spec allows any decimal-integer up to 2^64-1.

Configuration

{ startPosition: -1, lowLatencyMode: false }

Additional player setup steps

None. A bare page with hls.attachMedia(video); hls.loadSource(url); video.play() reproduces it.

Checklist

  • [x] The issue observed is not already reported by searching on Github under https://github.com/video-dev/hls.js/issues
  • [ ] The issue occurs in the stable client (latest release) on https://hlsjs.video-dev.org/demo and not just on my page. Not tried on the demo page; reproduced with a minimal page using the 1.7.4 release build.
  • [ ] The issue occurs in the latest client (main branch) on https://hlsjs-dev.video-dev.org/demo and not just on my page. Not tried; the code below is unchanged on master.
  • [x] The stream has correct Access-Control-Allow-Origin headers (CORS)
  • [x] There are no network errors such as 404s in the browser console when trying to play the stream

Steps to reproduce

  1. Serve a live fMP4 playlist with #EXT-X-DISCONTINUITY-SEQUENCE:1793559696 (no EXT-X-ENDLIST).
  2. Play it with hls.js.
  3. As soon as the init segment is parsed, initPTS[frag.cc] is set at index 1793559696. At the next live playlist refresh, onLevelLoaded runs:
// Remove timestamp mapping from sparse array for discontinuities no longer present
this.initPTS.some((t, i) => {
  if (i >= newDetails.startCC) {
    return true;
  }
  delete this.initPTS[i];
});

Array.prototype.some visits indices 0..length-1 (checking HasProperty for each), so it scans about 1.8 billion holes before it reaches the one real entry. This repeats at every playlist reload.

Expected behaviour

Playback starts. Cleaning up initPTS costs time proportional to the number of entries, not to the discontinuity sequence number.

What actually happened?

The page's main thread locks up right after hlsFragParsingInitSegment for the init segment. On an iPad mini 6 the page never recovers: no further events or timers run, and it stays on our "Starting…" state. With the same playlist rewritten to #EXT-X-DISCONTINUITY-SEQUENCE:0 (nothing else changed), it plays normally.

Timing of the loop alone (a = []; a[n] = {}; a.some(...), one callback):

n JavaScriptCore (Bun) V8 (Node)
1,000,000 20 ms 17 ms
50,000,000 762 ms 856 ms

Extrapolated to n = 1,793,559,696, that is about 30 s per playlist refresh on a fast desktop, and longer on a tablet.

A possible fix is to iterate only the keys that exist, for example for (const k in this.initPTS) / Object.keys, or to keep initPTS in a Map keyed by cc. Other cc-indexed sparse arrays (vttCCs, the initPTS.length - 1 used in the timeline controller) may have the same exposure.

Console output

[stream-controller]: Level 0 loaded [1793570400,1793573477][part-1793573477--1], cc [1793559696, 1793559696] duration:6156.024924000319
[stream-controller]: Loading main sn: 1793573475 of main playlist 0 (frag:[6150.018924000317-6152.020924000318]) cc: 1793559696 [1793570400-1793573477], target: 6150.042924000319
[passthrough-remuxer]: Found initPTS in video track 1 at playlist time: 6150.018924000317 offset: -0.000049000317630998325 (-1.176007628440857/24000)
[buffer-controller]: creating sourceBuffer(video/mp4;codecs=mp4a.40.2,avc1.640028)
[buffer-controller]: queuing "audiovideo" append sn: initSegment of main playlist 0 cc: 1793559696 offset: undefined bytes: 1396
(page freezes here)

Chrome media internals output

n/a (reproduced on iPadOS Safari)

melissamcdougall avatar Nov 02 '26 02:11 melissamcdougall