Skip to content

Commit c091003

Browse files
tyiuhccursoragent
andcommitted
fix(experiment-tag): guard relay callbacks against stale clients (WEB-130)
Tie beginRelaySync completion to the RelayClient instance that started it so a replaced client is not destroyed by an older promise. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 864c1eb commit c091003

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

packages/experiment-tag/src/experiment.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -729,17 +729,21 @@ export class DefaultWebExperimentClient implements WebExperimentClient {
729729
}
730730

731731
this.relayClient?.destroy();
732-
this.relayClient = new RelayClient(
732+
const relayClient = new RelayClient(
733733
this.apiKey,
734734
webExpIdV2,
735735
getRelayUrl(this.apiKey),
736736
);
737+
this.relayClient = relayClient;
737738

738739
void this.behavioralTargetingManager
739-
.beginRelaySync(this.relayClient)
740+
.beginRelaySync(relayClient)
740741
.then((behaviorsChanged) => {
741-
if (!this.relayClient?.relayAvailable) {
742-
this.relayClient?.destroy();
742+
if (this.relayClient !== relayClient) {
743+
return;
744+
}
745+
if (!relayClient.relayAvailable) {
746+
relayClient.destroy();
743747
this.relayClient = null;
744748
return;
745749
}
@@ -748,7 +752,10 @@ export class DefaultWebExperimentClient implements WebExperimentClient {
748752
});
749753
})
750754
.catch(() => {
751-
this.relayClient?.destroy();
755+
if (this.relayClient !== relayClient) {
756+
return;
757+
}
758+
relayClient.destroy();
752759
this.relayClient = null;
753760
});
754761
}

0 commit comments

Comments
 (0)