From c055bee482bcd4e5d110352492c35f11b4cf3162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ersin=20KO=C3=87?= Date: Wed, 25 Nov 2026 22:07:03 +0200 Subject: [PATCH] sdk/js: follow a gossip-triggered sync with a pull of its own A KEY_ROTATION or POLICY_UPDATE that arrived while a pull was running was lost in two ways: its timer fired mid-pull and sync() handed back the pull already in flight, which may have read the control plane before the event, or the running pull's periodic reschedule cleared its timer. A triggered pull now waits for the one in flight and then pulls again, and the periodic reschedule leaves a pending trigger's timer alone, as Go's one-slot trigger channel and the Python SDK do. --- sdk/js/src/session.ts | 13 +++++++++++-- sdk/js/src/sync.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/sdk/js/src/session.ts b/sdk/js/src/session.ts index 54c04377f..babc62cc6 100644 --- a/sdk/js/src/session.ts +++ b/sdk/js/src/session.ts @@ -244,6 +244,8 @@ export class MeshSession { /** The addresses of the record last announced under this member's peer key. */ #announced: string[] = []; #syncing: Promise | undefined; + /** A triggered pull is due and its timer has not fired yet. */ + #syncTriggered = false; #keepingRelay: Promise | undefined; #keepRelayPending = false; readonly #heldRouterIds: Set; @@ -1056,16 +1058,23 @@ export class MeshSession { if (this.#closed) { return; } + this.#syncTriggered = true; this.#scheduleSync(Math.floor(Math.random() * (this.#syncJitterMs + 1))); } #scheduleSync(delayMs: number): void { clearTimeout(this.#syncTimer); this.#syncTimer = setTimeout(() => { - this.sync() + this.#syncTriggered = false; + // A pull in flight may have read the control plane before this one was + // asked for, so it is not shared: this one starts when it ends. + (this.#syncing ?? Promise.resolve()) + .catch(() => {}) + .then(() => (this.#closed ? undefined : this.sync())) .catch(() => {}) .finally(() => { - if (!this.#closed && this.#syncIntervalMs > 0) { + // A trigger that came in meanwhile keeps its own, earlier timer. + if (!this.#closed && this.#syncIntervalMs > 0 && !this.#syncTriggered) { // Stretched by up to a tenth so a fleet started together does not pull together. this.#scheduleSync(this.#syncIntervalMs + Math.floor(Math.random() * (this.#syncIntervalMs / 10 + 1))); } diff --git a/sdk/js/src/sync.test.ts b/sdk/js/src/sync.test.ts index 467fae445..ef3aff196 100644 --- a/sdk/js/src/sync.test.ts +++ b/sdk/js/src/sync.test.ts @@ -82,6 +82,9 @@ class FakeControlPlane { keys: SigningKey[]; banned: string[] = []; refreshes = 0; + /** The /policies reads so far, one per pull; the hook may hold an answer. */ + policyReads = 0; + onPolicyRead: ((reads: number) => Promise) | undefined; constructor(initial: SigningKey) { this.keys = [initial]; } @@ -127,6 +130,8 @@ class FakeControlPlane { ); } if (req.method === "GET" && path === "/policies") { + this.policyReads++; + await this.onPolicyRead?.(this.policyReads); return proto(toBinary(PolicyConfigGetResponseSchema, create(PolicyConfigGetResponseSchema, { datalogRules: ['granted_publish_all_types(true) <- role("mesh:role:node")'] }))); } if (req.method === "POST" && path === "/refresh") { @@ -318,3 +323,38 @@ test("a banned peer is hung up on and refused at the gate and the handshake", as await session.close(); } }); + +test("an event that arrives while a pull is in flight is followed by another pull", async () => { + const mesh = await AgentMesh.enroll({ controlPlaneUrl: "http://127.0.0.1:1", bootstrapToken: "sbt", fetch: cp.fetch }); + // No jitter, so a triggered pull is due at once; no periodic pull within the test. + const session = await mesh.join({ refreshLeadMs: 0, controlPlaneSyncIntervalMs: 3_600_000, controlPlaneSyncJitterMs: 0 }); + const start = cp.policyReads; + let secondPull!: () => void; + const pulledAgain = new Promise((resolve) => (secondPull = resolve)); + // The first triggered pull holds its /policies answer (it has read the + // control plane) while a second event triggers. The answer is released + // from a timer queued after the second trigger's, so that trigger comes + // due while the first pull is still in flight. + cp.onPolicyRead = (reads) => { + if (reads === start + 2) { + secondPull(); + } + if (reads !== start + 1) { + return Promise.resolve(); + } + return new Promise((release) => { + session.triggerSync(); + setTimeout(release, 0); + }); + }; + let deadline: ReturnType | undefined; + try { + session.triggerSync(); + await Promise.race([pulledAgain, new Promise((resolve) => (deadline = setTimeout(resolve, 5_000)))]); + assert.equal(cp.policyReads, start + 2, "the event that came during the pull was not followed by a pull of its own"); + } finally { + clearTimeout(deadline); + cp.onPolicyRead = undefined; + await session.close(); + } +});