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/gateway.test.tsmodified+34-6
Changes to packages/rpx/test/gateway.test.ts
@@ -22,6 +22,7 @@ import * as path from 'node:path'
2222import * as process from 'node:process'
2323import { mergeGatewayFragments, readGatewayFragments, resolveGatewayOptions, startGateway } from '../src/gateway'
2424import * as Start from '../src/start'
25import { envDump, jsFields, parentDump, procDump, runSweep, say } from './helpers/spawn-diag'
2526
2627const CERTS_DIR = '/etc/rpx/certs'
2728
@@ -353,9 +354,16 @@ async function freePort(): Promise<number> {
353354 return port
354355}
355356
356const CLI_STARTS = process.platform !== 'linux'
357
358describe.skipIf(!CLI_STARTS)('rpx gateway (CLI, end to end)', () => {
357/**
358 * #2267 diagnostic, in situ. The Linux skip is lifted on this branch so the
359 * baseline runs exactly where the hang was recorded — this file is position 23
360 * of 48 in bun's deterministic order, and the same shape passes from a
361 * standalone file at position 38. On a hang the child's and this parent's
362 * /proc state is dumped before the kill, then the variant sweep runs from the
363 * same spot. The long-lived case stays skipped on Linux: it is not the
364 * discriminator and would only add 45s.
365 */
366describe('rpx gateway (CLI, end to end)', () => {
359367 it('starts and prints its help', async () => {
360368 const cli = path.join(import.meta.dir, '..', 'bin', 'cli.ts')
361369 const child = spawn(process.execPath, [cli, '--help'], { stdio: ['ignore', 'pipe', 'pipe'] })
@@ -368,13 +376,33 @@ describe.skipIf(!CLI_STARTS)('rpx gateway (CLI, end to end)', () => {
368376 new Promise<string>(resolve => setTimeout(() => resolve('still running after 45s'), 45_000)),
369377 ])
370378
371 if (child.exitCode === null && child.signalCode === null)
372 child.kill('SIGKILL')
379 // Trigger on the race, not on exitCode/signalCode: last run the dud
380 // ChildProcess reported a non-null one at 45s with no exit event and no
381 // bytes, which took the wrong branch and skipped the dump.
382 if (exit.startsWith('still running')) {
383 say('IN-SITU BASELINE HUNG at gateway.test.ts')
384 say(` ${jsFields(child)}`)
385 say(` child:\n ${child.pid ? procDump(child.pid) : '(no pid)'}`)
386 say(` parent:\n ${parentDump()}`)
387 try {
388 child.kill('SIGKILL')
389 }
390 catch {}
391 }
392 else {
393 say('IN-SITU BASELINE OK:', exit, `${output.length} bytes`)
394 }
373395
374396 expect(output, `rpx --help produced nothing (${exit})`).toContain('gateway')
375397 }, 60_000)
376398
377 it('serves a fragment\'s route over plain HTTP with --no-https on the given port', async () => {
399 it('[2267-diag] in-situ spawn variant sweep', async () => {
400 envDump()
401 const results = await runSweep(path.join(import.meta.dir, '..', 'bin', 'cli.ts'), 'in-situ')
402 expect(results).toHaveLength(8)
403 }, 200_000)
404
405 it.skipIf(process.platform === 'linux')('serves a fragment\'s route over plain HTTP with --no-https on the given port', async () => {
378406 const upstream = Bun.serve({ port: 0, hostname: '127.0.0.1', fetch: req => new Response(`gateway-upstream ${req.headers.get('x-forwarded-host')}`) })
379407 const httpPort = await freePort()
380408 // Two routes so the fragment looks like a real box (and the shared