Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1012 B
Raw Blame
import type { ChildProcess } from "node:child_process";

export async function stopTestProcess(child: ChildProcess, graceMs = 5_000) {
  if (child.exitCode !== null || child.signalCode !== null || !child.pid) return;
  // `killed` only records successful signal delivery, not process termination.
  let onExit: () => void;
  const exited = new Promise<boolean>((resolve) => {
    onExit = () => resolve(true);
    child.once("exit", onExit);
  });
  async function waitForExit(ms: number) {
    let timer: NodeJS.Timeout;
    try {
      return await Promise.race([
        exited,
        new Promise<boolean>((resolve) => {
          timer = setTimeout(() => resolve(false), ms);
        }),
      ]);
    } finally {
      clearTimeout(timer!);
    }
  }
  try {
    child.kill("SIGTERM");
    if (await waitForExit(graceMs)) return;
    child.kill("SIGKILL");
    if (!(await waitForExit(5_000))) throw new Error(`Test process ${child.pid} did not exit`);
  } finally {
    child.off("exit", onExit!);
  }
}