Skip to content

Commit 160e368

Browse files
authored
fix(gateway): run full node teardown when a node token is revoked (#124117)
device.token.revoke for role=node only invalidated wake state, while its sibling device.pair.remove — the other way node authority permanently ends — runs the full teardown owner: clearRemovedNodeRuntimeState (pending actions, pending work, wake state, surface caps, remote node info) plus reconcileRevokedDeviceWorker (worker environments and active placements). A revoked node therefore kept stranded pending work, stale surface caps, and live worker placements until restart. Revoke now runs the same teardown owner as pairing removal. Rotate and reapproval intentionally keep their narrower wake-state invalidation: the node stays paired there and reconnects with the new token/generation. Regression test mirrors the pair.remove teardown coverage and fails pre-fix.
1 parent 11bcc0e commit 160e368

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎src/gateway/server-methods/devices.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,55 @@ describe("deviceHandlers", () => {
214214
});
215215
});
216216

217+
it("clears node runtime state and reconciles workers after revoking a node token", async () => {
218+
const nodeId = "revoked-node-device";
219+
revokeDeviceTokenMock.mockResolvedValue({
220+
ok: true,
221+
entry: { token: "raw-node-token", role: "node", scopes: [], revokedAtMs: 456 },
222+
});
223+
await seedNodeWakeState(nodeId);
224+
enqueueNodePendingWork({ nodeId, type: "location.request" });
225+
const wakeLifecycle = captureNodeWakeLifecycle(nodeId);
226+
const workerEnvironmentService = {};
227+
const reconciledDevices: string[] = [];
228+
bindDeviceWorkerReconciliation(workerEnvironmentService, async (deviceId) => {
229+
reconciledDevices.push(deviceId);
230+
return [];
231+
});
232+
const opts = createOptions(
233+
"device.token.revoke",
234+
{ deviceId: nodeId, role: "node" },
235+
// Non-operator role management requires an admin-scoped caller.
236+
{ client: createClient(["operator.admin"], "admin-device", { isDeviceTokenAuth: true }) },
237+
);
238+
Object.assign(opts.context, { workerEnvironmentService });
239+
240+
await expectDefined(
241+
deviceHandlers["device.token.revoke"],
242+
'deviceHandlers["device.token.revoke"] test invariant',
243+
)(opts);
244+
245+
// Revocation ends node authority like pairing removal: the same teardown
246+
// owner must run so pending work, wake state, surface caps, and worker
247+
// placements are not stranded on a dead node.
248+
expect(getNodeWakeStateSnapshot(nodeId)).toBeUndefined();
249+
expect(wakeLifecycle.aborted).toBe(true);
250+
expect(drainNodePendingWork(nodeId).items.map((item) => item.id)).toEqual(["baseline-status"]);
251+
const nodeRegistry = opts.context.nodeRegistry as unknown as {
252+
updateSurface: ReturnType<typeof vi.fn>;
253+
};
254+
expect(nodeRegistry.updateSurface).toHaveBeenCalledWith(nodeId, {
255+
caps: [],
256+
commands: [],
257+
permissions: undefined,
258+
});
259+
expect(reconciledDevices).toEqual([nodeId]);
260+
expect(opts.context.invalidateClientsForDevice).toHaveBeenCalledWith(nodeId, {
261+
role: "node",
262+
reason: "device-token-revoked",
263+
});
264+
});
265+
217266
it("disconnects active clients after removing a paired device", async () => {
218267
removePairedDeviceMock.mockResolvedValue({ deviceId: "device-1", removedAtMs: 123 });
219268
const opts = createOptions("device.pair.remove", { deviceId: " device-1 " });

‎src/gateway/server-methods/devices.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,11 @@ export const deviceHandlers: GatewayRequestHandlers = {
818818
role: entry.role,
819819
});
820820
if (entry.role === "node") {
821-
invalidateNodeWakeState(normalizedDeviceId);
821+
// Revoking a node token ends its authority like pairing removal does:
822+
// run the same teardown owner so pending actions/work, wake state,
823+
// surface caps, and worker placements are not stranded on a dead node.
824+
clearRemovedNodeRuntimeState({ nodeId: normalizedDeviceId, context });
825+
await reconcileRevokedDeviceWorker(context, normalizedDeviceId);
822826
}
823827
// Mark affected clients invalid *before* responding so any RPCs already
824828
// pipelined into their WS socket buffer are rejected at the per-request

0 commit comments

Comments
 (0)