Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: tree-kill a cancelled video render on Windows (#4171)

Adam Eivy34713fcd472e
diff --git a/.changelog/next/fixed-issue-4171.md b/.changelog/next/fixed-issue-4171.md
new file mode 100644
index 0000000000..fa8072b5c4
--- /dev/null
+++ b/.changelog/next/fixed-issue-4171.md
@@ -0,0 +1 @@
+- Cancelling a video render on Windows now tree-kills the runner, so ffmpeg/download children die with it instead of orphaning and holding GPU memory
diff --git a/server/lib/README.md b/server/lib/README.md
index 964d00ce4f..339a86e706 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -191,7 +191,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `commandExists.js` | `commandExists(cmd, args = ['--version'], { timeoutMs = 5_000 })` — does running `cmd args` succeed? A capability probe (`execFile`-based), not a PATH lookup like `processEnv.js`'s `whichFirst`. Consolidates the two previously-private copies in `localLlm.js`/`ollamaManager.js`; callers probing a heavier CLI (e.g. `codeReview.js`'s reviewer-binary probe) pass a longer `timeoutMs`. |
| `spawnCwd.js` | `resolveSpawnCwd(workspacePath, fallbackRoot, label)` — resolves and **logs** the working directory a run/agent spawns into (expanding `~`), and throws when a workspace was requested but is missing / not a directory. Behind `services/runner.js#resolveRunCwd`, which turns that throw into a normal failed-run record for the two spawning runners. Stops a bad app `repoPath` from silently spawning in the PortOS checkout (#3180). `withSpawnCwdEnv(env, cwd)` — returns a copy of `env` with `PWD` pinned to `cwd` (dropping stale case-variant keys), because `spawn({ cwd })` doesn't rewrite the inherited `PWD` and OpenCode resolves its project root as `process.env.PWD ?? process.cwd()` (#3193). Apply it at every spawn that names its own cwd — the shared wrappers (`bufferedSpawn`, `spawnDetached`) already do, so their callers inherit it. `spawnCwd.test.js` discovers cwd-passing spawns across `server/` and fails on any that neither pins nor is listed exempt.
| `commandSecurity.js` | Allowlist of safe shell commands + `validatePm2Command(args)` (rejects daemon-wide `pm2 kill`/`startup`/`unstartup` and `<verb> all`). `validateCommand` runs the pm2 check for `pm2` base commands. Mirrored by the `agentGuard/` PATH shim for agentic paths. |
-| `detachedSpawn.js` | `spawnDetached(bin, args, {controlDir,env,cwd,killProcessGroup?})` → ChildProcess-like handle for a long media job that SURVIVES `pm2 restart portos-server`. A pure-`sh` double-fork reparents the job to init (escaping pm2's PPID-based TreeKill — `detached:true` alone doesn't, since it only changes the process group); the server tails on-disk log files for `stdout`/`stderr`/`close`. Group-kill mode persists a marker so cancel, reattach, and orphan reaping terminate a group-leader wrapper plus every runtime child together. Used by loraTraining + videoGen. Also exports `reattachDetached(controlDir)` / `isReattachable(controlDir)` to RE-ATTACH a survivor after a restart (boot re-attach, #1332) and `reapDetached` to checkpoint-kill one when re-attach isn't possible. |
+| `detachedSpawn.js` | `spawnDetached(bin, args, {controlDir,env,cwd,killProcessGroup?})` → ChildProcess-like handle for a long media job that SURVIVES `pm2 restart portos-server`. A pure-`sh` double-fork reparents the job to init (escaping pm2's PPID-based TreeKill — `detached:true` alone doesn't, since it only changes the process group); the server tails on-disk log files for `stdout`/`stderr`/`close`. Group-kill mode persists a marker so cancel, reattach, and orphan reaping terminate a group-leader wrapper plus every runtime child together. Windows has no double-fork (plain-spawn fallback), so its handle's `kill` delegates to `killProcessTree` (`taskkill /T /F`) — a cancel there takes the runner's children with it. Used by loraTraining + videoGen. Also exports `reattachDetached(controlDir)` / `isReattachable(controlDir)` to RE-ATTACH a survivor after a restart (boot re-attach, #1332) and `reapDetached` to checkpoint-kill one when re-attach isn't possible. |
| `hostShutdown.js` | Tells "PortOS was restarted out from under a running agent" apart from "the agent failed" (#3202). `markHostShuttingDown()` / `isHostShuttingDown()` are the in-process latch the SIGTERM/SIGINT handler sets first thing; `shouldAbandonForHostShutdown({sentinelPresent,terminatedByUser,paused})` keeps every spawn path on the same preserve-vs-finalize policy. `writeHostShutdownMarker({agentIds,signal})` / `readHostShutdownMarker()` / `clearHostShutdownMarker()` persist that verdict to `data/cos/host-shutdown.json` so the NEXT boot's orphan sweep can requeue those agents as *interrupted* — no orphan-retry charge, no 30-minute cooldown. All non-throwing: a missing marker degrades to the ordinary orphan path. |
| `execGit.js` | `execGit` utility imported by `git.js` + worktree manager. |
| `ffmpeg.js` | Shared ffmpeg helpers (videoGen + videoTimeline). Includes `probeFrameCount(videoPath)` (metadata `nb_frames`, falling back to a real `-count_frames` pass — expensive, so call it once per file) and `trimVideoFromFrame(videoPath, outPath, {startFrame, fps})` — a frame-EXACT head cut via the `trim` filter (an `-ss` seek can drift a frame, which reads as a stutter at a stitch seam), keeping audio in sync with `atrim` when the clip has a soundtrack and taking `-an` when it doesn't. Re-encodes by necessity, so a later concat must re-encode too; `outPath` may equal the input (temp-file + rename install). Used by the chained-render context window — see `videoContinuity.js`. `buildTrimConcatArgs({inputs, outPath, width, height, fps, withAudio})` builds the argv for the other half of that job: a concat that drops leading frames from some of its inputs inside a `filter_complex` graph, so the cuts ride along with the timeline encode instead of costing one pre-encode per clip (pass `withAudio` only when EVERY input has an audio stream — check with `hasAudioStream`). `H264_ENCODE_ARGS` / `AAC_ENCODE_ARGS` are the shared encode profile: clips produced by different paths here get concatenated together, so a mismatch shows up as one segment graded differently from its neighbours — spread these rather than re-typing the flags. Every re-encode here also pins BT.709: `BT709_CONTAINER_ARGS` (the `colr` atom, always emitted) plus `bt709TagFilter()` → the `BT709_TAG_FILTER` `setparams=…` string, or `null` on an ffmpeg without that filter (`supportsSetparamsFilter()` probes `-filters` once per process; `null` = not probed, and a probe that couldn't run stays uncached). Both halves are required — from ffmpeg 8 the encoder reads color properties off the FRAMES, silently overriding the container flags, so a flags-only output decodes washed-out. `buildTrimConcatArgs` takes the filter as its `colorTagFilter` option rather than probing, to stay pure. |
diff --git a/server/lib/detachedSpawn.js b/server/lib/detachedSpawn.js
index f0bf0c9d36..43a347276d 100644
--- a/server/lib/detachedSpawn.js
+++ b/server/lib/detachedSpawn.js
@@ -48,6 +48,7 @@ import { EventEmitter } from 'events';
import { constants as osConstants } from 'os';
import { join } from 'path';
import { open, readFile, writeFile, rm, stat, readdir } from 'fs/promises';
+import { killProcessTree } from './bufferedSpawn.js';
import { ensureDir, sleep } from './fileUtils.js';
import { withSpawnCwdEnv } from './spawnCwd.js';
@@ -230,6 +231,7 @@ function createLogTailer(handle, { controlDir, pollMs, cleanup }) {
* @param {boolean} [opts.killProcessGroup] - signal `-pid` on cancel/reap so a
* group-leader wrapper and every runtime child terminate together. The job is
* responsible for establishing its own process group before spawning children.
+ * POSIX-only — the win32 fallback's `kill` always tree-kills, group or not.
* @returns {Promise<object>} ChildProcess-like handle (resolves once the PID is known)
*/
export async function spawnDetached(bin, args = [], {
@@ -244,7 +246,27 @@ export async function spawnDetached(bin, args = [], {
// exitCode / signalCode), so callers are unaffected. Surviving a pm2 restart
// is a POSIX-only guarantee; Windows keeps its prior spawn semantics.
if (process.platform === 'win32') {
- return spawn(bin, args, { env: withSpawnCwdEnv(env ?? process.env, cwd), cwd, stdio: ['ignore', 'pipe', 'pipe'] });
+ const child = spawn(bin, args, { env: withSpawnCwdEnv(env ?? process.env, cwd), cwd, stdio: ['ignore', 'pipe', 'pipe'] });
+ // A bare `child.kill()` terminates ONLY the runner. Windows has no process
+ // group for the POSIX `-pid` trick `killProcessGroup` relies on, so whatever
+ // the runner spawned (the ffmpeg mux a CUDA video runtime shells out to, a
+ // model download) survives as an orphan still holding the output file and
+ // GPU memory. Delegate to the shared tree-killer instead — `taskkill /T /F`
+ // on Windows, the POSIX group signal elsewhere (#4171). Note `taskkill /T /F`
+ // ignores the requested signal and force-kills, so a SIGTERM cancel is not
+ // graceful here; that is killProcessTree's documented Windows contract.
+ const nativeKill = child.kill.bind(child);
+ // What we hand killProcessTree: inherits from `child` (so `pid` reads
+ // through and `instanceof ChildProcess` still holds — the taskkill branch is
+ // gated on it) but exposes Node's own kill, so killProcessTree's POSIX
+ // fall-through can never re-enter the override below.
+ const treeKillTarget = Object.create(child, { kill: { value: nativeKill } });
+ child.kill = (signal = 'SIGTERM') => {
+ child.killed = true;
+ killProcessTree(treeKillTarget, signal, { processGroup: true });
+ return true;
+ };
+ return child;
}
const handle = new EventEmitter();
diff --git a/server/lib/detachedSpawn.test.js b/server/lib/detachedSpawn.test.js
index e9ea9ad35f..ea368f1876 100644
--- a/server/lib/detachedSpawn.test.js
+++ b/server/lib/detachedSpawn.test.js
@@ -1,12 +1,21 @@
-import { describe, it, expect, afterEach } from 'vitest';
+import { describe, it, expect, afterEach, vi } from 'vitest';
import { mkdir, mkdtemp, rm, stat, writeFile } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
-import { execFile } from 'child_process';
+import { ChildProcess, execFile } from 'child_process';
import { promisify } from 'util';
import { pinPlatform } from './testHelper.js';
+import { killProcessTree } from './bufferedSpawn.js';
import { spawnDetached, reapDetached, reapAndCleanDetachedDirs, reattachDetached, isReattachable, isDetachedRunning } from './detachedSpawn.js';
+// Only the win32 fallback's kill() reaches killProcessTree, so stubbing it is
+// inert for every POSIX test here — and it lets the win32 test assert the
+// delegation on a platform where `taskkill` doesn't exist.
+vi.mock('./bufferedSpawn.js', async (importOriginal) => ({
+ ...(await importOriginal()),
+ killProcessTree: vi.fn(),
+}));
+
const execFileAsync = promisify(execFile);
// spawnDetached's POSIX `sh` double-fork — and with it the whole control-dir
@@ -219,6 +228,44 @@ describe('spawnDetached', () => {
}
});
+ it('win32 fallback kill() tree-kills so the runner\'s children die with it', async () => {
+ const restorePlatform = pinPlatform('win32');
+ try {
+ killProcessTree.mockClear();
+ const controlDir = await tmpControlDir();
+ const handle = await spawnDetached('sh', ['-c', 'sleep 30'], { controlDir });
+ const closed = onClose(handle);
+ expect(handle.kill('SIGKILL')).toBe(true);
+ expect(handle.killed).toBe(true);
+ expect(killProcessTree).toHaveBeenCalledTimes(1);
+ const [target, signal, opts] = killProcessTree.mock.calls[0];
+ expect(signal).toBe('SIGKILL');
+ expect(opts).toEqual({ processGroup: true });
+ // The target must still be a real ChildProcess — killProcessTree's
+ // `taskkill /T /F` branch is gated on `instanceof ChildProcess` — and must
+ // carry Node's own kill, not the override, so its POSIX fall-through
+ // can't recurse back into it.
+ expect(target).toBeInstanceOf(ChildProcess);
+ expect(target.pid).toBe(handle.pid);
+ expect(target.kill).not.toBe(handle.kill);
+ // killProcessTree is stubbed, so nothing actually died — reap the sleeper.
+ target.kill('SIGKILL');
+ await closed;
+ } finally {
+ restorePlatform();
+ }
+ });
+
+ it.runIf(IS_POSIX)('leaves the POSIX path on its own pid/group signalling (no tree-kill)', async () => {
+ killProcessTree.mockClear();
+ const controlDir = await tmpControlDir();
+ const handle = await spawnDetached('sh', ['-c', 'sleep 30'], { controlDir, pollMs: 25 });
+ await new Promise((r) => setTimeout(r, 50));
+ expect(handle.kill('SIGKILL')).toBe(true);
+ expect(killProcessTree).not.toHaveBeenCalled();
+ await onClose(handle);
+ });
+
describe('reapDetached', () => {
it.runIf(IS_POSIX)('SIGTERMs a surviving orphan and reports it reaped', async () => {
const controlDir = await tmpControlDir();