Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(ws): prevent duplicate connection listeners (#1453)

teocaeb4a0abe84
diff --git a/js/app/packages/websocket/core/websocket.ts b/js/app/packages/websocket/core/websocket.ts
index dec379b9ce..8cc8403cfe 100644
--- a/js/app/packages/websocket/core/websocket.ts
+++ b/js/app/packages/websocket/core/websocket.ts
@@ -608,6 +608,8 @@ export class Websocket<Send = WebsocketData, Receive = WebsocketData> {
return; // no backoff defined, no retry
}
+ this.cancelScheduledConnectionRetry();
+
// handler dispatches the retry event to all listeners of the retry event-type
const handleRetryEvent = (detail: RetryEventDetail) => {
const event: CustomEvent<RetryEventDetail> = new CustomEvent(
diff --git a/js/app/packages/websocket/tests/websocket-heartbeat.test.ts b/js/app/packages/websocket/tests/websocket-heartbeat.test.ts
index 7eff725d1a..5e6e835eda 100644
--- a/js/app/packages/websocket/tests/websocket-heartbeat.test.ts
+++ b/js/app/packages/websocket/tests/websocket-heartbeat.test.ts
@@ -169,4 +169,32 @@ describe('Testsuite for Websocket with Heartbeat', () => {
expect(restartIndex).toBeGreaterThan(2);
expect(missedCounts.slice(0, 3)).toEqual([1, 2, 3]);
});
+
+ test(
+ 'Calling scheduleConnectionRetryIfNeeded twice should only produce one retry',
+ async () => {
+ let retryCount = 0;
+
+ await new Promise<void>((resolve) => {
+ client = new WebsocketBuilder(url)
+ .withBackoff(new ConstantBackoff(50))
+ .withMaxRetries(Infinity)
+ .onRetry(() => {
+ retryCount++;
+ })
+ .onOpen(() => resolve())
+ .build();
+ });
+
+ // Simulate the race between the synthetic close event (from sendHeartbeat
+ // detecting CLOSING state) and the real browser close event — both call
+ // scheduleConnectionRetryIfNeeded before either retry timeout fires.
+ (client as any).scheduleConnectionRetryIfNeeded();
+ (client as any).scheduleConnectionRetryIfNeeded();
+
+ await new Promise((r) => setTimeout(r, 300));
+ expect(retryCount).toBe(1);
+ },
+ testTimeout
+ );
});