Skip to content

sdk/js: follow a gossip-triggered sync with a pull of its own - #950

Merged
aojea merged 2 commits into
google:mainfrom
ersinkoc:audit/sdk-sync
Nov 28, 2026
Merged

aojea merged 2 commits into
google:mainfrom
ersinkoc:audit/sdk-sync

Conversation

@ersinkoc

Copy link
Copy Markdown
Contributor

In the JavaScript SDK, a KEY_ROTATION or POLICY_UPDATE event that arrived while a pull was running could be 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;
  • the running pull's periodic reschedule cleared the trigger's pending 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. This matches the Go node's one-slot trigger channel and the Python SDK, which re-arms its trigger before each pull.

The new test in sync.test.ts failed on the previous code (one pull after the event instead of two). The full JavaScript suite and the type check pass.

@ersinkoc
ersinkoc requested a review from aojea as a code owner November 25, 2026 20:30

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request ensures that if a sync event is triggered while a control plane sync is already in flight, a subsequent sync is scheduled and executed once the current one completes. It introduces a #syncTriggered flag to track pending syncs and chains the new sync execution after the existing #syncing promise. Additionally, a new integration test has been added to verify this behavior. I have no feedback to provide as the implementation is clean and includes appropriate test coverage.

@aojea

aojea commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

needs rebase

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.
@ersinkoc

Copy link
Copy Markdown
Contributor Author

Rebased on main. The only conflict was the new #refreshing field next to #syncTriggered; both are kept, and the triggered pull still skips its sync() once close() has started.

@aojea

aojea commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

A couple of small observations:

  • Early return in triggerSync(): If this.#syncTriggered is already true, returning early prevents a burst of events from resetting the jitter timer, matching Go's one-slot channel.
  • Avoid premature periodic timer while queued: When a triggered timer fires mid-pull, it clears #syncTriggered = false; when Pull 1 settles, Pull 1's .finally() still schedules a periodic timer even though Pull 2 is queued right behind it (harmless at 15m, but can cause back-to-back pulls if controlPlaneSyncIntervalMs is shorter than a slow pull). Guarding .finally() while a pull is queued behind #syncing avoids that.
  • Test for the pending-timer path: The new test covers the mid-pull firing path (jitter = 0 firing before release), but not the case where Pull 1 finishes while #syncTriggered is still true and its .finally() must leave the pending timer alone.

A trigger while one is already due no longer pushes its timer back, like
the router's one-slot channel. A pull with another queued behind it no
longer schedules a periodic pull; the queued one does when it ends.

The test router accepts more than five inbound connections per second:
every member in these tests dials from 127.0.0.1.
@ersinkoc

Copy link
Copy Markdown
Contributor Author

Thanks, all three are in 7f0b7a0:

  • triggerSync() now returns early while a triggered pull is already due, so a burst keeps the first event's delay, like the router's one-slot channel.
  • Each sync timer firing takes a run number, and only the latest run schedules the next periodic pull. A pull with another queued behind it no longer schedules one; the queued pull does when it ends.
  • The pending-timer case already held (the !#syncTriggered guard); it now has a test. A second event arrives during the first pull and comes due after that pull ends, and the test checks that its pull still happens instead of being replaced by a periodic timer.

There are also tests for the burst case and the queued-pull case, and both fail on the previous commit. One change to the test setup: the test router now accepts more than libp2p's default of five inbound connections per host per second. All members in sync.test.ts dial from 127.0.0.1, and with the extra joins the later tests were being refused.

@aojea
aojea merged commit a55f42a into google:main Nov 28, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants