Repository navigation
fix(cluster): use reference equality to guard superseded pubSub connect error in #discover() - #3520
Conversation
…very When #discover() removes a pubSub node whose address is no longer in the new topology, it calls destroy() on the client. If both channelsListeners and patternsListeners are empty at that moment (e.g. a SUBSCRIBE is still in-flight or all channels were just unsubscribed), #initiatePubSubClient() is never called and this.pubSubNode is left pointing at the destroyed client. The next getPubSubClient() call sees a non-null pubSubNode and returns Promise.resolve(pubSubNode.client) — the destroyed client — causing ClientClosedError on every subsequent subscribe. Null pubSubNode immediately after destroy(), matching the identical pattern at all five other callsites in the same file.
…very When #discover() removes a pubSub node whose address is no longer in the new topology, it calls destroy() on the client. If both channelsListeners and patternsListeners are empty at that moment, #initiatePubSubClient() is never called and this.pubSubNode is left pointing at the destroyed client, causing ClientClosedError on every subsequent subscribe. Also guard #initiatePubSubClient's .then/.catch callbacks with a local-reference check so that a concurrent rediscovery cannot clobber a newer pubSubNode: the .then destroys the old client and returns early, and the .catch skips the null-assignment when this.pubSubNode has already been replaced. Regression tests use a minimal mock CLUSTER SLOTS server (no Docker required) to exercise both code paths through the real production API.
…stale connect When #initiatePubSubClient's .then callback detects that this.pubSubNode was replaced by a concurrent rediscovery, it was destroying the old client and then returning it to callers. Callers waiting on the original connectPromise would receive a destroyed client and SUBSCRIBE would fail with ClientClosedError. Fix: throw ClientClosedError so the promise rejects cleanly. Also add a second identity check after the async extendPubSubListeners call so that a replacement occurring during resubscription is caught before resolving.
Issue 1 (medium): throwing ClientClosedError on supersession caused #discover()'s Promise.all to reject, which failed the topology refresh and disabled client-side cache even though a replacement pubSubNode was already created. Fix: wrap the #initiatePubSubClient push in #discover() to treat ClientClosedError as non-fatal when this.pubSubNode is non-null (i.e. a concurrent rediscovery already replaced the node). Issue 2 (low): the topology-removal regression test was trivially asserting a manual assignment rather than exercising #discover(). Fix: plant a stale pubSubNode at an address outside the mock topology, call rediscover(), and assert pubSubNode is cleared by the real code.
…ct error The previous guard err instanceof ClientClosedError && this.pubSubNode was racy: if a concurrent connection failure cleared this.pubSubNode after supersession had occurred, the guard evaluated false and re-threw the ClientClosedError, aborting #discover() and disabling client-side cache even though the cluster was healthy. Fix: capture this.pubSubNode immediately after #initiatePubSubClient (which sets it synchronously before any await), then compare by reference. this.pubSubNode !== capturedPubSubNode is true if and only if the node was replaced by a concurrent call, regardless of what subsequently happened to the replacement.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
nkaradzhov
left a comment
There was a problem hiding this comment.
Thanks for looking into this. The race is real, but the new check is always true by the time it runs:
- If this node was replaced,
this.pubSubNodepoints to another node or isundefined. - If this node failed on its own, the
.catchinside#initiatePubSubClient()clearsthis.pubSubNodebefore this guard runs, so it isundefined.
Either way this.pubSubNode !== capturedPubSubNode, so the PR swallows every ClientClosedError from this call. That includes teardown, which the old comment said should re-throw.
What the guard needs to know is whether the cluster is still open. Could you change it to this?
promises.push(
this.#initiatePubSubClient({
[PUBSUB_TYPE.CHANNELS]: channelsListeners,
[PUBSUB_TYPE.PATTERNS]: patternsListeners
}).catch(err => {
// While the cluster is open, ClientClosedError means this pub/sub node was
// deliberately dropped (replaced or destroyed), not that discovery failed.
if (err instanceof ClientClosedError && this.#isOpen) return;
throw err;
})
);No new test needed for this one.
… #discover() The reference-equality guard introduced in the previous commit was always true by the time it ran: either the node was replaced (this.pubSubNode differs) or it failed on its own (the .catch in #initiatePubSubClient clears this.pubSubNode to undefined before this guard fires). Either way the condition was true, silently swallowing every ClientClosedError from this call — including teardown. Use this.#isOpen instead: while the cluster is open, a ClientClosedError from this call means the pub-sub node was deliberately dropped (replaced or destroyed), not that discovery failed. If the cluster was destroyed concurrently, #isOpen is false and the error propagates normally.
|
Hi, This issue is fixed. |
Problem
#discover()pushed the#initiatePubSubClient()promise intopromiseswith a
.catchguard to treat supersession as non-fatal: