From 5658234c44c5239ec1ce3a2bea03df5672de8b53 Mon Sep 17 00:00:00 2001 From: sx Date: Fri, 16 Oct 2026 01:21:41 +0800 Subject: [PATCH] fix(launcher): recognise the spawn poll's own child through the registry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The spawn poll reads the single proxy-origin pointer, which is last-writer-wins: when two lanes start together the later writer replaces it, the first launcher's launchToken check fails, and the live-record guard makes it skip probing its own preferred port. It then burns the whole 20s window and degrades to "proxy bootstrap failed — model traffic goes direct (uncompressed)", silently disabling compression for that lane until the next healthy bring-up. Match the child by pid against the per-instance registry, which still identifies it when the pointer names another lane's proxy. The registry lookup is a LauncherDeps seam so the regression test can drive it; that test reproduces the original 20s "did not become healthy" failure when the registry branch is removed. --- src/launcher.ts | 19 +++++++++++++++++++ tests/launcher.test.ts | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/launcher.ts b/src/launcher.ts index 35de4acd9f..6d0d2ed8de 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -227,6 +227,12 @@ export interface LauncherDeps { * BILI_NATIVE_ATTACH_EXTERNAL > config `native.attachExternal` > false. */ resolveAttachExternal?: () => boolean; readInstanceFile?: () => ProxyInstanceFile | { origin: string } | undefined; + /** Spawn-poll discovery of the instance this launch started. The + * single proxy-origin file is last-writer-wins, so a concurrent lane + * launch can leave it naming another lane's live proxy; the registry + * is per-instance and still identifies our own child by pid. + * Default: discoverLiveInstances. */ + discoverInstances?: () => ProxyInstanceFile[]; spawnImpl?: SpawnFn; now?: () => number; sleep?: (ms: number) => Promise; @@ -2808,6 +2814,7 @@ export async function ensureProxyRunning( const fetchImpl = deps.fetchImpl ?? defaultFetch; const fetchHealthInfo = deps.fetchHealthInfo ?? fetchHealthInfoDefault; const readInstance = deps.readInstanceFile ?? readProxyInstanceFile; + const discoverInstances = deps.discoverInstances ?? discoverLiveInstances; const spawnImpl = deps.spawnImpl ?? (spawn as SpawnFn); const now = deps.now ?? Date.now; const sleepImpl = deps.sleep ?? ((ms: number) => new Promise((r) => setTimeout(r, ms))); @@ -3033,6 +3040,18 @@ export async function ensureProxyRunning( } continue; } + // A concurrent launch of another lane overwrites the single + // proxy-origin pointer, so that file can name a live proxy that is + // not ours. The registry is per-instance, and our own child is the + // entry whose pid matches — the file view cannot vouch for either + // child when two lanes start together. + const own = child.pid === undefined + ? undefined + : discoverInstances().find((live) => live.pid === child.pid); + if (own !== undefined && (await probeHealth(own.origin, fetchImpl))) { + settleZonePort(own.port); + return { origin: own.origin, port: own.port, child, logPath }; + } // Fallback for a child that cannot write the instance file (broken // state dir) or an old pre-handshake binary: only trust the preferred // origin when NO record vouches for it — a LIVE record's owner owns diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 2a5b923378..fe17a2f6fc 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -978,6 +978,49 @@ test("ensureProxyRunning: spawns when not healthy, polls until healthy", async ( assert.ok(probes >= 2); }); +test("ensureProxyRunning: finds its own child when a concurrent lane overwrote proxy-origin", async () => { + const spawnImpl: SpawnFn = () => makeFakeChild(42499); + const fetchImpl = async (url: string) => ({ ok: url.includes("18799") }); + const handle = await ensureProxyRunning( + { host: "127.0.0.1", port: 8787, passthrough: false, debug: false, lane: "dsh-dev" }, + { + fetchImpl, + spawnImpl, + sleep: () => Promise.resolve(), + // The single pointer belongs to a DIFFERENT lane's live launch. + readInstanceFile: () => ({ + origin: "http://127.0.0.1:18790", + instanceId: "other-lane", + pid: 1, + startedAt: 1, + host: "127.0.0.1", + port: 18790, + passthrough: false, + mitmDomains: [], + modelWindows: {}, + launchToken: "other-launch-token", + lane: "dsh-prod", + }), + // The registry still identifies OUR child by pid. + discoverInstances: () => [{ + origin: "http://127.0.0.1:18799", + instanceId: "our-lane", + pid: 42499, + startedAt: 2, + host: "127.0.0.1", + port: 18799, + passthrough: false, + mitmDomains: [], + modelWindows: {}, + lane: "dsh-dev", + }], + }, + ); + assert.equal(handle.origin, "http://127.0.0.1:18799"); + assert.equal(handle.port, 18799); + assert.equal(handle.attached, undefined); +}); + test("ensureProxyRunning: throws when never healthy within deadline", async () => { const fetchImpl = async () => ({ ok: false }); const spawnImpl: SpawnFn = () => makeFakeChild(42422);