Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
bump: patch
---

### Fixed

- `video-digest` `run-watch.js` now removes its temp video, frame and sheet directories when SIGHUP, SIGINT or SIGTERM interrupts a watch before `watch.json` records them, then exits `129`, `130` or `143`. On POSIX the interrupt kills each child's whole process group, so a yt-dlp ffmpeg grandchild ends too; on Windows only direct children are killed. Cleanup runs once, and the handlers are removed when the run ends.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* A signal that lands while frame extraction runs stops the watch before it writes
* anything from the killed work: no watching manifest, and the run reports failure.
* The signal handler is invoked in-process, with process.exit stubbed.
*/

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { createAcquisitionEnvelope } from "../adapters/adapter-contract.js";
import { acquireMedia } from "../adapters/registry.js";
import { orchestrateWatching } from "../watching/orchestrate-watching.js";
import { writeWatchingManifest } from "../watching/write-watching-manifest.js";
import { runWatchCli } from "./run-watch.js";

vi.mock("@melodic/video-digestion/shared/terminal", () => ({
writeStderr: () => {},
writeStdout: () => {},
}));

vi.mock("../adapters/registry.js", async (importOriginal) => {
const actual = await importOriginal();
return { ...actual, acquireMedia: vi.fn() };
});

vi.mock("../transcript/asr-transcribe.js", async (importOriginal) => {
const actual = await importOriginal();
return { ...actual, detectAsrCapability: vi.fn(async () => ({ available: false })) };
});

vi.mock("../watching/orchestrate-watching.js", () => ({ orchestrateWatching: vi.fn() }));

vi.mock("../watching/write-watching-manifest.js", () => ({
writeWatchingManifest: vi.fn(async () => ({
selectionPath: "selection.json",
coveragePlanPath: "coverage-plan.json",
frameCount: 0,
contactSheetCount: 0,
})),
}));

const URL = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
const ENV_KEYS = ["TMPDIR", "TEMP", "TMP"];

/** @type {string} */
let sandbox;
/** @type {Record<string, string|undefined>} */
let savedEnv;

describe("run-watch interrupted during frame extraction", () => {
beforeEach(async () => {
sandbox = await fs.mkdtemp(path.join(os.tmpdir(), "watch-signal-watching-"));
const fakeTmp = path.join(sandbox, "tmp");
await fs.mkdir(fakeTmp);
savedEnv = Object.fromEntries(ENV_KEYS.map((key) => [key, process.env[key]]));
for (const key of ENV_KEYS) process.env[key] = fakeTmp;
process.env.YOUTUBE_WORK_ROOT = path.join(sandbox, "work");
vi.mocked(acquireMedia).mockReset();
vi.mocked(orchestrateWatching).mockReset();
vi.mocked(writeWatchingManifest).mockClear();
vi.spyOn(process, "exit").mockImplementation(/** @type {any} */ (() => undefined));
});

afterEach(async () => {
vi.restoreAllMocks();
for (const [key, value] of Object.entries(savedEnv)) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
delete process.env.YOUTUBE_WORK_ROOT;
await fs.rm(sandbox, { recursive: true, force: true });
});

it("writes no watching manifest and returns 1 when SIGTERM lands mid-extraction", async () => {
vi.mocked(acquireMedia).mockImplementation(async (_url, { workDir }) => {
const mediaPath = path.join(workDir, "dQw4w9WgXcQ.mp4");
const metadataPath = path.join(workDir, "dQw4w9WgXcQ.info.json");
await fs.writeFile(mediaPath, "binary");
await fs.writeFile(metadataPath, "{}");
return {
success: true,
data: createAcquisitionEnvelope({
entries: [{ mediaPath, captionPaths: [], metadataPath, caption: null }],
metadata: { id: "dQw4w9WgXcQ", title: "Fixture Talk", description: "", chapters: [], comments: [] },
workDir,
}),
};
});
vi.mocked(orchestrateWatching).mockImplementation(async () => {
const handler = /** @type {() => void} */ (process.listeners("SIGTERM").at(-1));
handler();
// A killed ffmpeg leaves a degraded result rather than a rejection.
return /** @type {any} */ ({
selectedFrames: [],
targetMinFrames: 0,
highVolume: false,
candidateCount: 0,
probedDurationSec: null,
densificationWindows: [],
});
});

expect(await runWatchCli(["node", "run-watch.js", URL])).toBe(1);
expect(vi.mocked(orchestrateWatching)).toHaveBeenCalledOnce();
expect(vi.mocked(writeWatchingManifest)).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* A watch killed by SIGHUP, SIGINT or SIGTERM during acquisition removes the temp dirs it
* made and exits with the conventional signal code. A real child run is used: a
* fake `yt-dlp` on PATH blocks the acquisition so the signal lands mid-download.
* POSIX only (the fake yt-dlp is a shell script).
*/

import { spawn } from "node:child_process";
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";

import { afterEach, beforeEach, describe, expect, it } from "vitest";

const RUN_WATCH = fileURLToPath(new URL("./run-watch.js", import.meta.url));
const URL_UNDER_TEST = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
const TEMP_PREFIXES = ["video-extraction-", "video-frames-", "video-sheets-"];

/** @type {string} */
let sandbox;
/** @type {string} */
let fakeTmp;
/** @type {string} */
let started;

async function watchTempDirs() {
const names = await fs.readdir(fakeTmp);
return names.filter((name) => TEMP_PREFIXES.some((prefix) => name.startsWith(prefix)));
}

async function waitFor(predicate, timeoutMs = 20_000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (await predicate()) return;
await new Promise((resolve) => setTimeout(resolve, 50));
}
throw new Error("timed out waiting for the child to reach acquisition");
}

describe.skipIf(process.platform === "win32")("run-watch interrupted by a signal", () => {
beforeEach(async () => {
sandbox = await fs.mkdtemp(path.join(os.tmpdir(), "watch-signal-"));
fakeTmp = path.join(sandbox, "tmp");
const bin = path.join(sandbox, "bin");
started = path.join(sandbox, "yt-dlp-started");
await fs.mkdir(fakeTmp);
await fs.mkdir(bin);
await fs.writeFile(
path.join(bin, "yt-dlp"),
// Keeps writing into the work dir, as a real download would, so a signal that
// leaves it running races the cleanup and recreates files after the rm.
`#!/bin/sh\ntouch "${started}"\nwhile :; do\n for d in "$TMPDIR"/video-extraction-*; do\n [ -d "$d" ] && echo x >> "$d/partial.part"\n done\n sleep 0.05\ndone\n`,
{ mode: 0o755 },
);
});

afterEach(async () => {
await fs.rm(sandbox, { recursive: true, force: true });
});

it.each([
["SIGTERM", 143],
["SIGINT", 130],
])("%s removes the temp dirs and exits %i", async (signal, exitCode) => {
const child = spawn(process.execPath, [RUN_WATCH, URL_UNDER_TEST], {
env: {
...process.env,
PATH: `${path.join(sandbox, "bin")}${path.delimiter}${process.env.PATH}`,
TMPDIR: fakeTmp,
VIDEO_DIGEST_WORK_ROOT: path.join(sandbox, "work"),
},
stdio: "ignore",
});
const exited = new Promise((resolve) => child.on("exit", (code, sig) => resolve({ code, sig })));

await waitFor(() => existsSync(started));
expect(await watchTempDirs()).toHaveLength(3);

child.kill(signal);
expect(await exited).toEqual({ code: exitCode, sig: null });
expect(await watchTempDirs()).toEqual([]);
// A surviving fake yt-dlp would recreate the work dir's file within one tick.
await new Promise((resolve) => setTimeout(resolve, 300));
expect(await watchTempDirs()).toEqual([]);
});

it.each([
["SIGTERM", 143],
["SIGHUP", 129],
])("%s also ends a grandchild the yt-dlp child started", async (signal, exitCode) => {
const grandPid = path.join(sandbox, "grandchild.pid");
// A real yt-dlp runs ffmpeg as its own child; this one leaves a writer looping
// in the background, which outlives a SIGKILL sent to yt-dlp alone.
await fs.writeFile(
path.join(sandbox, "bin", "yt-dlp"),
`#!/bin/sh\n( while :; do\n for d in "$TMPDIR"/video-extraction-*; do\n [ -d "$d" ] && echo x >> "$d/merge.part"\n done\n sleep 0.05\ndone ) &\necho $! > "${grandPid}"\ntouch "${started}"\nwait\n`,
{ mode: 0o755 },
);
const child = spawn(process.execPath, [RUN_WATCH, URL_UNDER_TEST], {
env: {
...process.env,
PATH: `${path.join(sandbox, "bin")}${path.delimiter}${process.env.PATH}`,
TMPDIR: fakeTmp,
VIDEO_DIGEST_WORK_ROOT: path.join(sandbox, "work"),
},
stdio: "ignore",
});
const exited = new Promise((resolve) => child.on("exit", (code, sig) => resolve({ code, sig })));

await waitFor(() => existsSync(started));
const pid = Number(await fs.readFile(grandPid, "utf8"));
const alive = () => {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
};
try {
child.kill(signal);
expect(await exited).toEqual({ code: exitCode, sig: null });
await waitFor(() => !alive(), 2_000).catch(() => {});
expect(alive()).toBe(false);
await new Promise((resolve) => setTimeout(resolve, 300));
expect(await watchTempDirs()).toEqual([]);
} finally {
if (alive()) process.kill(pid, "SIGKILL");
}
// A surviving grandchild holds yt-dlp's pipes open, so the run waits out its
// settle window before exiting; the timeout leaves room for that failure.
}, 30_000);
});
Loading
Loading