Skip to content

Commit b0d6fff

Browse files
authored
perf(gateway): start macOS system-CA warmup before bootstrap (#128422)
Measured on this machine, the first boot after the OS trust cache went cold spent 4972ms in post-attach.system-ca; a fresh standalone Node process took 3529ms, while subsequent boots took 16-160ms. Start the single-flight warmup after kernel creation so the existing post-attach barrier observes only the residual wait.
1 parent fef6c6d commit b0d6fff

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎src/gateway/server-start.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { GatewayServer, GatewayServerOptions } from "./server-public.js";
1010
import { createGatewayHttpTransport } from "./server-runtime-state.js";
1111
import { runGatewayShutdownSteps } from "./server-shutdown.js";
1212
import { finishGatewayStartup } from "./server-startup-finish.js";
13+
import { beginMacOSSystemCaWarmupOnce } from "./system-ca-warmup.js";
1314

1415
const loadGatewayStartupPostAttachModule = createLazyRuntimeModule(
1516
() => import("./server-startup-post-attach.js"),
@@ -40,6 +41,11 @@ export async function startGatewayServerCore(
4041
releasePostReadyWork = resolve;
4142
});
4243
const gatewayKernel = await createGatewayKernel(port, opts);
44+
if (!gatewayKernel.minimalTestGateway) {
45+
// Start the Keychain read early so it overlaps bootstrap; post-attach awaits the
46+
// shared promise before plugins can use TLS.
47+
void beginMacOSSystemCaWarmupOnce({ log });
48+
}
4349
let startupSettled: Promise<void>;
4450
const {
4551
beginClosePrelude,

‎src/gateway/server-startup-post-attach.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ import {
5252
type GatewayStartupOutcomeRecorder,
5353
} from "./server-startup-outcomes.js";
5454
import { measureStartup, type GatewayStartupTrace } from "./server-startup-trace.js";
55-
import { warmMacOSSystemCaOffMainThread } from "./system-ca-warmup.js";
55+
import {
56+
beginMacOSSystemCaWarmupOnce,
57+
type warmMacOSSystemCaOffMainThread,
58+
} from "./system-ca-warmup.js";
5659
const ACP_BACKEND_READY_TIMEOUT_MS = 5_000;
5760
const ACP_BACKEND_READY_POLL_MS = 50;
5861
const PROVIDER_AUTH_PREWARM_START_DELAY_MS = 5_000;
@@ -1003,7 +1006,7 @@ const defaultGatewayPostAttachRuntimeDeps: GatewayPostAttachRuntimeDeps = {
10031006
scheduleGatewayUpdateCheck: async (...args) =>
10041007
(await import("../infra/update-startup.js")).scheduleGatewayUpdateCheck(...args),
10051008
startGatewaySidecars,
1006-
warmSystemCa: warmMacOSSystemCaOffMainThread,
1009+
warmSystemCa: beginMacOSSystemCaWarmupOnce,
10071010
loadSubagentRegistryActivation: async () =>
10081011
(await import("../agents/subagents/registry/subagent-registry.js")).activateSubagentRegistry,
10091012
};

‎src/gateway/system-ca-warmup.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,35 @@ describe("warmMacOSSystemCaOffMainThread", () => {
127127
);
128128
});
129129
});
130+
131+
describe("beginMacOSSystemCaWarmupOnce", () => {
132+
it("shares one worker across concurrent and settled calls", async () => {
133+
vi.resetModules();
134+
const { beginMacOSSystemCaWarmupOnce } = await import("./system-ca-warmup.js");
135+
const worker = new FakeWorker();
136+
const createWorker = vi.fn(() => worker);
137+
const options = { platform: "darwin" as const, env: {}, createWorker };
138+
139+
const first = beginMacOSSystemCaWarmupOnce(options);
140+
const concurrent = beginMacOSSystemCaWarmupOnce(options);
141+
142+
expect(concurrent).toBe(first);
143+
expect(createWorker).toHaveBeenCalledOnce();
144+
145+
worker.emit("message", { ok: true, certificateCount: 42 });
146+
await first;
147+
148+
expect(beginMacOSSystemCaWarmupOnce(options)).toBe(first);
149+
expect(createWorker).toHaveBeenCalledOnce();
150+
});
151+
152+
it("settles without creating a worker outside macOS", async () => {
153+
vi.resetModules();
154+
const { beginMacOSSystemCaWarmupOnce } = await import("./system-ca-warmup.js");
155+
const createWorker = vi.fn(() => new FakeWorker());
156+
157+
await beginMacOSSystemCaWarmupOnce({ platform: "linux", env: {}, createWorker });
158+
159+
expect(createWorker).not.toHaveBeenCalled();
160+
});
161+
});

‎src/gateway/system-ca-warmup.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type SystemCaWarmupOptions = {
3636

3737
type SystemCaWarmupMessage = { ok: true; certificateCount: number } | { ok: false; error: string };
3838

39+
let macOSSystemCaWarmupPromise: Promise<void> | undefined;
40+
3941
function isSystemCaWarmupMessage(value: unknown): value is SystemCaWarmupMessage {
4042
if (!value || typeof value !== "object" || Array.isArray(value)) {
4143
return false;
@@ -144,3 +146,11 @@ export async function warmMacOSSystemCaOffMainThread(
144146
worker.unref();
145147
});
146148
}
149+
150+
/**
151+
* One warmup worker runs per process, and every caller awaits its shared completion.
152+
* The settled promise is retained after success or failure because warmup is only an optimization.
153+
*/
154+
export function beginMacOSSystemCaWarmupOnce(options: SystemCaWarmupOptions = {}): Promise<void> {
155+
return (macOSSystemCaWarmupPromise ??= warmMacOSSystemCaOffMainThread(options));
156+
}

0 commit comments

Comments
 (0)