Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
Expand Down Expand Up @@ -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<void>((r) => setTimeout(r, ms)));
Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions tests/launcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down