-
Notifications
You must be signed in to change notification settings - Fork 34
fix: harden control-plane health diagnostics #71
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { execSync } from 'node:child_process'; | ||
|
|
||
| export const CONTROL_PLANE_HEALTH_PATH = '/api/team'; | ||
|
|
||
| export async function checkControlPlaneHealth( | ||
| port: number, | ||
| timeoutMs = 3000, | ||
| ): Promise<{ ok: boolean; data?: any; error?: string }> { | ||
| try { | ||
| const controller = new AbortController(); | ||
| const timer = setTimeout(() => controller.abort(), timeoutMs); | ||
| const res = await fetch(`http://127.0.0.1:${port}${CONTROL_PLANE_HEALTH_PATH}`, { | ||
| signal: controller.signal, | ||
| }); | ||
| clearTimeout(timer); | ||
| if (!res.ok) return { ok: false, error: `HTTP ${res.status}` }; | ||
| const data = await res.json(); | ||
| return { ok: true, data }; | ||
| } catch (err) { | ||
| return { ok: false, error: err instanceof Error ? err.message : 'Unknown error' }; | ||
| } | ||
| } | ||
|
|
||
| export function isLikelyMemorixServeHttpCommand(command: string): boolean { | ||
| return command.includes('memorix') && command.includes('serve-http'); | ||
| } | ||
|
|
||
| export function isMemorixBackgroundProcess(pid: number, readProcessCommand = readProcessCommandForPid): boolean { | ||
| try { | ||
| const command = readProcessCommand(pid); | ||
| return isLikelyMemorixServeHttpCommand(command); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function readProcessCommandForPid(pid: number): string { | ||
| if (process.platform === 'linux') { | ||
| return execSync(`tr '\0' ' ' < /proc/${pid}/cmdline`, { encoding: 'utf-8' }).trim(); | ||
| } | ||
| return execSync(`ps -p ${pid} -o command=`, { encoding: 'utf-8' }).trim(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The non-Linux branch runs Useful? React with 👍 / 👎. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { | ||
| CONTROL_PLANE_HEALTH_PATH, | ||
| isLikelyMemorixServeHttpCommand, | ||
| isMemorixBackgroundProcess, | ||
| } from '../../src/cli/commands/control-plane-shared.js'; | ||
|
|
||
| describe('control-plane-shared', () => { | ||
| it('uses the team API as the HTTP control-plane health endpoint', () => { | ||
| expect(CONTROL_PLANE_HEALTH_PATH).toBe('/api/team'); | ||
| }); | ||
|
|
||
| it('recognizes memorix serve-http commands', () => { | ||
| expect(isLikelyMemorixServeHttpCommand('node /tmp/memorix serve-http --port 3211')).toBe(true); | ||
| expect(isLikelyMemorixServeHttpCommand('/opt/homebrew/bin/node /Users/ravi/.bun/bin/memorix serve-http --port 3211')).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects unrelated commands for PID reuse checks', () => { | ||
| expect(isLikelyMemorixServeHttpCommand('node some-other-server.js')).toBe(false); | ||
| expect(isLikelyMemorixServeHttpCommand('memorix serve')).toBe(false); | ||
| }); | ||
|
|
||
| it('only treats a pid as memorix when the inspected command matches serve-http', () => { | ||
| expect(isMemorixBackgroundProcess(123, () => 'node /Users/test/.bun/bin/memorix serve-http --port 3211')).toBe(true); | ||
| expect(isMemorixBackgroundProcess(123, () => 'node unrelated.js')).toBe(false); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The timeout is only cleared after a successful
fetch, so when the request fails fast (for example,ECONNREFUSED) the function returns fromcatchbut leaves a pending timer alive untiltimeoutMselapses. Sincedoctornow uses this shared helper for its probes, failed checks can keep the CLI process running for an extra 2–3 seconds per probe even though the network failure happened immediately.Useful? React with 👍 / 👎.