Repository navigation
Add failure-only production 503 diagnostics #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
10a962e
Record bounded unexpected responses in production CI
cursoragent 7acccc7
Bound production body reads to one cancellable deadline
cursoragent 4aa68e5
Keep process-group teardown after the leader exits
cursoragent 3d65c2d
Escalate production teardown KILL on group liveness
cursoragent 20c95b4
Merge branch 'main' into cursor/issue22-production-diagnostics-41a2
cursoragent 85dfe36
Merge branch 'main' into cursor/issue22-production-diagnostics-41a2
sdcarlson dda4090
Install SIGTERM handler before fixture readiness
cursoragent 0416146
Merge branch 'main' into cursor/issue22-production-diagnostics-41a2
sdcarlson 7b1f4cc
Satisfy oxlint on production diagnostic tests
cursoragent 01adb7c
Merge branch 'main' into cursor/issue22-production-diagnostics-41a2
sdcarlson 0d7c68d
Merge branch 'main' into cursor/issue22-production-diagnostics-41a2
sdcarlson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| import { spawn } from 'node:child_process'; | ||
|
|
||
| const BODY_BYTE_LIMIT = 2048; | ||
| const BODY_READ_TIMEOUT_MS = 2_000; | ||
| const RESPONSE_METADATA_NAMES = ['content-type', 'retry-after']; | ||
|
|
||
| const JWT_SHAPE = /eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g; | ||
|
|
||
| export function processSnapshot(child) { | ||
| return { | ||
| pid: child?.pid ?? null, | ||
| exitCode: child?.exitCode ?? null, | ||
| signal: child?.signalCode ?? null, | ||
| }; | ||
| } | ||
|
|
||
| function selectedResponseMetadata(headers) { | ||
| const selected = {}; | ||
| if (!headers) return selected; | ||
| for (const name of RESPONSE_METADATA_NAMES) { | ||
| const value = | ||
| typeof headers.get === 'function' ? headers.get(name) : headers[name]; | ||
| if (value) selected[name] = String(value); | ||
| } | ||
| return selected; | ||
| } | ||
|
|
||
| function redactAuthenticationJwts(text) { | ||
| return String(text ?? '').replace(JWT_SHAPE, '[redacted-jwt]'); | ||
| } | ||
|
|
||
| export async function readBoundedBody( | ||
| response, | ||
| { byteLimit = BODY_BYTE_LIMIT, timeoutMs = BODY_READ_TIMEOUT_MS } = {}, | ||
| ) { | ||
| const empty = { text: '', bytes: 0, truncated: false, timedOut: false }; | ||
| if (!response?.body || typeof response.body.getReader !== 'function') { | ||
| return empty; | ||
| } | ||
| const reader = response.body.getReader(); | ||
| const chunks = []; | ||
| let bytes = 0; | ||
| let truncated = false; | ||
| let timedOut = false; | ||
| let timeoutId; | ||
| const expired = new Promise((resolve) => { | ||
| timeoutId = setTimeout(() => { | ||
| timedOut = true; | ||
| resolve(); | ||
| }, timeoutMs); | ||
| }); | ||
| try { | ||
| while (bytes < byteLimit && !timedOut) { | ||
| const outcome = await Promise.race([ | ||
| reader.read().then((chunk) => ({ kind: 'chunk', chunk })), | ||
| expired.then(() => ({ kind: 'timeout' })), | ||
| ]); | ||
| if (outcome.kind === 'timeout') { | ||
| timedOut = true; | ||
| break; | ||
| } | ||
| const { done, value } = outcome.chunk; | ||
| if (done) break; | ||
| if (!value) continue; | ||
| const chunk = value instanceof Uint8Array ? value : new Uint8Array(value); | ||
| const room = byteLimit - bytes; | ||
| if (chunk.byteLength > room) { | ||
| chunks.push(chunk.subarray(0, room)); | ||
| bytes += room; | ||
| truncated = true; | ||
| break; | ||
| } | ||
| chunks.push(chunk); | ||
| bytes += chunk.byteLength; | ||
| } | ||
| } finally { | ||
| const cancel = Promise.resolve(reader.cancel()).catch(() => {}); | ||
| await Promise.race([cancel, expired]); | ||
| clearTimeout(timeoutId); | ||
| } | ||
| return { | ||
| text: Buffer.concat(chunks.map((part) => Buffer.from(part))).toString( | ||
| 'utf8', | ||
| ), | ||
| bytes, | ||
| truncated, | ||
| timedOut, | ||
| }; | ||
| } | ||
|
|
||
| export function describeUnexpectedResponse({ | ||
| operation, | ||
| status, | ||
| body, | ||
| truncated = false, | ||
| timedOut = false, | ||
| bodyBytes, | ||
| headers, | ||
| processState, | ||
| }) { | ||
| const meta = selectedResponseMetadata(headers); | ||
| const state = processState ?? {}; | ||
| const safeBody = redactAuthenticationJwts(body); | ||
| const flags = `${truncated ? ' truncated' : ''}${timedOut ? ' timed-out' : ''}`; | ||
| const lines = [ | ||
| '=== unexpected response ===', | ||
| `operation: ${operation}`, | ||
| `status: ${status}`, | ||
| ]; | ||
| for (const name of RESPONSE_METADATA_NAMES) { | ||
| if (meta[name]) lines.push(`${name}: ${meta[name]}`); | ||
| } | ||
| lines.push( | ||
| `body-bytes: ${bodyBytes ?? Buffer.byteLength(String(body ?? ''))}${flags}`, | ||
| ); | ||
| lines.push('body:'); | ||
| lines.push(safeBody); | ||
| lines.push( | ||
| `process: pid=${state.pid ?? null} exit=${state.exitCode ?? null} signal=${state.signal ?? null}`, | ||
| ); | ||
| lines.push('=== end unexpected response ==='); | ||
| return `${lines.join('\n')}\n`; | ||
| } | ||
|
|
||
| function stdioOpen(server) { | ||
| return [server?.stdout, server?.stderr].some( | ||
| (stream) => stream && !stream.destroyed && !stream.readableEnded, | ||
| ); | ||
| } | ||
|
|
||
| function waitForClose(server, timeoutMs) { | ||
| if (!server) return Promise.resolve(); | ||
| if ( | ||
| !stdioOpen(server) && | ||
| (server.exitCode !== null || server.signalCode !== null) | ||
| ) { | ||
| return Promise.resolve(); | ||
| } | ||
| return new Promise((accept) => { | ||
| let settled = false; | ||
| const done = () => { | ||
| if (settled) return; | ||
| settled = true; | ||
| clearTimeout(timer); | ||
| accept(); | ||
| }; | ||
| const timer = setTimeout(done, timeoutMs); | ||
| server.once('close', done); | ||
| if ( | ||
| !stdioOpen(server) && | ||
| (server.exitCode !== null || server.signalCode !== null) | ||
| ) { | ||
| done(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function groupAlive(server) { | ||
| if (!server?.pid) return false; | ||
| if (process.platform === 'win32') { | ||
| return server.exitCode === null && server.signalCode === null; | ||
| } | ||
| try { | ||
| process.kill(-server.pid, 0); | ||
| return true; | ||
| } catch (error) { | ||
| if (error.code === 'ESRCH') return false; | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| async function signalGroup(server, posixSignal) { | ||
| if (!server?.pid) return; | ||
| if (process.platform === 'win32') { | ||
| await new Promise((accept) => { | ||
| const stop = spawn('taskkill', ['/pid', String(server.pid), '/T', '/F'], { | ||
| windowsHide: true, | ||
| stdio: 'ignore', | ||
| }); | ||
| stop.on('exit', accept); | ||
| stop.on('error', accept); | ||
| }); | ||
| return; | ||
| } | ||
| try { | ||
| process.kill(-server.pid, posixSignal); | ||
| } catch (error) { | ||
| if (error.code !== 'ESRCH') { | ||
| console.error('Unable to stop test worker:', error); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function finishProductionServer( | ||
| server, | ||
| log, | ||
| { timeoutMs = 5_000 } = {}, | ||
| ) { | ||
| const closed = waitForClose(server, timeoutMs); | ||
| await signalGroup(server, 'SIGTERM'); | ||
| await closed; | ||
| if (groupAlive(server)) { | ||
| const last = waitForClose(server, timeoutMs); | ||
| await signalGroup(server, 'SIGKILL'); | ||
| await last; | ||
| } | ||
| if (log && !log.writableEnded) { | ||
| await new Promise((accept, reject) => { | ||
| log.once('finish', accept); | ||
| log.once('error', reject); | ||
| log.end(); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.