Skip to content

Commit 0ff2a80

Browse files
committed
fix(scripts): enforce npm runtime validation timeout
1 parent 87607f7 commit 0ff2a80

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎scripts/build-npm-launcher.test.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ fs.readFileSync = (file, ...rest) => {
352352
"#!/t3-test-missing-loader\n",
353353
'#!/bin/sh\necho "runtime loader failed" >&2\nexit 42\n',
354354
"#!/bin/sh\nkill -TERM $$\n",
355+
'#!/bin/sh\ntrap \'echo caught > "$T3CODE_TEST_TERM"; exit 42\' TERM\necho ready > "$T3CODE_TEST_READY"\nwhile :; do :; done\n',
355356
]) {
356357
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-npm-validation-test-" });
357358
const output = yield* buildNpmLauncher({
@@ -361,19 +362,40 @@ fs.readFileSync = (file, ...rest) => {
361362
const release = yield* serveFakeRelease(root, executable);
362363
const home = path.join(root, "home");
363364
const dir = path.join(home, "runtime/versions", VERSION);
365+
const probeTimeoutHook = path.join(root, "probe-timeout.mjs");
366+
yield* fs.writeFileString(
367+
probeTimeoutHook,
368+
`import childProcess from "node:child_process";
369+
import { syncBuiltinESMExports } from "node:module";
370+
const { spawnSync } = childProcess;
371+
childProcess.spawnSync = (command, args, options) => spawnSync(command, args,
372+
options?.timeout === undefined ? options : { ...options, timeout: 500 });
373+
syncBuiltinESMExports();
374+
`,
375+
);
364376
const env = {
365377
PATH: process.env.PATH,
366378
T3CODE_HOME: home,
367379
T3CODE_RELEASE_BASE_URL: release.baseUrl,
380+
T3CODE_TEST_READY: path.join(root, "probe-ready"),
381+
T3CODE_TEST_TERM: path.join(root, "probe-term"),
368382
};
369383
const launcher = path.join(output.packageDir, "bin/t3.mjs");
370-
const failed = yield* run(process.execPath, [launcher, "serve"], { cwd: root, env });
384+
const failed = yield* run(
385+
process.execPath,
386+
["--import", probeTimeoutHook, launcher, "serve"],
387+
{ cwd: root, env },
388+
);
371389
assert.equal(failed.exitCode, 1, failed.stderr);
372390
assert.deepStrictEqual(yield* fs.readDirectory(path.dirname(dir)), [], failed.stderr);
373391
assert.include(failed.stderr, "could not run");
374-
if (executable.includes("exit 42")) {
392+
if (executable.includes("runtime loader failed")) {
375393
assert.include(failed.stderr, "runtime loader failed");
376394
}
395+
if (executable.includes("trap")) {
396+
assert.isTrue(yield* fs.exists(env.T3CODE_TEST_READY));
397+
assert.isFalse(yield* fs.exists(env.T3CODE_TEST_TERM));
398+
}
377399
if ((yield* HostProcess.Platform) === "linux") {
378400
assert.include(failed.stderr, "glibc");
379401
assert.include(failed.stderr, "musl");

‎scripts/npm-launcher/t3.mjs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ async function install(dir, key) {
9393
const validate = spawnSync(
9494
path.join(staging, process.platform === "win32" ? "t3.exe" : "t3"),
9595
["--version"],
96-
{ stdio: ["ignore", "ignore", "pipe"], encoding: "utf8", timeout: 30_000 },
96+
{
97+
stdio: ["ignore", "ignore", "pipe"],
98+
encoding: "utf8",
99+
timeout: 30_000,
100+
killSignal: "SIGKILL",
101+
},
97102
);
98103
if (validate.status !== 0) {
99104
throw executableError(

0 commit comments

Comments
 (0)