Skip to content

fix(media-use): wait-bgm looks for the track before it reports a timeout - #8856

Queued
miguel-heygen wants to merge 2 commits into
mainfrom
fix/musicgen-launch-flake
Queued

miguel-heygen wants to merge 2 commits into
mainfrom
fix/musicgen-launch-flake

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

wait-bgm now always looks for the music track at least once, and looks once more after its last wait, before it reports timeout. A finished track is reported ready however busy the machine is.

Why

The wait loop checked the deadline before it looked:

const started = Date.now();
...
while (Date.now() - started <= timeoutMs) { /* look for the track */ }

With --timeout-ms 0 the loop body runs only if the clock has not moved since started was read. On a busy machine one millisecond passes between those two reads, the loop never runs, and the script reports timeout without having looked at the track. For a positive timeout the same shape skips the look after the last wait: a track that landed during that wait was reported as a timeout.

The flake in bgm.launch.test.mjs, "a musicgen job replaces the engine's earlier private track and keeps it private", is this case. In the failing CI run the track had been written with the right mode and bytes, the assertion just before passed, and bgm_status.json still said timeout. Every test in wait-bgm.test.mjs and bgm.launch.test.mjs that runs wait-bgm --timeout-ms 0 shares it.

How

The loop looks first, then decides whether time is up, then waits only for the time left:

for (;;) {
  // look for the track; ready or failed exits
  const waited = Date.now() - started;
  if (waited >= timeoutMs) break;
  await sleep(Math.min(intervalMs, timeoutMs - waited));
}

The timeoutMs === 0 special case goes away: it is the first waited >= timeoutMs.

Test plan

  • Unit tests added/updated
  • Manual testing performed
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

fails without the fix: the two new cases in skills/media-use/audio/scripts/wait-bgm.test.mjs. Both run the real script under a preloaded clock that moves on every read and by the full length of every wait, as time does on a loaded machine.

  • a finished track is ready with --timeout-ms 0 even when the clock moves before the first look
  • a track that lands during the last wait is ready

On main both report timeout where ready is due. With the fix both pass.

Twenty runs of the flaky test under four CPU-bound processes passed on main too. The machine I used has enough cores that those processes caused no contention, so the moving clock, not load, is what makes the failure repeatable.

With the fix, wait-bgm.test.mjs, bgm.launch.test.mjs, bgm.test.mjs and audio.test.mjs pass three runs in a row (59 tests). bun run test:skills passes 933, skips 5 and has no failures once the parsers package is built.

@terencecho terencecho 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.

P2 · Keep a zero timeout to one check even if the system clock moves backward. At skills/media-use/audio/scripts/wait-bgm.mjs:183, a backward step makes waited negative, so --timeout-ms 0 can schedule a timer. With an absent track and a 1,000 ms simulated rollback, the base returned timeout without a timer and this head scheduled a 1,000 ms wait. Use a monotonic elapsed clock or retain the explicit zero-timeout exit after the first check. The final-wait test could also assert the timer delay. It still passes if the sleep is changed to the full polling interval.

Both named regressions fail on the base and pass here. All 59 targeted audio tests passed in three runs, and the skills manifest is in sync.

— tai (automated review)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Addressed at 754b506d.

  • Clock going backward: wait-bgm now measures elapsed time with performance.now(), a monotonic clock, not Date.now(). A wall clock that steps back no longer makes waited negative, so a zero timeout looks once and never waits. New test: a zero timeout looks once and never waits, even when the wall clock steps back. It steps Date.now() back by 1 s on every read and asserts that no timer is scheduled. On 82ab79c6 it fails: that version kept scheduling ever-longer waits and never ended (over 600,000 timers before I stopped it), and the test's spawn now has a 10 s cap.
  • Final-wait delay: a track that lands during the last wait is ready now runs with a 1,000 ms interval against a 100 ms timeout, and asserts every wait is at most 100 ms. With the sleep changed to the full interval, it fails.
  • The test clock moves both clocks forward on every read and by the full length of every wait. Only the rollback case steps the wall clock back.

Red checks for the three cases:

Version Fails
main the two original regressions
82ab79c6 the rollback case
the full-interval mutant the final-wait case

With the fix, wait-bgm.test.mjs, bgm.launch.test.mjs, bgm.test.mjs and audio.test.mjs pass 60/60, three runs in a row.

@terencecho terencecho 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.

Verified that monotonic elapsed time keeps a zero timeout from scheduling a timer after a wall-clock rollback. The final-wait test also fails if the sleep cap is removed. Both original deadline tests fail on the base, and all 60 targeted audio tests passed on this head.

— tai (automated review)

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 25, 2026
Any commits made after this event will not be merged.

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