Merge pull request #2786 from pikasTech/fix/caserun-native-port-restart
fix: CaseRun 重启回收 Vite 子进程端口
This commit is contained in:
@@ -106,8 +106,7 @@ async function releaseStaleServicePort(service: ServiceName, env: Record<string,
|
||||
const port = Number(requiredEnv(env, service === "worker" ? "CASERUN_WORKER_HEALTH_PORT" : `${prefix}_PORT`));
|
||||
if (await portAvailable(bindHost, port)) return;
|
||||
const expectedPortKey = service === "api" ? "HARNESSRL_API_PORT" : service === "worker" ? "HARNESSRL_WORKER_HEALTH_PORT" : "WORKBENCH_WEB_PORT";
|
||||
const commandMarker = service === "api" ? "hwlab-harnessrl-api/main.ts" : service === "worker" ? "hwlab-harnessrl-worker/main.ts" : "dev:native-caserun";
|
||||
const candidates = await matchingServiceGroups(commandMarker, expectedPortKey, String(port));
|
||||
const candidates = await matchingServiceGroups(expectedPortKey, String(port));
|
||||
if (candidates.length === 0) {
|
||||
throw codedError("native_port_owned_by_other_service", `CaseRun ${service} port ${port} is occupied by another service`);
|
||||
}
|
||||
@@ -125,17 +124,15 @@ async function releaseStaleServicePort(service: ServiceName, env: Record<string,
|
||||
if (!await waitForPort(bindHost, port, 1_000)) throw codedError("native_port_release_timeout", `CaseRun ${service} port ${port} did not become available`);
|
||||
}
|
||||
|
||||
async function matchingServiceGroups(commandMarker: string, portKey: string, port: string): Promise<number[]> {
|
||||
async function matchingServiceGroups(portKey: string, port: string): Promise<number[]> {
|
||||
const groups = new Set<number>();
|
||||
for (const entry of await readdir("/proc").catch(() => [])) {
|
||||
if (!/^\d+$/u.test(entry)) continue;
|
||||
const base = `/proc/${entry}`;
|
||||
const [cmdline, environ, stat] = await Promise.all([
|
||||
readFile(`${base}/cmdline`, "utf8").catch(() => ""),
|
||||
const [environ, stat] = await Promise.all([
|
||||
readFile(`${base}/environ`, "utf8").catch(() => ""),
|
||||
readFile(`${base}/stat`, "utf8").catch(() => ""),
|
||||
]);
|
||||
if (!cmdline.replace(/\0/gu, " ").includes(commandMarker)) continue;
|
||||
const variables = environ.split("\0");
|
||||
if (!variables.includes("HWLAB_CASERUN_NATIVE_TEST=1") || !variables.includes(`${portKey}=${port}`)) continue;
|
||||
const fields = stat.slice(stat.lastIndexOf(") ") + 2).trim().split(/\s+/u);
|
||||
|
||||
Reference in New Issue
Block a user