ReviewOS

also looking at this

stacks/rpx

[DO NOT MERGE] diag: #2267 CLI-hang discriminator

#2269
Closed chris wants to merge diag/2267-cli-hang into main
5 files +370 -6
packages/rpx/test/helpers/canary.preload.tsadded+69-0
Changes to packages/rpx/test/helpers/canary.preload.ts
@@ -0,0 +1,69 @@
1/**
2 * TEMPORARY — #2267 canary. Not for merge.
3 *
4 * Loaded through `[test] preload` in the root bunfig.toml on the diag branch,
5 * so this `beforeEach` runs inside every test file. `Bun.main` names the file
6 * currently under test, so the canary fires once per file, before that file's
7 * first test: it spawns `bun --version` through `node:child_process.spawn` —
8 * the path that returns a dud ChildProcess (pid 12345, no process) at
9 * gateway.test.ts's position — with a short budget, and logs whether a real
10 * child ran. The first file whose canary is a DUD names the file BEFORE it as
11 * the one that poisons the spawn path; the first OK after a DUD names the file
12 * that clears it. The parent's pipe/thread state is dumped at both edges.
13 *
14 * Every line is prefixed `[2267-canary]`.
15 */
16import { beforeEach } from 'bun:test'
17import { spawn } from 'node:child_process'
18import * as process from 'node:process'
19import { parentDump } from './spawn-diag'
20
21const P = '[2267-canary]'
22const BUDGET_MS = 3_000
23
24let lastFile = ''
25let fileIndex = 0
26let wasDud = false
27
28function canary(): Promise<string> {
29 return new Promise((resolve) => {
30 const t0 = performance.now()
31 let out = ''
32 let done = false
33 const child = spawn(process.execPath, ['--version'], { stdio: ['ignore', 'pipe', 'pipe'] })
34 child.stdout?.on('data', (c: Buffer) => { out += c.toString() })
35 const finish = (verdict: string): void => {
36 if (done)
37 return
38 done = true
39 resolve(`${verdict} pid=${child.pid} exitCode=${child.exitCode} signalCode=${child.signalCode} ${Math.round(performance.now() - t0)}ms`)
40 }
41 child.on('error', e => finish(`ERROR(${e.message})`))
42 child.on('exit', code => finish(out.trim() ? `OK(${out.trim()})` : `EXIT-NO-OUTPUT(code=${code})`))
43 setTimeout(() => {
44 if (done)
45 return
46 try {
47 child.kill('SIGKILL')
48 }
49 catch {}
50 finish('DUD')
51 }, BUDGET_MS)
52 })
53}
54
55beforeEach(async () => {
56 const file = Bun.main.split('/').pop() ?? Bun.main
57 if (file === lastFile)
58 return
59 lastFile = file
60 fileIndex++
61 const result = await canary()
62 const isDud = result.startsWith('DUD')
63 console.log(P, `file#${fileIndex}`, file, '->', result)
64 if (isDud !== wasDud) {
65 console.log(P, isDud ? 'FIRST DUD — state right after the poisoning file:' : 'RECOVERED — state right after the clearing file:')
66 console.log(P, ` ${parentDump()}`)
67 wasDud = isDud
68 }
69})