File size: 10,259 Bytes
f778c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { spawn, type ChildProcess } from "node:child_process";
import { once } from "node:events";
import fs from "node:fs";
import path from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { afterEach, describe, expect, it, vi } from "vitest";
import { withTestTimeout } from "../../../test/helpers/promise.js";
import { createTempDirTracker } from "../../../test/helpers/temp-dir.js";
import { resolveRuntimeWorkerUrl } from "../../infra/runtime-worker-url.js";
import { gatewayDirectStopEntrypoints } from "../cli-entrypoint.test-support.js";

const tempDirs = createTempDirTracker();
const children = new Map<ChildProcess, Promise<unknown[]>>();
const runLoopUrl = resolveRuntimeWorkerUrl(gatewayDirectStopEntrypoints.runLoop).href;
const restartUrl = resolveRuntimeWorkerUrl(gatewayDirectStopEntrypoints.restartPolicy).href;
const fileLogTransportUrl = resolveRuntimeWorkerUrl(
  gatewayDirectStopEntrypoints.fileLogTransport,
).href;

const childScript = `
  import fs from "node:fs";
  import http from "node:http";
  import { runGatewayLoop } from ${JSON.stringify(runLoopUrl)};
  import { setGatewaySigusr1RestartPolicy } from ${JSON.stringify(restartUrl)};
  import { fileLogTransport } from ${JSON.stringify(fileLogTransportUrl)};
  const faultPath = process.argv[1];
  const closeFailure = process.argv[2];
  setGatewaySigusr1RestartPolicy({ allowExternal: true });
  let starts = 0;
  try {
    await runGatewayLoop({
      ownsProcessLifecycle: true,
      start: async () => {
        const attempt = ++starts;
        process.stdout.write("start:" + attempt + "\\n");
        if (fs.existsSync(faultPath)) throw new Error("fixture startup refused");
        const server = http.createServer((_request, response) => response.end("ready"));
        await new Promise((resolve, reject) => {
          server.once("error", reject);
          server.listen(0, "127.0.0.1", resolve);
        });
        process.stdout.write("ready:" + attempt + "\\n");
        return {
          getTailscaleIngressEndpoint: () => undefined,
          startupSettled: Promise.resolve(),
          close: () => {
            if (closeFailure === "pending") {
              fileLogTransport.setAppenderForTests(() => {
                process.stdout.write("append:pending\\n");
                return new Promise(() => {});
              });
              process.stdout.write("close:pending\\n");
              return new Promise(() => {});
            }
            if (closeFailure) {
              const error = new TypeError("fixture close owner failed");
              error.stack = "TypeError: fixture close owner failed\\n    at closeOwner (fixture.js:12:3)";
              if (closeFailure === "sync") throw error;
              return Promise.reject(error);
            }
            return new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve()))
              .then(() => process.stdout.write("closed:" + attempt + "\\n"));
          },
        };
      },
      runtime: {
        log: () => {},
        error: (...args) => console.error(...args),
        exit: code => process.exit(code),
      },
    });
  } catch (error) {
    console.error(error);
    process.exitCode = 1;
  }
`;

afterEach(async () => {
  for (const child of children.keys()) {
    if (child.exitCode === null && child.signalCode === null) {
      child.kill("SIGKILL");
    }
  }
  const results = await withTestTimeout(
    Promise.allSettled(children.values()),
    5_000,
    "restart liveness children did not close; retaining their fixture directories",
  );
  const errors = results.flatMap((result) => (result.status === "rejected" ? [result.reason] : []));
  if (errors.length) {
    throw new AggregateError(errors, "restart liveness cleanup failed");
  }
  children.clear();
  tempDirs.cleanup();
});

function startFixture(initialFailure = false, closeFailure = "") {
  const directory = tempDirs.make("openclaw-restart-liveness-");
  const home = path.join(directory, "home");
  fs.mkdirSync(home);
  const faultPath = path.join(directory, "startup-fault");
  const logFile = path.join(directory, "gateway.jsonl");
  const stateDir = path.join(directory, "state");
  fs.writeFileSync(
    path.join(directory, "openclaw.json"),
    JSON.stringify({ logging: { level: "info", file: logFile } }),
  );
  if (initialFailure) {
    fs.writeFileSync(faultPath, "refuse");
  }
  const child = spawn(
    process.execPath,
    ["--import", "tsx", "--input-type=module", "--eval", childScript, faultPath, closeFailure],
    {
      env: {
        PATH: process.env.PATH,
        HOME: home,
        TMPDIR: directory,
        OPENCLAW_STATE_DIR: stateDir,
        OPENCLAW_CONFIG_PATH: path.join(directory, "openclaw.json"),
        OPENCLAW_NO_RESPAWN: "1",
        ...(closeFailure === "pending"
          ? { OPENCLAW_LAUNCHD_LABEL: "ai.openclaw.gateway.test" }
          : {}),
        NODE_DISABLE_COMPILE_CACHE: "1",
        TSX_DISABLE_CACHE: "1",
        ESBUILD_WORKER_THREADS: "0",
      },
      stdio: ["ignore", "pipe", "pipe"],
    },
  );
  const closed = once(child, "close");
  children.set(child, closed);
  void closed.catch(() => {});
  let output = "";
  child.stdout?.on("data", (chunk: Buffer) => (output += chunk.toString()));
  child.stderr?.on("data", (chunk: Buffer) => (output += chunk.toString()));
  const waitForOutput = (text: string) =>
    vi.waitFor(() => expect(output).toContain(text), { timeout: 45_000, interval: 25 });
  return { child, closed, faultPath, logFile, stateDir, waitForOutput, output: () => output };
}

async function expectFailedRestartWaiting(
  fixture: ReturnType<typeof startFixture>,
  attempt: number,
) {
  expect(fixture.child.kill("SIGUSR1")).toBe(true);
  await fixture.waitForOutput(`start:${attempt}`);
  await vi.waitFor(
    () =>
      expect(
        fixture.output().split("Process will stay alive; fix the issue and restart.").length - 1,
      ).toBe(attempt - 1),
    { timeout: 5_000, interval: 25 },
  );
  // Only the parent observes an idle interval. A child timer or IPC channel
  // would hide the lost-process regression after its real listener closes.
  expect(
    await Promise.race([
      fixture.closed.then((exit) => ({ exit })),
      delay(750).then(() => "waiting"),
    ]),
    fixture.output(),
  ).toBe("waiting");
}

describe("runGatewayLoop failed-restart process lifetime", () => {
  const posixIt = process.platform === "win32" ? it.skip : it;

  it.skipIf(process.platform !== "darwin")(
    "exits before the hard watchdog when a shutdown-deadline log append stalls",
    async () => {
      const fixture = startFixture(false, "pending");
      await fixture.waitForOutput("ready:1");
      expect(fixture.child.kill("SIGTERM")).toBe(true);
      await fixture.waitForOutput("close:pending");
      expect(await fixture.closed, fixture.output()).toEqual([0, null]);
      expect(fixture.output()).toContain("append:pending");
      const bundleDir = path.join(fixture.stateDir, "logs", "stability");
      const files = fs.readdirSync(bundleDir);
      expect(files).toHaveLength(1);
      expect(JSON.parse(fs.readFileSync(path.join(bundleDir, files[0]!), "utf8"))).toMatchObject({
        reason: "gateway.stop_shutdown_timeout",
      });
    },
    60_000,
  );

  posixIt.each(["sync", "rejected"])(
    "persists %s close failures before a SIGUSR1 force-exit",
    async (mode) => {
      const fixture = startFixture(false, mode);
      await fixture.waitForOutput("ready:1");
      expect(fixture.child.kill("SIGUSR1")).toBe(true);
      expect(await fixture.closed, fixture.output()).toEqual([1, null]);
      const bundleDir = path.join(fixture.stateDir, "logs", "stability");
      const files = fs.readdirSync(bundleDir);
      expect(files).toHaveLength(1);
      const bundle = JSON.parse(fs.readFileSync(path.join(bundleDir, files[0]!), "utf8"));
      expect(bundle).toMatchObject({
        reason: "gateway.restart_close_failed",
        error: { name: "TypeError", message: "fixture close owner failed" },
        evidence: {
          shutdown: {
            step: "gateway-server-close",
            errors: [
              {
                name: "TypeError",
                message: "fixture close owner failed",
                stack: "TypeError: fixture close owner failed\n    at closeOwner (fixture.js:12:3)",
              },
            ],
          },
        },
      });
      expect(fs.readFileSync(fixture.logFile, "utf8")).toContain(
        "shutdown step failed (gateway server close): fixture close owner failed",
      );
      expect(fixture.output()).not.toContain("start:2");
    },
    60_000,
  );

  posixIt(
    "recovers in the same process after repeated operator-triggered startup failures",
    async () => {
      const fixture = startFixture();
      await fixture.waitForOutput("ready:1");
      fs.writeFileSync(fixture.faultPath, "refuse");
      await expectFailedRestartWaiting(fixture, 2);
      await expectFailedRestartWaiting(fixture, 3);
      fs.unlinkSync(fixture.faultPath);
      expect(fixture.child.kill("SIGUSR1")).toBe(true);
      await fixture.waitForOutput("ready:4");
      expect(fixture.child.kill("SIGTERM")).toBe(true);
      expect(await fixture.closed, fixture.output()).toEqual([0, null]);
    },
    60_000,
  );

  posixIt.each(["SIGTERM", "SIGINT"] as const)(
    "exits cleanly on %s while waiting after a failed restart",
    async (signal) => {
      const fixture = startFixture();
      await fixture.waitForOutput("ready:1");
      fs.writeFileSync(fixture.faultPath, "refuse");
      await expectFailedRestartWaiting(fixture, 2);
      expect(fixture.child.kill(signal)).toBe(true);
      expect(await fixture.closed, fixture.output()).toEqual([0, null]);
      expect(fixture.output()).not.toContain("start:3");
    },
    60_000,
  );

  posixIt(
    "releases process ownership when initial startup rejects",
    async () => {
      const fixture = startFixture(true);
      expect(await fixture.closed, fixture.output()).toEqual([1, null]);
      expect(fixture.output()).toContain("fixture startup refused");
      expect(fixture.output()).not.toContain("Process will stay alive");
    },
    60_000,
  );
});