Skip to content

Commit c136ec6

Browse files
authored
test: deflake the Gateway waiter lifetime and Bun plugin retention fixtures (#160200)
- server.request-lifetime: the default non-ephemeral test client starts a session-history prewarm (`ws:preauth`) alongside `ws:agent.wait`, so the "exactly one active root work" assertion raced it (seen as `2 !== 1` in CI). The waiter cases now connect as an ephemeral CLI client; failure messages name the active holders. - runtime.retention: under Bun, forced collections inside Promise continuations can leave JavaScriptCore's microtask stack holding completed async values, so WeakRef checks intermittently saw retained instances. Collect inside native setImmediate callbacks instead; assertions unchanged.
1 parent 7fac9b1 commit c136ec6

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎src/gateway/server.request-lifetime.test.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import path from "node:path";
66
import { setImmediate as nextTurn } from "node:timers/promises";
77
import { describe, expect, it, vi } from "vitest";
88
import { WebSocket } from "ws";
9+
import {
10+
GATEWAY_CLIENT_IDS,
11+
GATEWAY_CLIENT_MODES,
12+
} from "../../packages/gateway-protocol/src/client-info.js";
913
import type { ResponseFrame } from "../../packages/gateway-protocol/src/schema/frames.js";
1014
import { isAgentRunRestartAbortReason } from "../agents/run-termination.js";
1115
import { resolveStateDir } from "../config/paths.js";
@@ -14,6 +18,7 @@ import { initializeGlobalHookRunner } from "../plugins/hook-runner-global.js";
1418
import { createEmptyPluginRegistry } from "../plugins/registry-empty.js";
1519
import {
1620
getActiveGatewayRootWorkCount,
21+
getActiveGatewayRootWorkHolders,
1722
markGatewayRestartDraining,
1823
} from "../process/gateway-work-admission.js";
1924
import { getAsyncWorkSignal } from "../shared/async-work-scope.js";
@@ -93,7 +98,16 @@ describe("public Gateway close request lifetime", () => {
9398
await gateway.server.startupSettled;
9499
phase = "WebSocket connection";
95100
ws = await gateway.openWs();
96-
await connectOk(ws, { scopes: ["operator.admin"] });
101+
// An ephemeral CLI avoids admitting history prewarming alongside the waiter.
102+
await connectOk(ws, {
103+
scopes: ["operator.admin"],
104+
client: {
105+
id: GATEWAY_CLIENT_IDS.CLI,
106+
mode: GATEWAY_CLIENT_MODES.CLI,
107+
version: "1.0.0",
108+
platform: "test",
109+
},
110+
});
97111
const response = onceMessage<Pick<ResponseFrame, "type" | "id" | "ok" | "error">>(
98112
ws,
99113
(frame) => frame.type === "res" && frame.id === "wait-for-shutdown",
@@ -114,7 +128,9 @@ describe("public Gateway close request lifetime", () => {
114128
requestSent = true;
115129
phase = "waiter admission";
116130
await Promise.race([entered.promise, prematureReply]);
117-
expect(getActiveGatewayRootWorkCount()).toBe(1);
131+
expect(getActiveGatewayRootWorkCount(), getActiveGatewayRootWorkHolders().join(", ")).toBe(
132+
1,
133+
);
118134
// A failing owner is released through the real terminal registry, not an abandoned wait.
119135
releaseTimer = setTimeout(() => {
120136
emergencyRelease = true;

‎src/plugins/runtime.retention.test-support.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from "node:fs";
33
import { createRequire } from "node:module";
44
import os from "node:os";
55
import path from "node:path";
6+
import { setImmediate as scheduleImmediate } from "node:timers";
67
import { setImmediate } from "node:timers/promises";
78
import { withEnvAsync } from "../test-utils/env.js";
89
import {
@@ -24,8 +25,13 @@ async function collect() {
2425
assert.ok(gc, "The retention child requires --expose-gc");
2526
const control = new WeakRef({ unowned: true });
2627
for (let pass = 0; pass < 8; pass += 1) {
27-
await setImmediate();
28-
gc();
28+
// JavaScriptCore's promise-microtask stack can retain completed async values.
29+
await new Promise<void>((resolve) => {
30+
scheduleImmediate(() => {
31+
gc();
32+
resolve();
33+
});
34+
});
2935
}
3036
assert.equal(control.deref(), undefined, "Unowned control must collect");
3137
}

0 commit comments

Comments
 (0)