From e652fdc13072cc1617eb8ac2e02ee8b95e11a432 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 14 Dec 2025 11:59:43 +0000 Subject: [PATCH] Fix duplicate answer processing race condition (#6) Add polling guard to prevent concurrent pollInternal() execution. The setInterval callback doesn't await the async pollInternal(), which could cause multiple polls to process the same answer before lastPollTimestamp is updated, resulting in "Called in wrong state: stable" errors from setRemoteDescription(). --- src/rondevu.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/rondevu.ts b/src/rondevu.ts index 3d7c3de..9522a93 100644 --- a/src/rondevu.ts +++ b/src/rondevu.ts @@ -259,6 +259,7 @@ export class Rondevu extends EventEmitter { private filling = false private pollingInterval: ReturnType | null = null private lastPollTimestamp = 0 + private isPolling = false // Guard against concurrent poll execution private constructor( apiUrl: string, @@ -634,6 +635,13 @@ export class Rondevu extends EventEmitter { private async pollInternal(): Promise { if (!this.filling) return + // Prevent concurrent poll execution to avoid duplicate answer processing + if (this.isPolling) { + this.debug('Poll already in progress, skipping') + return + } + + this.isPolling = true try { const result = await this.api.poll(this.lastPollTimestamp) @@ -674,6 +682,8 @@ export class Rondevu extends EventEmitter { } } catch (err) { console.error('[Rondevu] Polling error:', err) + } finally { + this.isPolling = false } } @@ -710,6 +720,7 @@ export class Rondevu extends EventEmitter { stopFilling(): void { this.debug('Stopping offer filling and polling') this.filling = false + this.isPolling = false // Reset polling guard // Stop polling if (this.pollingInterval) {