Skip to content

Commit d365dab

Browse files
committed
fix(gateway): reject stale node work before enqueue
1 parent 7a54874 commit d365dab

2 files changed

Lines changed: 46 additions & 18 deletions

File tree

‎src/gateway/server-methods/nodes.pending-work.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,46 @@ describe("node.pending handlers", () => {
356356
expect(call?.[2]).toBeUndefined();
357357
});
358358

359+
it("does not enqueue work when pairing invalidates during the generation check", async () => {
360+
const lifecycleController = new AbortController();
361+
mocks.captureNodeWakeLifecycle.mockReturnValue(lifecycleController.signal);
362+
mocks.isNodePairingGenerationCurrent.mockImplementation(async () => {
363+
lifecycleController.abort();
364+
return true;
365+
});
366+
mocks.enqueueNodePendingWork.mockReturnValue({
367+
revision: 5,
368+
deduped: false,
369+
item: {
370+
id: "pending-stale-generation",
371+
type: "location.request",
372+
priority: "default",
373+
createdAtMs: 100,
374+
expiresAtMs: null,
375+
},
376+
});
377+
const respond = vi.fn();
378+
379+
await expectDefined(
380+
nodePendingWorkHandlers["node.pending.enqueue"],
381+
'nodePendingWorkHandlers["node.pending.enqueue"] test invariant',
382+
)({
383+
params: { nodeId: "node-stale-generation", type: "location.request", wake: false },
384+
respond: respond as never,
385+
client: null,
386+
context: makeContext() as never,
387+
req: { type: "req", id: "req-node-stale-generation", method: "node.pending.enqueue" },
388+
isWebchatConnect: () => false,
389+
});
390+
391+
expect(mocks.enqueueNodePendingWork).not.toHaveBeenCalled();
392+
expect(respondCall(respond)).toMatchObject([
393+
false,
394+
undefined,
395+
{ details: { code: "PAIRING_CHANGED" } },
396+
]);
397+
});
398+
359399
it("returns unavailable when pairing removal invalidates an enqueued item", async () => {
360400
const lifecycleController = new AbortController();
361401
const wakeLifecycle = lifecycleController.signal;

‎src/gateway/server-methods/nodes.pending-work.ts‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import {
1010
captureNodePairingGeneration,
1111
isNodePairingGenerationCurrent,
12-
type NodePairingGeneration,
1312
} from "../../infra/device-pairing-node-state.js";
1413
import {
1514
drainNodePendingWork,
@@ -20,12 +19,12 @@ import {
2019
} from "../node-pending-work.js";
2120
import {
2221
captureNodeWakeLifecycle,
23-
isNodeWakeLifecycleCurrent,
2422
NODE_WAKE_RECONNECT_RETRY_WAIT_MS,
2523
NODE_WAKE_RECONNECT_WAIT_MS,
2624
releaseNodeWakeLifecycle,
2725
} from "../node-wake-state.js";
2826
import { respondUnavailableOnThrow } from "./nodes.helpers.js";
27+
import { isNodePairingWorkCurrent } from "./nodes.shared.js";
2928
import {
3029
maybeSendNodeWakeNudge,
3130
maybeWakeNodeWithApns,
@@ -46,17 +45,6 @@ function respondPairingChanged(respond: RespondFn) {
4645
);
4746
}
4847

49-
async function isPendingGenerationCurrent(params: {
50-
nodeId: string;
51-
generation: NodePairingGeneration;
52-
lifecycle: AbortSignal;
53-
}): Promise<boolean> {
54-
return (
55-
isNodeWakeLifecycleCurrent(params.nodeId, params.lifecycle, params.generation.key) &&
56-
(await isNodePairingGenerationCurrent(params.generation))
57-
);
58-
}
59-
6048
function resolveClientNodeId(
6149
client: { connect?: { device?: { id?: string }; client?: { id?: string } } } | null,
6250
): string | null {
@@ -127,7 +115,7 @@ export const nodePendingWorkHandlers: GatewayRequestHandlers = {
127115
}
128116
const wakeLifecycle = captureNodeWakeLifecycle(nodeId, generation.key);
129117
try {
130-
if (!(await isPendingGenerationCurrent({ nodeId, generation, lifecycle: wakeLifecycle }))) {
118+
if (!(await isNodePairingWorkCurrent({ nodeId, generation, lifecycle: wakeLifecycle }))) {
131119
respondPairingChanged(respond);
132120
return;
133121
}
@@ -178,7 +166,7 @@ export const nodePendingWorkHandlers: GatewayRequestHandlers = {
178166
);
179167
}
180168
if (
181-
(await isPendingGenerationCurrent({
169+
(await isNodePairingWorkCurrent({
182170
nodeId,
183171
generation,
184172
lifecycle: wakeLifecycle,
@@ -216,7 +204,7 @@ export const nodePendingWorkHandlers: GatewayRequestHandlers = {
216204
}
217205
}
218206
if (
219-
(await isPendingGenerationCurrent({
207+
(await isNodePairingWorkCurrent({
220208
nodeId,
221209
generation,
222210
lifecycle: wakeLifecycle,
@@ -237,7 +225,7 @@ export const nodePendingWorkHandlers: GatewayRequestHandlers = {
237225
`node pending wake done node=${nodeId} req=${wakeReqId} connected=false reason=not_connected`,
238226
);
239227
} else if (
240-
await isPendingGenerationCurrent({
228+
await isNodePairingWorkCurrent({
241229
nodeId,
242230
generation,
243231
lifecycle: wakeLifecycle,
@@ -248,7 +236,7 @@ export const nodePendingWorkHandlers: GatewayRequestHandlers = {
248236
);
249237
}
250238
}
251-
if (!(await isPendingGenerationCurrent({ nodeId, generation, lifecycle: wakeLifecycle }))) {
239+
if (!(await isNodePairingWorkCurrent({ nodeId, generation, lifecycle: wakeLifecycle }))) {
252240
if (!queued.deduped) {
253241
removeNodePendingWorkItem({
254242
nodeId,

0 commit comments

Comments
 (0)