Repository navigation
fix(platform): contain video-toolchain children that leave their group - #4096
Conversation
ProvisioningClock and every stage's elapsed time read Date.now(), so a wall-clock step spent or granted provisioning budget: one hour forward left a fresh run 0 ms for every stage, and a step back printed a negative "timed out after" figure. They read performance.now() now; the fixed overall and stage budgets are unchanged.
spawnBounded settled a stopped stage on `close`, which also waits for every holder of a captured stdout. When a process the child started outside its group held that pipe, the stage rejected two graces later with "pid N did not exit after SIGKILL" although pid N had exited, and the open pipe kept the process alive until the holder ended. It now tells the two apart by the child's own `exit`: the error says the child exited (and how) while something it started still holds its stdout, or that it did not exit. Either way it closes that pipe on its side and unrefs a child that never exits, so no handle outlives the stage's bound.
A stopped stage signalled only its child's process group, so anything the child started in a group or session of its own (`setsid`, GNU `timeout`, a job-control shell) outlived the stage: #4084's synthetic lookup starting `setsid sleep 300` kept running, and kept the captured stdout open. At the deadline and at the SIGKILL step, spawnBounded now also lists, through /proc, the members of the child's session in another group and the child's descendants, and signals each individually beside the group. It lists only while the child is unreaped, when its pid cannot name an unrelated parent, group or session; it signals a listed process only while its pid still carries the same start time; and it signals nothing it did not find that way, so unrelated processes are untouched. After the child closes, the stage settles once those are gone too, within the same final grace, and the error names any that outlive it or could not be signalled. Without /proc (macOS) only the group is reached, as before, and a held stdout is reported.
The apt stage ran `sudo -n apt-get install -y ffmpeg`. This process can signal sudo but not the root apt-get it starts: only sudo's relayed SIGTERM reaches it, and nothing once sudo is SIGKILLed, so a stalled install could outlive its stage bound. It now runs `sudo -n timeout -k <kill grace> <stage bound> apt-get install -y ffmpeg`: GNU timeout, as root, sends the same SIGTERM and SIGKILL at the same bound from the privileged side. The overall and stage budgets are unchanged; an exhausted budget passes 1 ms, since timeout reads 0 as no limit. A sudoers rule that allows apt-get alone now refuses the install at once, and the stage reports the manual-install hint, as it does for a sudo that needs a password.
|
Independent review of PR #4096 at
The module is correct in what it claims:
The PID-marker question (manager
MEDIUM 1: make the markers race-free, with no longer deadlines.
MEDIUM 2: an empty marker becomes pid 0, and the cleanup then SIGKILLs the test runner's own process group.
LOW
INFO
Repairs go through the manager. I'll re-review the next exact head. The fixture fix shouldn't touch the module. |
Independent review of #4096 at b712da4 asked for two fixture repairs in ytdlp_toolchain.test.ts; the module itself was accepted. The stalling fixtures wrote their pid file only after forking, while the stage's 2 s deadline ran from spawn(). A runner too loaded to reach that line in time had the fixture stopped first: the timeout assertion passed, and the pid read then failed with ENOENT. A fixture that sleeps 3 s first fails that way every time on b712da4. Each fake process now appends its own pid to its marker as its first command, before any trap, fork or setsid, so a process missing from the marker never started, and a missing marker reads as none. A test that needs its fixture running fakes the module's timers alone (setTimeout, setInterval and their clears), waits on real time until every process has recorded itself and the last one has reached its sleep (its trap and setsid done), then runs the deadline and lets the kill graces follow real time; performance.now() stays real. A late-starting fixture is now a test, with readiness controls: past the deadline in real time the stage has not settled, and the escaped child already has a session of its own. An empty or blank marker parsed as pid 0, and the cleanup SIGKILLed whatever it read: to kill(), 0 is the worker's own process group. A marker line now counts only when complete and a whole number above 1; every pid the cleanup keeps is pinned to its /proc start time when it is first seen, and is signalled only while that start time still matches. Without /proc nothing is signalled. A parser test covers '' and ' ', and a pin test covers a pid handed to another process. ytdlp_toolchain.ts is unchanged.
|
Independent re-review of PR #4096 at
Local runs at this head [local]
Disposition of my previous findings
New LOW [source]
Still open from the earlier review, not required for this repair (LOW/INFO): the module LOWs 1–3; the fake Next: the merge needs the Unit job, and every required check, green on |
|
Merged as |
Five commits on
main19776cf18: four fixes, then a repair of the test fixture asked for by the independent review (2716aba72, below). They fix the residual of #4073 that #4079's independent review found (receipt), filed as #4084. #4079's ordinary-process fix is unchanged: the group kill, the stage budgets, the 600 s hook and its tests. These commits only add to it.What changed
services/platform/backend/core/video_links/ytdlp_toolchain.ts(spawnBounded,ensureFfmpeg,ProvisioningClock) and its unit test./proc. It finds two kinds: members of the child's session in another group (setpgid), and the child's descendants (setsid).exit: either the child did not exit, or it exited while something it started still holds its stdout.cleanup incomplete:suffix.sudo -n timeout -k <kill grace> <stage bound> apt-get install -y ffmpeg. As root, GNUtimeoutsends the same SIGTERM, then SIGKILL, at the same bound. An exhausted budget passes 1 ms, becausetimeout 0means no limit.ProvisioningClockand every elapsed figure readperformance.now(). A wall-clock step no longer spends or grants budget, and a step back can no longer print a negative "timed out after".The fixed budgets are unchanged:
totalMs540 s, the stage deadlines, and the 5 s kill grace.Finding → commit → test map
main, passes on headProvisioningClockusesDate.now()a7bf2195efix(platform): keep toolchain budgets on the monotonic clockmonotonic accounting › spends no budget on a wall-clock step;… › reports the real elapsed time across a wall-clock step back0a22861f8fix(platform): say what a toolchain cleanup left, and close its pipeprovisionToolchain bounds › reports a stdout holder it could not stop, and keeps no handle opensetsid sleepsurvives67a9f3c17fix(platform): stop toolchain children that leave their process groupbounded children › stops what the child started outside its process group;… › leaves processes it did not start alone while it stops one outside the group(bystander control);provisionToolchain bounds › stops a lookup child holding its stdout outside the group, and keeps no handle open(the receipt's case)b712da415fix(platform): bound the privileged apt-get on its own sideprovisionToolchain bounds › bounds the privileged install on its own sideb712da415, MEDIUM 1 and 2: racy pid markers; an empty marker read as pid 02716aba72test(platform): race-free toolchain pid markers, and a pinned cleanupOrdinary success, same-group termination and the original bystander test are unchanged and pass: all 14 of #4079's tests.
Review repair: race-free pid markers and a pinned cleanup (
2716aba72)The independent review of
b712da415accepted the module and asked for two repairs to the test fixture.2716aba72touches onlyytdlp_toolchain.test.ts. The module blob is unchanged (a7a68d5a4).spawn(). On a loaded runner the timeout assertion passed, and the pid read then failed with ENOENT. This affects all 13 marker reads, 7 of them new in this PR.setsid, and a missing marker reads as none. A test that needs its fixture running fakes only the module's timers (setTimeout,setIntervaland their clears). It waits on real time until every process has recorded itself and the last one has reached itssleep(trap andsetsiddone). It then runs the deadline and lets the kill graces follow real time (vi.setTimerTickMode('interval')).performance.now()stays real.bounded children › holds the deadline for a child that starts late, then stops it outside the group: its first line sleeps 3 s. Readiness controls: past the deadline in real time, the stage has not settled, and the escaped child already has its own session. Each escape test checks the escapee's session before the deadline.0(''.trim().split(' ').map(Number),Number('')), andafterEachSIGKILLed it. Tokill(), 0 is the worker's own process group.isPid). Every pid the cleanup keeps is pinned to its/procstart time when first seen (own). The cleanup signals it only while that start time still matches (stillOwned). Without/proc, nothing is signalled.fixture pids › reads no pid from an empty, blank or unfinished marker, or a line that is not one('',' ', …);… › leaves the cleanup only whole pids above 1;… › leaves the cleanup a pid only while it names the process it was pinned totimeout 2sis not held. On a runner too loaded to start apt-get within it,timeoutends apt-get before it records itself, and the readiness wait ends withtimeout.timed out after 14 ms (deadline 2000 ms). The monotonic test still runs on real timers.Verified at
2716aba72. All runs were local and synthetic, and no signal to pid 0 was ever executed.ytdlp_toolchain.test.tspasses 25/25 in six runs in a row, about 9 s each (it was about 27 s).b712da415, a 3 s sleep before the stalling script's publish line fails all 9 tests that read its pid file with ENOENT, each after its timeout assertion passed.2716aba72, with a 3 s sleep before the first command of every fake process, all 25 pass, and the privileged test takes thetimeoutpath.isPidaccepting 0 and 1;stillOwnedwithout the start time;startedInwithout its wait: the 5 escape-path tests go red;main358f69107:git merge-treeis clean, and bothvideo_linksblobs are identical.video_links/passes 148 with 5 skipped (the gated live suite).oxlint --type-aware --type-checkexits 0, and a planted probe confirms it reports.How I verified it (at
b712da415)Everything ran locally and is synthetic: fakes on
PATH, a loopback stub,setsid, GNUtimeout. There was no sudo, no apt, no root, no network and no production contact. Host: Node 24.21.0, Bun 1.4.2, Linux, util-linux 2.38.1, coreutils 9.1.Head
b712da415:Each commit on its own (
ytdlp_toolchain.test.ts):a7bf2195e16/16,0a22861f817/17,67a9f3c1720/20,b712da41521/21.The new tests against
main's module. I swapped ingit show origin/main:…/ytdlp_toolchain.ts, ran with-t "monotonic|outside|stdout holder|privileged", then restored the file. All 7 fail:Mutants:
destroy(), the handle check fails (expected 5 to be 4).Synthetic repro through the public
provisionToolchain(). It uses a warm cache and runs each scenario in its own process undertimeout 25. "Exit" is how that process ended.main(node)setsid sleep 300and waits on itPipeWrapopen; process killed bytimeout 25(exit 124)setsid -f sleep 300, re-parented away at once, and exits 0cleanup incomplete: pid 5947 exited with code 0, but a process it started still holds its stdout after the group SIGKILL; pipe closed; exits 0 by itself. The holder could not be found, so it is not signalled.sudohides its command in its own session, re-parented away, and relays only SIGTERM; apt-get ignores SIGTERMapt-get install -y ffmpeg; apt-get survivestimeout -k 0.2s 2s apt-get install -y ffmpeg; apt-get gone by the time the stage settlesProvisioningClock, with the wall clock stepped one hour forwardstage(10000)→{ms: 0, budgetMs: 540000}{ms: 10000}Stand-in and fake scripts used
Sweep
ytdlp_live.test.ts: the hook staystotalMs + 60 s. CI's live run exercises the realsudo -n timeout … apt-getpath on the runner. This PR adds no skips and no hook time.ytdlp.ts:runYtdlp: it uses the same group kill. yt-dlp, ffmpeg and deno stay in their group, and none of them changes session, so it is not touched here.timeout-minutesand the APT timeouts. It is a separate path and unchanged here.docs/orservices/docsmentions this helper.comm(the executable name, 15 characters at most). They never include arguments or environment values. Nothing reads/proc/*/environ.Behaviour changes
apt-get(and nottimeout) fails the same way./proc(macOS). The stage still reaches only the group, as before. A held stdout is reported and closed rather than waited on.Not covered
Closes #4084
Refs #4073