Skip to content

Commit 7f76651

Browse files
roodboiclaude
andcommitted
test(native): publish the hanging status PID atomically and scale authored budgets
Under host load, "cancel during the runtime status check refuses before the startup intent" failed settlement after its assertions passed. The stand-in status request appended its PID with appendFile, and the case cancelled as soon as the request was logged. The product's SIGKILL could land after the file was created but before the PID was written. Settlement reads an empty PID file as a write still in progress, so it waited out its budget. - The stand-in now stages its PID and renames it into place, so the record is either absent or complete. - The case waits for the published PID (awaitPidFile) before it cancels, instead of the call log. - The file's settlement, startup and stop harness budgets now derive from testBudgetMs. The 50 ms deadline cases and their wall bound stay literal. Refs HACK-1367 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent becf358 commit 7f76651

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

‎tests/native-authored-project-start.test.ts‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,29 @@ import {
4141
evaluateComposeServiceEnvFreshness,
4242
} from "../src/lib/compose-env-fingerprint.ts";
4343
import { writeLifecycleMuxFixture } from "./helpers/lifecycle-mux-fixture.ts";
44+
import { awaitPidFile } from "./helpers/native-authored-settlement.ts";
4445
import {
4546
createNativeFixtureCases,
4647
type NativeFixtureCase,
4748
} from "./helpers/native-fixture-cases.ts";
49+
import { testBudgetMs } from "./helpers/test-budget.ts";
4850

4951
const run = "a".repeat(32);
5052
const CANARY = "synthetic-private-native-start-canary";
51-
/** Allowance for a case's owners, groups and stop endpoints to be absent afterwards. */
52-
const SETTLEMENT_BUDGET_MS = 15_000;
53+
/**
54+
* Allowance for a case's owners, groups and stop endpoints to be absent
55+
* afterwards. Harness budget: a passing case settles as soon as they exit.
56+
*/
57+
const SETTLEMENT_BUDGET_MS = testBudgetMs(5000);
5358
/**
5459
* Caller startup deadline for cases that are not about the deadline itself.
5560
* Each start spawns several Bun children (compiler, planner, owner, hooks); on
5661
* a loaded host that takes well over the 10-20 s these cases used to allow.
62+
* Cases that test the deadline pass their own literal product deadline.
5763
*/
58-
const STARTUP_TIMEOUT_MS = 60_000;
64+
const STARTUP_TIMEOUT_MS = testBudgetMs(15_000);
5965
/** Caller stop deadline for ordinary `down` in the hook and live-stop cases. */
60-
const STOP_TIMEOUT_MS = 60_000;
66+
const STOP_TIMEOUT_MS = testBudgetMs(15_000);
6167
/**
6268
* Body budget for every case: a full start plus a stop, with slack. The guard
6369
* adds the settlement budget on top, so cases never fall back to Bun's 5 s.
@@ -464,6 +470,9 @@ console.log(JSON.stringify(result));
464470
group: true,
465471
});
466472
// A hanging status request must be killed by cancellation, not outlive the case.
473+
// The stand-in publishes its PID by rename, so the file is either absent or
474+
// complete: a cancel that lands mid-write must not leave an empty record that
475+
// settlement reads as a write still in progress.
467476
const statusPid = join(root, "status.pid");
468477
owner.settlement.record({
469478
kind: "pid-file",
@@ -475,11 +484,11 @@ console.log(JSON.stringify(result));
475484
await writeFile(
476485
binary,
477486
`#!${process.execPath}
478-
import {appendFile,readFile,readdir,unlink,writeFile} from 'node:fs/promises';
487+
import {appendFile,readFile,readdir,rename,unlink,writeFile} from 'node:fs/promises';
479488
import {createHash} from 'node:crypto';import {join} from 'node:path';
480489
const args=process.argv.slice(2);const action=args[4];await appendFile(${JSON.stringify(calls)},JSON.stringify(args)+'\\n');
481490
if(${options.runtimeStopped === true}&&args[2]==='runtime'&&args[3]==='status'){console.log(JSON.stringify({phase:'stopped',process_alive:false}));process.exit(0)}
482-
if(${options.runtimeStatusHangs === true}&&args[2]==='runtime'&&args[3]==='status'){await appendFile(${JSON.stringify(statusPid)},process.pid+'\\n');await new Promise((resolve)=>setTimeout(resolve,${2 * CASE_TIMEOUT_MS}));process.exit(0)}
491+
if(${options.runtimeStatusHangs === true}&&args[2]==='runtime'&&args[3]==='status'){const staged=${JSON.stringify(statusPid)}+'.'+process.pid;await writeFile(staged,process.pid+'\\n');await rename(staged,${JSON.stringify(statusPid)});await new Promise((resolve)=>setTimeout(resolve,${2 * CASE_TIMEOUT_MS}));process.exit(0)}
483492
if(${options.runtimeStatusWrappedStop === true}&&args[2]==='runtime'&&args[3]==='status'){console.error(JSON.stringify({code:'native_graph_foreground',cause_code:'runtime_not_running',message:${JSON.stringify(CANARY)}}));process.exit(2)}
484493
const journal=${JSON.stringify(journal)};
485494
const root=${JSON.stringify(join(projectDir, ".internal", "native-authored-runs"))};
@@ -578,6 +587,7 @@ if(['plan','serve','frontend-plan','frontend-serve'].includes(action)){
578587
calls,
579588
delivery,
580589
sources,
590+
statusPid,
581591
};
582592
}
583593

@@ -887,22 +897,12 @@ macTest(
887897
signal: signal.signal,
888898
startupTimeoutMs: STARTUP_TIMEOUT_MS,
889899
});
890-
// Cancel once the status request is in flight. The check treats the
891-
// aborted request as inconclusive; the start must still not reserve.
892-
// The stand-in request hangs past the case budget, so this waits on the
893-
// request itself rather than racing a shorter fixed window.
894-
const deadline = performance.now() + CASE_TIMEOUT_MS;
895-
while (
896-
!(
897-
(await Bun.file(selected.calls).exists()) &&
898-
(await Bun.file(selected.calls).text()).includes('"status"')
899-
)
900-
) {
901-
if (performance.now() > deadline) {
902-
throw new Error("runtime status was never requested");
903-
}
904-
await Bun.sleep(20);
905-
}
900+
// Cancel once the status request is in flight and has published its PID.
901+
// The check treats the aborted request as inconclusive; the start must
902+
// still not reserve. The stand-in request hangs past the case budget, so
903+
// this waits on the request's own identity rather than a fixed window, and
904+
// the cancel cannot land while the stand-in is still recording it.
905+
await awaitPidFile(selected.statusPid, { budgetMs: CASE_TIMEOUT_MS });
906906
signal.abort();
907907
const error = await failure(started, "not-started");
908908
expect(error).toHaveProperty("canceled", true);

0 commit comments

Comments
 (0)