Skip to content

Commit 53006a2

Browse files
authored
fix(release): restart gateway after migration convergence (#120091)
1 parent 9571ae8 commit 53006a2

7 files changed

Lines changed: 485 additions & 37 deletions

File tree

‎scripts/lib/cross-os-release-checks/config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export type LaneState = {
4242
export type GatewayHandle = {
4343
child: ChildProcess;
4444
closeLog: () => Promise<void>;
45+
launchLogOffset: number;
4546
logPath: string;
47+
waitForClose: () => Promise<void>;
4648
};
4749
export type CommandResult = { exitCode: number; stdout: string; stderr: string };
4850
export type AgentTurnResult = CommandResult | { status: number; stdout: string; stderr: string };

‎scripts/lib/cross-os-release-checks/installed.ts‎

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ import {
3939
import { readLogFileSize, readLogTextSince } from "./logs.ts";
4040
import {
4141
canConnectToLoopbackPort,
42+
hasChildExited,
4243
resolveCommandSpawnInvocation,
4344
runCommand,
4445
runCommandInvocation,
46+
waitForGatewayWithStartupMigrationRestart,
4547
withAllocatedGatewayPort,
4648
} from "./process.ts";
4749
import { formatError, shellEscapeForSh, sleep } from "./shared.ts";
@@ -506,6 +508,7 @@ export async function startManualGatewayFromInstalledCli(params: {
506508
logPath: string;
507509
}): Promise<GatewayHandle> {
508510
mkdirSync(dirname(params.logPath), { recursive: true });
511+
const launchLogOffset = readLogFileSize(params.logPath);
509512
const gatewayLog = createWriteStream(params.logPath, { flags: "a" });
510513
const invocation = resolveInstalledCliInvocation(
511514
params.cliPath,
@@ -529,24 +532,33 @@ export async function startManualGatewayFromInstalledCli(params: {
529532
child.stderr?.on("data", (chunk) => {
530533
gatewayLog.write(chunk);
531534
});
532-
let logClosed = false;
533-
const closeLog = async () => {
534-
if (logClosed) {
535-
return;
536-
}
537-
logClosed = true;
538-
await new Promise<void>((resolvePromise) => {
535+
let resolveChildClose: () => void;
536+
const childClosePromise = new Promise<void>((resolvePromise) => {
537+
resolveChildClose = resolvePromise;
538+
});
539+
let closeLogPromise: Promise<void> | undefined;
540+
const closeLog = () => {
541+
closeLogPromise ??= new Promise<void>((resolvePromise) => {
539542
gatewayLog.once("error", () => resolvePromise());
540543
gatewayLog.end(() => resolvePromise());
541544
});
545+
return closeLogPromise;
542546
};
543547
child.once("close", () => {
548+
resolveChildClose();
544549
void closeLog();
545550
});
546551
child.once("error", () => {
552+
resolveChildClose();
547553
void closeLog();
548554
});
549-
return { child, closeLog, logPath: params.logPath };
555+
return {
556+
child,
557+
closeLog,
558+
launchLogOffset,
559+
logPath: params.logPath,
560+
waitForClose: () => childClosePromise,
561+
};
550562
}
551563

552564
async function resolveInstalledGatewayStatusArgs(params: {
@@ -609,8 +621,37 @@ export async function waitForInstalledGateway(params: {
609621
lane: LaneState;
610622
cliPath: string;
611623
env: NodeJS.ProcessEnv;
624+
gateway?: GatewayHandle;
625+
gatewayHolder?: { current: GatewayHandle | null };
626+
gatewayLogPath?: string;
612627
logPath: string;
613628
}) {
629+
if (params.gatewayHolder) {
630+
if (!params.gatewayLogPath) {
631+
throw new Error("Gateway restart coordination requires a gateway log path.");
632+
}
633+
const gatewayLogPath = params.gatewayLogPath;
634+
await waitForGatewayWithStartupMigrationRestart({
635+
gatewayHolder: params.gatewayHolder,
636+
restartGateway: () =>
637+
startManualGatewayFromInstalledCli({
638+
lane: params.lane,
639+
cliPath: params.cliPath,
640+
env: params.env,
641+
logPath: gatewayLogPath,
642+
}),
643+
waitUntilReady: (gateway) =>
644+
waitForInstalledGateway({
645+
lane: params.lane,
646+
cliPath: params.cliPath,
647+
env: params.env,
648+
gateway,
649+
logPath: params.logPath,
650+
}),
651+
});
652+
return;
653+
}
654+
614655
const statusArgs = await resolveInstalledGatewayStatusArgs({
615656
cliPath: params.cliPath,
616657
cwd: params.lane.homeDir,
@@ -619,6 +660,9 @@ export async function waitForInstalledGateway(params: {
619660
});
620661
const deadline = Date.now() + gatewayReadyDeadlineMs();
621662
while (Date.now() < deadline) {
663+
if (params.gateway && hasChildExited(params.gateway.child)) {
664+
throw new Error(`Gateway exited before becoming ready on port ${params.lane.gatewayPort}.`);
665+
}
622666
const result = await runInstalledCli({
623667
cliPath: params.cliPath,
624668
args: statusArgs,
@@ -631,6 +675,9 @@ export async function waitForInstalledGateway(params: {
631675
if (result.exitCode === 0) {
632676
return;
633677
}
678+
if (params.gateway && hasChildExited(params.gateway.child)) {
679+
throw new Error(`Gateway exited before becoming ready on port ${params.lane.gatewayPort}.`);
680+
}
634681
await sleep(2_000);
635682
}
636683
throw new Error(`Gateway did not become ready on port ${params.lane.gatewayPort}.`);

‎scripts/lib/cross-os-release-checks/lanes.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import { formatError, trimForSummary } from "./shared.ts";
8282
export async function runFreshLane(params: LaneBaseParams & { build: CandidateBuild }) {
8383
const lane = createLaneState("fresh");
8484
const cleanup: Cleanup[] = [];
85+
const gatewayHolder: { current: GatewayHandle | null } = { current: null };
8586
try {
8687
const env = buildLaneEnv(lane, params.providerConfig, params.providerSecretValue);
8788
await runTimedLanePhase(lane, "install-candidate", async () => {
@@ -143,12 +144,15 @@ export async function runFreshLane(params: LaneBaseParams & { build: CandidateBu
143144
logPath: join(params.logsDir, "fresh-gateway.log"),
144145
}),
145146
);
146-
cleanup.push(() => stopGateway(gateway));
147+
gatewayHolder.current = gateway;
148+
cleanup.push(() => stopGateway(gatewayHolder.current));
147149

148150
await runTimedLanePhase(lane, "wait-gateway", async () => {
149151
await waitForGateway({
150152
lane,
151153
env,
154+
gatewayHolder,
155+
gatewayLogPath: join(params.logsDir, "fresh-gateway.log"),
152156
logPath: join(params.logsDir, "fresh-gateway-status.log"),
153157
});
154158
});
@@ -200,6 +204,7 @@ export async function runUpgradeLane(
200204
}
201205
const lane = createLaneState("upgrade");
202206
const cleanup: Cleanup[] = [];
207+
const gatewayHolder: { current: GatewayHandle | null } = { current: null };
203208
try {
204209
const env = buildLaneEnv(lane, params.providerConfig, params.providerSecretValue);
205210
await runTimedLanePhase(lane, "install-baseline", async () => {
@@ -348,12 +353,15 @@ export async function runUpgradeLane(
348353
logPath: join(params.logsDir, "upgrade-gateway.log"),
349354
}),
350355
);
351-
cleanup.push(() => stopGateway(gateway));
356+
gatewayHolder.current = gateway;
357+
cleanup.push(() => stopGateway(gatewayHolder.current));
352358

353359
await runTimedLanePhase(lane, "wait-gateway", async () => {
354360
await waitForGateway({
355361
lane,
356362
env,
363+
gatewayHolder,
364+
gatewayLogPath: join(params.logsDir, "upgrade-gateway.log"),
357365
logPath: join(params.logsDir, "upgrade-gateway-status.log"),
358366
});
359367
});
@@ -560,6 +568,8 @@ export async function runInstallerFreshSuite(
560568
lane,
561569
cliPath: freshShell.cliPath,
562570
env,
571+
gatewayHolder: manualGateway,
572+
gatewayLogPath: join(params.logsDir, "installer-fresh-gateway.log"),
563573
logPath: join(params.logsDir, "installer-fresh-gateway-status.log"),
564574
});
565575
}
@@ -790,6 +800,8 @@ export async function runDevUpdateSuite(
790800
lane,
791801
cliPath: verifiedShell.cliPath,
792802
env,
803+
gatewayHolder: manualGateway,
804+
gatewayLogPath: join(params.logsDir, "dev-update-gateway.log"),
793805
logPath: join(params.logsDir, "dev-update-gateway-status.log"),
794806
});
795807
} else {

‎scripts/lib/cross-os-release-checks/network-smokes.ts‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,50 @@ export function buildDiscordSmokeGuildsConfig(guildId: string, channelId: string
3737
};
3838
}
3939

40+
export async function restartManualGatewayForDiscordSmoke(params: {
41+
lane: LaneState;
42+
cliPath: string;
43+
env: NodeJS.ProcessEnv;
44+
gatewayHolder: { current: GatewayHandle | null };
45+
gatewayLogPath: string;
46+
statusLogPath: string;
47+
operations?: {
48+
startGateway: typeof startManualGatewayFromInstalledCli;
49+
stopGateway: typeof stopGateway;
50+
waitForGateway: typeof waitForInstalledGateway;
51+
};
52+
}) {
53+
const operations = params.operations ?? {
54+
startGateway: startManualGatewayFromInstalledCli,
55+
stopGateway,
56+
waitForGateway: waitForInstalledGateway,
57+
};
58+
if (params.gatewayHolder.current) {
59+
await operations.stopGateway(params.gatewayHolder.current);
60+
params.gatewayHolder.current = null;
61+
}
62+
params.gatewayHolder.current = await operations.startGateway({
63+
lane: params.lane,
64+
cliPath: params.cliPath,
65+
env: params.env,
66+
logPath: params.gatewayLogPath,
67+
});
68+
await operations.waitForGateway({
69+
lane: params.lane,
70+
cliPath: params.cliPath,
71+
env: params.env,
72+
gatewayHolder: params.gatewayHolder,
73+
gatewayLogPath: params.gatewayLogPath,
74+
logPath: params.statusLogPath,
75+
});
76+
}
77+
4078
async function configureDiscordSmoke(params: {
4179
lane: LaneState;
4280
cliPath: string;
4381
cwd: string;
4482
env: NodeJS.ProcessEnv;
45-
gatewayHolder?: { current: GatewayHandle | null };
83+
gatewayHolder: { current: GatewayHandle | null };
4684
logPath: string;
4785
token: string;
4886
guildId: string;
@@ -95,24 +133,17 @@ async function configureDiscordSmoke(params: {
95133
});
96134
if (!shouldUseManagedGatewayService()) {
97135
const gatewayEnv = { ...params.env, DISCORD_BOT_TOKEN: params.token };
98-
if (params.gatewayHolder?.current) {
99-
await stopGateway(params.gatewayHolder.current);
100-
params.gatewayHolder.current = null;
101-
}
102-
const gateway = await startManualGatewayFromInstalledCli({
103-
lane: params.lane,
104-
cliPath: params.cliPath,
105-
env: gatewayEnv,
106-
logPath: join(params.cwd, `.openclaw/logs/${params.lane.name}-discord-gateway.log`),
107-
});
108-
if (params.gatewayHolder) {
109-
params.gatewayHolder.current = gateway;
110-
}
111-
await waitForInstalledGateway({
136+
const gatewayLogPath = join(
137+
params.cwd,
138+
`.openclaw/logs/${params.lane.name}-discord-gateway.log`,
139+
);
140+
await restartManualGatewayForDiscordSmoke({
112141
lane: params.lane,
113142
cliPath: params.cliPath,
114143
env: gatewayEnv,
115-
logPath: params.logPath,
144+
gatewayHolder: params.gatewayHolder,
145+
gatewayLogPath,
146+
statusLogPath: params.logPath,
116147
});
117148
return;
118149
}

‎scripts/lib/cross-os-release-checks/process.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
CROSS_OS_COMMAND_HEARTBEAT_SECONDS,
3030
CROSS_OS_PROCESS_TREE_KILL_AFTER_MS,
3131
} from "./config.ts";
32+
import { readLogTextSince } from "./logs.ts";
3233
import { formatError, sleep, toLintErrorObject, trimForSummary } from "./shared.ts";
3334

3435
const CROSS_OS_SIGNAL_EXIT_CODES: Partial<Record<NodeJS.Signals, number>> = {
@@ -37,6 +38,8 @@ const CROSS_OS_SIGNAL_EXIT_CODES: Partial<Record<NodeJS.Signals, number>> = {
3738
SIGTERM: 143,
3839
};
3940
const CROSS_OS_ACTIVE_CHILD_TREE_KILLERS = new Set<(signal: NodeJS.Signals) => void>();
41+
const STARTUP_MIGRATION_RESTART_PREFIX =
42+
"OpenClaw plugin migration inputs changed during startup convergence;";
4043
let forwardedSignalExitCode: number | undefined;
4144
let forwardedSignalForceKillTimer: NodeJS.Timeout | undefined;
4245

@@ -117,10 +120,40 @@ export async function canConnectToLoopbackPort(port: number, timeoutMs = 1_000)
117120
});
118121
}
119122

120-
function hasChildExited(child: ChildProcess) {
123+
export function hasChildExited(child: ChildProcess) {
121124
return child.exitCode !== null || (child.signalCode ?? null) !== null;
122125
}
123126

127+
export async function waitForGatewayWithStartupMigrationRestart(params: {
128+
gatewayHolder: { current: GatewayHandle | null };
129+
restartGateway: () => Promise<GatewayHandle>;
130+
waitUntilReady: (gateway: GatewayHandle) => Promise<void>;
131+
}) {
132+
let gateway = params.gatewayHolder.current;
133+
if (!gateway) {
134+
throw new Error("Gateway restart coordination requires an active gateway handle.");
135+
}
136+
137+
for (let attempt = 0; attempt < 2; attempt += 1) {
138+
try {
139+
await params.waitUntilReady(gateway);
140+
return;
141+
} catch (error) {
142+
if (!hasChildExited(gateway.child)) {
143+
throw error;
144+
}
145+
await gateway.waitForClose();
146+
await gateway.closeLog();
147+
const startupLog = readLogTextSince(gateway.logPath, gateway.launchLogOffset);
148+
if (attempt > 0 || !startupLog.includes(STARTUP_MIGRATION_RESTART_PREFIX)) {
149+
throw error;
150+
}
151+
gateway = await params.restartGateway();
152+
params.gatewayHolder.current = gateway;
153+
}
154+
}
155+
}
156+
124157
export async function stopGateway(gateway: GatewayHandle | null) {
125158
try {
126159
if (!gateway?.child?.pid) {

0 commit comments

Comments
 (0)