From 26bc6198c88f4675c8894eceb994a45f4bc4593d Mon Sep 17 00:00:00 2001 From: Cesar Sulbaran Date: Fri, 19 Sep 2025 13:43:51 +0200 Subject: [PATCH] TASK-1293 Improving retry when context loss --- src/main/cdp/frame.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/cdp/frame.ts b/src/main/cdp/frame.ts index 5892e39..f0ddd58 100644 --- a/src/main/cdp/frame.ts +++ b/src/main/cdp/frame.ts @@ -197,17 +197,26 @@ export class Frame extends EventEmitter { } private async withRetryOnContextLoss(fn: (ctx: ExecutionContext) => Promise): Promise { - let ctx = await this.getCurrentExecutionContext(); - try { - return await fn(ctx); - } catch (err: any) { - if (/Cannot find context with specified id/.test(err.message)) { - // Recreate isolated world and retry once - this._isolatedWorld = null; - ctx = await this.getCurrentExecutionContext(); + const maxAttempts = 3; + let attempts = 0; + let lastError: any; + while (attempts < maxAttempts) { + const ctx = await this.getCurrentExecutionContext(); + try { return await fn(ctx); + } catch (err: any) { + lastError = err; + if (/Cannot find context with specified id/.test(err.message)) { + this._isolatedWorld = null; + attempts += 1; + if (attempts < maxAttempts) { + await new Promise(resolve => setTimeout(resolve, 100)); + continue; + } + } + throw err; } - throw err; } + throw lastError; } }