Skip to content

Commit ed106bd

Browse files
authored
Fix channel startup admission ownership (#123885)
1 parent bc66c17 commit ed106bd

2 files changed

Lines changed: 139 additions & 1 deletion

File tree

‎src/gateway/server-channels.test.ts‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ import { createEmptyPluginRegistry, type PluginRegistry } from "../plugins/regis
2626
import { getActivePluginRegistry, setActivePluginRegistry } from "../plugins/runtime.js";
2727
import { createRuntimeChannel } from "../plugins/runtime/runtime-channel.js";
2828
import type { PluginRuntime } from "../plugins/runtime/types.js";
29+
import {
30+
isGatewaySubordinateWorkAdmissionClosed,
31+
resetGatewayWorkAdmission,
32+
tryBeginGatewayRootWorkAdmission,
33+
} from "../process/gateway-work-admission.js";
2934
import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js";
3035
import type { RuntimeEnv } from "../runtime.js";
3136
import {
@@ -303,6 +308,7 @@ describe("server-channels auto restart", () => {
303308
let previousRegistry: PluginRegistry | null = null;
304309

305310
beforeEach(() => {
311+
resetGatewayWorkAdmission();
306312
previousRegistry = getActivePluginRegistry();
307313
vi.useRealTimers();
308314
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });
@@ -323,10 +329,136 @@ describe("server-channels auto restart", () => {
323329
await flushMicrotasks();
324330
vi.clearAllTimers();
325331
vi.useRealTimers();
332+
resetGatewayWorkAdmission();
326333
setActiveDegradedSecretOwners([]);
327334
setActivePluginRegistry(previousRegistry ?? createEmptyPluginRegistry());
328335
});
329336

337+
it("keeps a channel task admitted after the starting request finishes", async () => {
338+
const continueChannelTask = createDeferred();
339+
const observedAdmission = createDeferred<boolean>();
340+
const startAccount = vi.fn(async ({ abortSignal }: ChannelGatewayContext<TestAccount>) => {
341+
await continueChannelTask.promise;
342+
observedAdmission.resolve(isGatewaySubordinateWorkAdmissionClosed());
343+
await new Promise<void>((resolve) => {
344+
abortSignal.addEventListener("abort", () => resolve(), { once: true });
345+
});
346+
});
347+
installTestRegistry(createTestPlugin({ startAccount }));
348+
const manager = createManager();
349+
const requestAdmission = tryBeginGatewayRootWorkAdmission();
350+
expect(requestAdmission).not.toBeNull();
351+
if (!requestAdmission) {
352+
return;
353+
}
354+
355+
try {
356+
await requestAdmission.run(async () => {
357+
await manager.startChannel("discord", DEFAULT_ACCOUNT_ID, { manual: true });
358+
await waitForImmediate();
359+
await waitForMicrotaskCondition(
360+
() => startAccount.mock.calls.length === 1,
361+
"expected channel task to start",
362+
);
363+
});
364+
requestAdmission.release();
365+
continueChannelTask.resolve();
366+
367+
await expect(observedAdmission.promise).resolves.toBe(false);
368+
} finally {
369+
requestAdmission.release();
370+
continueChannelTask.resolve();
371+
}
372+
});
373+
374+
it("keeps approval-bootstrap descendants admitted after the starting request finishes", async () => {
375+
const continueApprovalDescendant = createDeferred();
376+
const observedAdmission = createDeferred<boolean>();
377+
hoisted.startChannelApprovalHandlerBootstrap.mockImplementation(async () => {
378+
void Promise.resolve().then(async () => {
379+
await continueApprovalDescendant.promise;
380+
observedAdmission.resolve(isGatewaySubordinateWorkAdmissionClosed());
381+
});
382+
return async () => {};
383+
});
384+
const startAccount = vi.fn(
385+
async ({ abortSignal }: ChannelGatewayContext<TestAccount>) =>
386+
await new Promise<void>((resolve) => {
387+
abortSignal.addEventListener("abort", () => resolve(), { once: true });
388+
}),
389+
);
390+
installTestRegistry(createTestPlugin({ startAccount }));
391+
const manager = createManager();
392+
const requestAdmission = tryBeginGatewayRootWorkAdmission();
393+
expect(requestAdmission).not.toBeNull();
394+
if (!requestAdmission) {
395+
return;
396+
}
397+
398+
try {
399+
await requestAdmission.run(async () => {
400+
await manager.startChannel("discord", DEFAULT_ACCOUNT_ID, { manual: true });
401+
await waitForImmediate();
402+
await waitForMicrotaskCondition(
403+
() => startAccount.mock.calls.length === 1,
404+
"expected channel task to start",
405+
);
406+
});
407+
requestAdmission.release();
408+
continueApprovalDescendant.resolve();
409+
410+
await expect(observedAdmission.promise).resolves.toBe(false);
411+
} finally {
412+
requestAdmission.release();
413+
continueApprovalDescendant.resolve();
414+
}
415+
});
416+
417+
it("keeps automatic restarts admitted after the starting request finishes", async () => {
418+
const finishFirstChannelTask = createDeferred();
419+
const observedAdmission: boolean[] = [];
420+
const startAccount = vi.fn(async ({ abortSignal }: ChannelGatewayContext<TestAccount>) => {
421+
observedAdmission.push(isGatewaySubordinateWorkAdmissionClosed());
422+
if (observedAdmission.length === 1) {
423+
await finishFirstChannelTask.promise;
424+
return;
425+
}
426+
await new Promise<void>((resolve) => {
427+
abortSignal.addEventListener("abort", () => resolve(), { once: true });
428+
});
429+
});
430+
installTestRegistry(createTestPlugin({ startAccount }));
431+
const manager = createManager();
432+
const requestAdmission = tryBeginGatewayRootWorkAdmission();
433+
expect(requestAdmission).not.toBeNull();
434+
if (!requestAdmission) {
435+
return;
436+
}
437+
438+
try {
439+
await requestAdmission.run(async () => {
440+
await manager.startChannel("discord", DEFAULT_ACCOUNT_ID, { manual: true });
441+
await waitForImmediate();
442+
await waitForMicrotaskCondition(
443+
() => startAccount.mock.calls.length === 1,
444+
"expected initial channel task to start",
445+
);
446+
});
447+
requestAdmission.release();
448+
finishFirstChannelTask.resolve();
449+
await advanceTimersUntil(
450+
() => startAccount.mock.calls.length === 2,
451+
"expected channel task to restart",
452+
{ stepMs: 10, maxMs: 100 },
453+
);
454+
455+
expect(observedAdmission).toEqual([false, false]);
456+
} finally {
457+
requestAdmission.release();
458+
finishFirstChannelTask.resolve();
459+
}
460+
});
461+
330462
it("caps crash-loop restarts after max attempts", async () => {
331463
const startAccount = vi.fn(async () => {});
332464
installTestRegistry(

‎src/gateway/server-channels.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { withPluginHttpRouteRegistry } from "../plugins/http-registry.js";
3737
import { withPluginCommandAccountStartScope } from "../plugins/plugin-command-account-start-scope.js";
3838
import type { PluginRegistry } from "../plugins/registry.js";
3939
import type { PluginRuntimeChannel } from "../plugins/runtime/types-channel.js";
40+
import { runOutsideGatewayRootWorkAdmission } from "../process/gateway-work-admission.js";
4041
import { resolveAccountEntry, resolveNormalizedAccountEntry } from "../routing/account-lookup.js";
4142
import {
4243
DEFAULT_ACCOUNT_ID,
@@ -496,7 +497,7 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
496497
}
497498
};
498499

499-
const startChannelInternal = async (
500+
const startChannelProcessOwned = async (
500501
channelId: ChannelId,
501502
accountId?: string,
502503
optsValue: StartChannelOptions = {},
@@ -1068,6 +1069,11 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
10681069
}
10691070
};
10701071

1072+
// Channel lifetimes outlive the RPC or timer requesting startup, so their
1073+
// provider, approval, cleanup, and restart descendants must be process-owned.
1074+
const startChannelInternal = (...args: Parameters<typeof startChannelProcessOwned>) =>
1075+
runOutsideGatewayRootWorkAdmission(() => startChannelProcessOwned(...args));
1076+
10711077
const startChannel = async (
10721078
channelId: ChannelId,
10731079
accountId?: string,

0 commit comments

Comments
 (0)