Skip to content
Merged
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
1 change: 1 addition & 0 deletions scripts/live-files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const LIVE_FILES = [
"tests/index-entry-guard.test.ts",
"tests/issue1958-routed-warn.test.ts",
"tests/issue2752-forward-partial-outcome.test.ts",
"tests/issue4341-early-twin-adoption.test.ts",
"tests/issue760-percall-conversation.test.ts",
"tests/issue927-opencode-install.test.ts",
"tests/kimi-native.test.ts",
Expand Down
21 changes: 21 additions & 0 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,27 @@ export function classifySameLanePeer(
return undefined;
}

/** #4341: a registry twin is safe to adopt before persistence initialization
* only after the listener proves the same pid + instance identity. A stale
* marker, pid reuse, or foreign listener must fall through to the existing
* bind/EADDRINUSE policy instead of causing a false adopt. */
export async function findVerifiedSameLaneTwin(
entries: RegistryEntry[],
port: number,
lane: string | undefined,
ownFingerprint: string | undefined,
sameShape: (e: RegistryEntry) => boolean,
healthOf: (origin: string) => Promise<{ pid?: number; instanceId?: string } | undefined>,
): Promise<RegistryEntry | undefined> {
for (const entry of entries) {
const peer = classifySameLanePeer([entry], port, lane, ownFingerprint, sameShape);
if (peer?.kind !== "twin") continue;
const health = await healthOf(peer.entry.origin);
if (health?.pid === peer.entry.pid && health.instanceId === peer.entry.instanceId) return peer.entry;
}
return undefined;
}

/** #1225: content identity of a bili entry script (sha256 of its bytes).
* The spawned child records this for ITS script; an attaching launcher
* compares it against the hash of the script it would spawn — so "same
Expand Down
59 changes: 41 additions & 18 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ import { getStore, setGlobalSessionCommitListener } from "./persist.js";
import { log as loggerLog, configureLogger, getLogPath, closeLogger, isStreamWriteError, isBenignSocketRaceError, enterSessionContext, muteStderrLeg, onFileLegLive } from "./logger.js";
import { queryLogLines } from "./web/logs-query.js";
import { configFile, defaultLogFile, stateDir } from "./paths.js";
import { atomicWriteInstanceFile, clearProxyInstanceFile, classifySameLanePeer, entryScriptFingerprint, entryShape, findSameLanePredecessor, isPidAlive, listInstances, readProxyInstanceFile, registerInstanceAndWarn, sameInstanceShape, unregisterInstance, warnOnNewPeers, writeSpawnPortLog, type InstanceShape, type ProxyInstanceFile, type SameLanePeer } from "./instance.js";
import { atomicWriteInstanceFile, clearProxyInstanceFile, classifySameLanePeer, entryScriptFingerprint, entryShape, findSameLanePredecessor, findVerifiedSameLaneTwin, isPidAlive, listInstances, readProxyInstanceFile, registerInstanceAndWarn, sameInstanceShape, unregisterInstance, warnOnNewPeers, writeSpawnPortLog, type InstanceShape, type ProxyInstanceFile, type SameLanePeer } from "./instance.js";
import { createServedMarkerWriter, reapStaleWriterMarkers, writerMarkerDir, type ServedMarkerWriter } from "./served-markers.js";
import { mayClobberPointer, probeOwnerInfo, resolvePortClaim } from "./port-owner.js";
import { hoistTrappedToolItems } from "./tool-pair-order.js";
Expand Down Expand Up @@ -622,6 +622,46 @@ export async function createEngineContext(opts: ProxyOptions): Promise<EngineCon
}

export async function startServer(opts: ProxyOptions): Promise<http.Server> {
const launchToken = process.env.BILI_LAUNCH_TOKEN?.trim();
// #1225: lane identity + code fingerprint recorded into the instance file
// so later launches can decide reuse by WHO started us and WHICH code we
// run — not just config shape (same-version stale dist kept serving after
// a rebuild; different clients cross-wrote one shared proxy).
const launcherLane = process.env.BILI_LAUNCHER_LANE?.trim() || undefined;
const ownFingerprint = entryScriptFingerprint(process.argv[1]);
// #3801: our config shape EXACTLY as the registration below will record
// it — the byte-stable pair for judging whether a contended-port holder
// is a true duplicate or a config-drifted predecessor.
const ownShape: InstanceShape = {
host: opts.host,
passthrough: opts.passthrough,
mitmDomains: opts.mitm.enabled ? [...opts.mitm.domains] : [],
modelWindows: { ...LAUNCHER_MODEL_WINDOWS },
modelMaxOutputs: { ...LAUNCHER_MODEL_MAX_OUTPUTS },
};
const earlyTwin = await findVerifiedSameLaneTwin(
listInstances(),
opts.port,
launcherLane,
ownFingerprint,
(entry) => sameInstanceShape(entryShape(entry), ownShape),
(origin) => probeOwnerInfo(origin),
);
if (earlyTwin !== undefined) {
const hint = ` — port ${opts.port} is already served by a same-build bili instance (pid ${earlyTwin.pid}, lane ${launcherLane}) — exiting before persistence initialization instead of hopping: the duplicate loses and the serving instance keeps serving; the next launch will attach to it (#4341/#3044). A spawner reads this exit as an adopt signal and attaches to the serving instance itself (#3654).`;
const listenLine = `listen failed: EADDRINUSE listen EADDRINUSE: address already in use ${opts.host}:${opts.port}${hint}`;
// createEngineContext has not run yet, so its configureLogger call
// has not happened either — open the file leg here (mirroring it) so
// the adopt line lands in bili.log like the late-path twin line, gate
// on opts.log like every other startServer line, and drain the stream
// before exiting or the buffered line can be lost with the process.
configureLogger(opts.logFile ?? defaultLogFile());
logMsg(opts, "error", listenLine);
if (launchToken !== undefined) writeSpawnPortLog(opts.port, `${new Date().toISOString()} [error] [v=${VERSION}] ${listenLine}`);
await closeLogger();
process.exit(eaddrinuseExitCode("twin-collision"));
}

const engine = await createEngineContext(opts);
const { core, config, log, instanceId, instanceStartedAt, proxyWatchers, watcherLastSeen, initialWatcherPid, adminCtx } = engine;

Expand Down Expand Up @@ -1024,29 +1064,12 @@ export async function startServer(opts: ProxyOptions): Promise<http.Server> {
// #964: BILI_STRICT_PORT (claude SessionStart hook) opts OUT of the retry
// — the native posture dials a static baked-in URL, so a port-hop
// "success" would strand every model request on the dead original port.
const launchToken = process.env.BILI_LAUNCH_TOKEN?.trim();
const strictPort = process.env.BILI_STRICT_PORT === "1";
// #3044: a self-restart replacement must own exactly the port its
// predecessor freed — it fails fast instead of inheriting the +1 ladder
// (hopping strands it serving no client while the old instance keeps the
// real port: the orphan leak #3044 reports).
const upgradeReplacement = process.env[UPGRADE_REPLACEMENT_ENV] === "1";
// #1225: lane identity + code fingerprint recorded into the instance file
// so later launches can decide reuse by WHO started us and WHICH code we
// run — not just config shape (same-version stale dist kept serving after
// a rebuild; different clients cross-wrote one shared proxy).
const launcherLane = process.env.BILI_LAUNCHER_LANE?.trim() || undefined;
const ownFingerprint = entryScriptFingerprint(process.argv[1]);
// #3801: our config shape EXACTLY as the registration below will record
// it — the byte-stable pair for judging whether a contended-port holder
// is a true duplicate or a config-drifted predecessor.
const ownShape: InstanceShape = {
host: opts.host,
passthrough: opts.passthrough,
mitmDomains: opts.mitm.enabled ? [...opts.mitm.domains] : [],
modelWindows: { ...LAUNCHER_MODEL_WINDOWS },
modelMaxOutputs: { ...LAUNCHER_MODEL_MAX_OUTPUTS },
};
const MAX_LISTEN_ATTEMPTS = 17;
let listenAttempts = 0;
let lastTriedPort = opts.port;
Expand Down
Loading
Loading