Files
Strata/app/host/tests/process-tree.test.mjs
T
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

92 lines
6.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { readFileSync, rmSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { endTree, endTreeThenKill, groupAlive, ownGroup } from '../server/process-tree.mjs';
import { runCommand } from '../server/agent/custom-command-run.mjs';
const isWin = process.platform === 'win32';
const sleep = ms => new Promise(r => setTimeout(r, ms));
// Whether the process is there. A process that has ended but whose parent has not collected it yet (a zombie: Node reaps its own children when the event
// loop gets to it, init reaps the orphans when it gets to them) answers signal 0 and is not running: on a loaded machine that gap is long.
function alive(pid) {
try { process.kill(pid, 0); } catch (error) { return error.code === 'EPERM'; }
if (process.platform === 'linux') {
try { return readFileSync(`/proc/${pid}/stat`, 'utf8').replace(/^.*\) /, '')[0] !== 'Z'; } catch { return false; }
}
return true;
}
// Every wait is on the real condition, with a long limit: it returns the moment the condition holds, so the limit costs nothing when the machine is fine.
async function until(condition, ms = 30_000) { for (let waited = 0; waited < ms; waited += 50) { if (condition()) return true; await sleep(50); } return condition(); }
const gone = (pid, ms) => until(() => !alive(pid), ms);
// A program that starts a program of its own and reports its pid, the way Stop meets a job and its revision command (#4529 #4570).
const parentScript = `const { spawn } = require('node:child_process'); const k = spawn(process.execPath, ['-e', 'setTimeout(()=>{}, 120000)'], { stdio: 'ignore' }); console.log(k.pid); setTimeout(()=>{}, 120000);`;
async function tree() {
const child = spawn(process.execPath, ['-e', parentScript], { detached: ownGroup(), stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true });
const kid = await new Promise(resolve => child.stdout.once('data', b => resolve(Number(String(b).trim()))));
return { child, kid };
}
test('endTree ends the program and the program it started, and nothing else (#4529 #4570)', async () => {
const outsider = spawn(process.execPath, ['-e', 'setTimeout(()=>{}, 120000)'], { stdio: 'ignore', windowsHide: true }); // same program, not started by the job
const { child, kid } = await tree();
try {
assert.ok(alive(kid));
endTree(child);
assert.ok(await gone(child.pid), 'the job survived');
assert.ok(await gone(kid), 'what the job started survived');
assert.ok(alive(outsider.pid), 'a process of the same program that the job did not start was ended');
} finally { outsider.kill(); try { process.kill(kid, 'SIGKILL'); } catch { /* gone */ } child.kill('SIGKILL'); }
});
test('endTreeThenKill follows a TERM that is ignored with a KILL of the group', { skip: isWin && 'POSIX signals' }, async () => {
// Both programs ignore SIGTERM, and the test goes on only when both have said that the handler is in place (no fixed delay for a slow start).
const stubborn = `const { spawn } = require('node:child_process'); process.on('SIGTERM', () => {});
const k = spawn(process.execPath, ['-e', "process.on('SIGTERM',()=>{});console.log('up');setTimeout(()=>{},120000)"], { stdio: ['ignore', 'pipe', 'ignore'] });
k.stdout.once('data', () => { console.log(k.pid); }); setTimeout(()=>{}, 120000);`;
const child = spawn(process.execPath, ['-e', stubborn], { detached: true, stdio: ['ignore', 'pipe', 'ignore'] });
const kid = await new Promise(resolve => child.stdout.once('data', b => resolve(Number(String(b).trim()))));
try {
assert.ok(alive(kid));
endTreeThenKill(child, 500);
assert.ok(await gone(kid, 60_000), 'the group survived SIGTERM and was not killed');
assert.ok(await gone(child.pid, 60_000));
assert.ok(await until(() => !groupAlive(child), 60_000), 'a member of the group is still there');
} finally { try { process.kill(-child.pid, 'SIGKILL'); } catch { /* gone */ } }
});
test('a child that leads no group of its own is still ended, alone, as before', async () => {
const child = spawn(process.execPath, ['-e', 'setTimeout(()=>{}, 120000)'], { stdio: 'ignore', windowsHide: true });
endTree(child);
assert.ok(await gone(child.pid));
});
test('a custom command that runs out of time takes the programs it started with it', { skip: isWin && 'POSIX shell' }, async () => {
const marker = join(tmpdir(), `strata-cc-${process.pid}.pid`);
rmSync(marker, { force: true });
// The limit is far above what the shell needs to start and write its marker, so a slow machine cannot end the command before it did.
const result = await runCommand({ command: `sleep 120 & echo $! > ${marker}; wait`, timeoutSec: 8 });
assert.equal(result.ok, false);
assert.ok(await until(() => existsSync(marker) && /^\d+/.test(readFileSync(marker, 'utf8'))), 'the command never wrote its marker');
const pid = Number(readFileSync(marker, 'utf8').trim());
rmSync(marker, { force: true });
assert.ok(await gone(pid), 'the command\'s own child outlived the time limit');
});
test('ending a child that never started leaves the caller and its process group alone (macOS signalled the whole group)', { skip: isWin && 'POSIX process groups' }, async () => {
// A program that fails to start a child and ends it before the "error" event arrives, the way a Stop can meet a job whose Python is missing.
// It leads a group of its own, so if the signal reaches its group it dies by SIGTERM and the test fails; this process is never in the line of fire.
const body = `import { spawn } from 'node:child_process'; import { endTree, endTreeThenKill } from ${JSON.stringify(new URL('../server/process-tree.mjs', import.meta.url).href)};
const c = spawn('strata-no-such-program-xyz', [], { stdio: 'ignore' }); c.on('error', () => {}); endTree(c); endTreeThenKill(c, 10); setTimeout(() => console.log('survived'), 300);`;
const child = spawn(process.execPath, ['--input-type=module', '-e', body], { detached: true, stdio: ['ignore', 'pipe', 'ignore'] });
let out = ''; child.stdout.on('data', b => { out += b; });
const code = await new Promise(resolve => child.on('close', (c, s) => resolve(s ?? c)));
assert.equal(code, 0, `the caller was signalled (${code})`);
assert.match(out, /survived/);
});