Skip to content

fix(platform): bound video toolchain provisioning and its children - #4079

Merged
yannickmonney merged 2 commits into
mainfrom
fix/bound-video-toolchain-provisioning
Oct 2, 2026
Merged

yannickmonney merged 2 commits into
mainfrom
fix/bound-video-toolchain-provisioning

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

What changed

services/platform/backend/core/video_links/ytdlp_toolchain.ts (the live YouTube test's self-provisioner, also used by the dev engine's "Video toolchain" step) had no bound on any stage:

  • downloadTo called fetch and then arrayBuffer() with no deadline;
  • run and which spawned children with no deadline and never killed them;
  • the 600 s beforeAll timeout in ytdlp_live.test.ts rejected the hook but cancelled none of this.

In Unit job 110740061610 (PR #4072) a stalled sudo apt-get install -y ffmpeg therefore held the required job for ten minutes and lived on as a hanging process.

Now:

  • Per-stage deadlines, capped by a run budget.
    • VIDEO_TOOLCHAIN_DEADLINES sets each stage's bound: download response 30 s, download body 120 s, which lookup 10 s, unzip 60 s, package install 420 s.
    • ProvisioningClock caps every stage by what is left of the 540 s run budget.
    • The hook timeout is now totalMs + 60 s (= 600 s, unchanged), derived from the budget. So a stall fails inside provisioning, naming its stage, before the hook times out.
  • Downloads. One AbortController covers the request in two phases, <stage> response and then <stage> body. Expiry aborts it with the stage's error.
  • Children (spawnBounded) follow the ytdlp.ts:runYtdlp shape:
    • detached, so the child runs in its own process group;
    • on expiry, process.kill(-pid) sends SIGTERM, then SIGKILL after a 5 s grace;
    • once the child exits, SIGKILL goes to anything it left in its group;
    • the promise settles only after close, or, if even SIGKILL brings no close, after one more grace, saying so.
    • Only processes the helper started are signalled.
  • Errors.
    • VideoToolchainError carries stage, and its message reads, for example, [video-toolchain] yt-dlp download body: timed out after 120000 ms (deadline 120000 ms).
    • The "ffmpeg not found" error now carries the install stage's own failure.
  • Behaviour changes.
    • The apt install runs sudo -n. A password prompt is a wait with no deadline, and the group-isolated child has no terminal, so it fails at once and the error says why.
    • which still answers null for "not found", but now rejects when the lookup itself overruns its deadline.

Live-ingestion coverage is unchanged: the gated live suite and its bot-wall-only skip are untouched. A provisioning failure still fails the hook, as a named stage instead of an anonymous timeout. Nothing is skipped or turned into a pass, and no timeout was enlarged.

Finding → commit → test

Finding Commit Test (ytdlp_toolchain.test.ts)
Download body has no deadline 551f8ca5f rejects a body that stops arriving, naming the body stage
Download response has no deadline 551f8ca5f rejects a server that never answers, naming the response stage
Spawned child has no deadline or kill 551f8ca5f stops a stalled child and everything it started, then names the stage
SIGTERM-proof child 551f8ca5f escalates to SIGKILL when the child ignores SIGTERM
Leftovers after the child exits 551f8ca5f stops what a stopped child left behind in its group
Kill reaches only its own children 551f8ca5f leaves processes it did not start alone (bystanders in the worker's group and in their own group)
Stages wired into the real flow 551f8ca5f rejects a stalled ffmpeg lookup by its stage and stops the lookup, caps every stage by the rest of the provisioning budget
Controls 551f8ca5f success download, non-2xx download, exit 0, nonzero exit ('sh -c exit 3' exited 3), unstartable command, full success through provisionToolchain
A download that fails outright (DNS, reset, ENOSPC) rejected as a bare fetch failed 1a9620772 names the stage when the connection itself fails
A failed kill (EPERM) after the deadline was read as a failed start 1a9620772 covered by review: it now logs and keeps waiting for the bounded close (not reproducible without a foreign-uid process)

Every case is synthetic: a loopback HTTP stub and fake sh children. There are no real downloads, no apt and no network.

Verification

Failing on main (09d0b20d2). The new test file cannot import on main, because the seams do not exist there. So the behaviour was reproduced through the public ensureVideoToolchain() alone. The repro is a throwaway test, not committed, using:

  • a warm cache;
  • a fake which on PATH that sleeps and records its pid and its child's pid;
  • a fetch redirected to a loopback stub whose body stalls.
main  stalled-child {"outcome":"still pending after 12000 ms","childPids":[1445,1446],"aliveAfter":[1445,1446]}
main  stalled-body  {"outcome":"still pending after 12000 ms","fetched":["github.com/yt-dlp/yt-dlp/…"]}
main  after the run: `which apt-get` + `sleep 300` still alive (the next unbounded stage, leaked past the suite)
head  stalled-child {"outcome":"rejected: [video-toolchain] ffmpeg lookup: timed out after 10006 ms (deadline 10000 ms)","elapsedMs":10015,"aliveAfter":[]}
head  stalled-body  {"outcome":"rejected: [video-toolchain] yt-dlp download body: timed out after 120000 ms (deadline 120000 ms)","elapsedMs":120049}
head  after the run: no leftover process

Passing on the head:

cd services/platform
bunx vitest --run --project server backend/core/video_links/ytdlp_toolchain.test.ts backend/core/video_links/ytdlp.test.ts backend/core/video_links/ytdlp_live.test.ts
  Test Files  2 passed | 1 skipped (3)   Tests  59 passed | 5 skipped (63)
  (14 new, all passing; the live suite is skipped without YOUTUBE_LIVE_TEST)
bunx oxlint --type-aware --type-check backend/core/video_links/ytdlp_toolchain.ts backend/core/video_links/ytdlp_toolchain.test.ts backend/core/video_links/ytdlp_live.test.ts scripts/dev-engine.ts
  exit 0 (the same invocation reports a planted TS2322 and an unused variable)
bunx lint-staged   (conflict markers, oxfmt, opengrep): all passed

Mutations (each restored afterwards):

  • Group kill replaced by child.kill: 6 of the 13 tests of the first commit fail. These are every grandchild check plus the bystander test.
  • The SIGKILL after the child exits removed: exactly stops what a stopped child left behind in its group fails.
  • ytdlp_toolchain.ts at 551f8ca5f (before the follow-up): names the stage when the connection itself fails fails with got 'fetch failed'.

Concept sweep

  • Callers.
    • scripts/dev-engine.ts (the "Video toolchain" step) calls ensureVideoToolchain() and prints err.message through externalToolchainsUnavailable. Its warning now names the failing stage.
    • The step was awaited in the bun dev boot with no bound; it is now bounded by the same budget.
    • ytdlp.test.ts imports only BGUTIL_PLUGIN_NEST_DIR and bgutilPluginInstallDir, which are unchanged.
  • Docs, locales, migrations: none apply. The strings are dev and test diagnostics, and no docs page names the helper.
  • Security. Error text carries the stage, the command line the helper itself built (cache paths, public asset URLs) and timings. It carries no environment values or secrets. Spawned children inherit the environment exactly as before.

Not in this slice

The root cause of the apt stall is still unknown. Resolving ffmpeg before the hook would remove the heavy system install from the test entirely: either a Unit-job install step with a cache key, or honouring a pre-set VIDEO_INGEST_FFMPEG_LOCATION as an explicit input. That needs a .github/workflows change, which this token cannot push. The same applies to #4010's backend-integration "Install ffmpeg" step. This PR gives the helper-path half of #4010 its diagnostics: stage, elapsed time, deadline.

Closes #4073
Refs #4010

The live YouTube test's self-provisioner had no deadline on its downloads
or on the children it spawned, and the 600 s hook timeout rejected without
cancelling either: a stalled apt-get held the required Unit job for ten
minutes and outlived the suite.

Each stage now runs under its own deadline, capped by a 540 s run budget
that stays below the hook timeout, which is now derived from it. A
download aborts its request. A child is stopped through its own process
group, the runYtdlp shape: SIGTERM, SIGKILL after a 5 s grace, and SIGKILL
for anything it left behind. Only processes the helper started are
touched. The rejection names the stage, its elapsed time and its
deadline, and the "ffmpeg not found" error carries the install stage's
failure. The apt install runs `sudo -n`, because a password prompt is a
wait with no deadline.
A download that fails outright (DNS, reset, a full disk) rejected with
the runtime's bare "fetch failed", which names no stage. It now rejects
as the stage's VideoToolchainError, with the original error as its cause.

A failed kill after the deadline (EPERM) emits 'error' on the child,
which was read as a failed start. It is now logged, and the bounded
wait for 'close' goes on.
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Independent review: accepted at 1a962077243510644795134cb4d76edb451ae939. Merge held: Backend integration is red on an unrelated lane.

  • Reviewer: Tale agent update dashboard ui (#508) #2 3f9fdcee-79e7-430b-a430-ba179d5b5132, run 9fd4795d-7331-4e32-89e0-0e55c5fe8344 (TALE-359 review-merge occurrence, 2026-10-02 12:45 Europe/Zurich).
  • Implementation: agent optimize chat agent #5 718fc56b-b13d-42a9-8543-78297f0272ed, run 3b5dab6d-a3d1-43b4-b499-bb1d9c676c51. The reviewer is distinct.
  • Head: 2 commits on 09d0b20d2, and it merges cleanly with main.

Verdict: this meets #4073.

  • Every stage is bounded, and a stall rejects with an error that names its stage.
  • The child and everything in its process group are stopped, and nothing else is signalled.
  • The 600 s hook is unchanged, and the bound is derived from the budget.

Local checks on the exact head, synthetic only, with no network, apt or brew

  • Unit: ytdlp_toolchain.test.ts passes 14/14, and with ytdlp.test.ts 59/59.

  • My own repro through the public ensureVideoToolchain() alone: a warm cache, and a fake which on PATH that stalls and starts a child.

    Result Processes alive afterwards
    main still pending after 15 s both
    head rejects in 10.02 s with [video-toolchain] ffmpeg lookup: timed out after 10008 ms (deadline 10000 ms) none
  • Mutants: each one is caught.

    • detached: false: 6 of 14 fail.
    • Without the SIGKILL to the group after close: "stops what a stopped child left behind in its group" fails.
    • With stages not capped by the budget: "caps every stage by the rest of the provisioning budget" fails.

CI at the exact head (merge ref af94f37, onto 09d0b20d2)

  • Unit executed: 1,001 files and 81,011 tests passed. The live suite ran for real:
    • the yt-dlp and deno downloads;
    • sudo -n apt-get install -y ffmpeg, which is passwordless on the runner;
    • the bgutil plugin;
    • then 5 tests, 1 skipped, in 124.5 s.
  • Green: Build, E2E, SAST and Commitlint, 49 checks in all. Trivy is neutral, and 11 were skipped by design.
  • Red: Backend integration (job 110803014512), 1320/1321. The failing check is rag watchdog: a row lock held past the settle lock timeout defers that row alone (locked=failed, want running).
    • This PR touches nothing on that path. The same check passed on main 09d0b20d2 (job 110786456545, 1321/1321).
    • A likely mechanism, unverified: the lane releases the row lock before it reads the rows (watchdogs.integration.ts:300 releases it, :303 reads the rows), so a concurrent watchdog tick can settle the row in between.
  • Hold: every check must be green on one final head. Ask: one empty chore: commit from the author or the manager, since my token can't re-run jobs. The next TALE-359 occurrence merges once that head is all green and its tree is still 1a9620772's.

Non-blocking (P3)

  1. The kill reaches only the child's own process group.
    • Proved: with a fake lookup that starts setsid sleep 300, the head rejects only after both graces (20.0 s), and the escaped sleep survives.
    • The realistic case is the apt stage (reasoned; no sudo here to prove it):
      • sudo with use_pty (Ubuntu's default sudoers) runs apt-get in its own session;
      • an unprivileged process cannot SIGKILL a root apt-get in any case.
    • So only sudo's relayed SIGTERM reaches it, though the settle stays bounded.
    • Fix: let the privileged side enforce the bound: sudo -n timeout -k 5 <stage seconds> apt-get install -y ffmpeg.
  2. A misleading message in that case.
    • It says pid N did not exit after SIGKILL although pid N had exited. The escaped descendant held the captured stdout pipe, so close never came.
    • That open pipe also keeps the handle, and the event loop, alive. This part is reasoned.
    • Fix: settle on exit after the group kill, and destroy child.stdout.
  3. ProvisioningClock uses Date.now(). A wall-clock step changes the budget; performance.now() is monotonic.
  4. Six exports exist for the tests only. Knip is green.
  5. Behaviour change, documented in the PR: on a Linux laptop where sudo needs a password, the dev engine's video step now fails at once with the manual-install hint, instead of prompting.

Not covered: no real apt or brew run locally. The live provisioning evidence is CI's. No production contact.

@yannickmonney

Copy link
Copy Markdown
Contributor Author

Root merge acceptance at unchanged head 1a962077243510644795134cb4d76edb451ae939.

Distinct native review: agent #2, run 9fd4795d, separate from author agent #5 / run 3b5dab6d. Root read the three-file change and the detailed review. The local synthetic stalled-lookup and mutant evidence belongs to that reviewer; the live provisioning execution belongs to CI.

The original Backend integration job 110803014512 remains a recorded failure (1320/1321, RAG lock timing). Root requested exactly one normal rerun of that failed job on the same head, without an empty commit. Checks attempt 2, replacement job 110817119162, freshly executed and passed 1321/1321 checks across 214/214 lanes. Current-head rollup: 50 success, 11 intentional skips and 1 neutral Trivy; no pending/failed check. Skipped/neutral outcomes are not counted as executed passes.

The accepted scope is bounded provisioning and cleanup of the original child process group. Escaped descendants and truthful cleanup diagnostics remain #4084; the realistic privileged apt variant needs local proof. Neither this rerun nor this merge resolves that residual. Existing #4010 remains separate. Normal protected squash merge only, matching the exact accepted head, followed by main CI and the ordinary release gate.

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.

bug(ci): bound video toolchain provisioning and stop timed-out children

1 participant