{"record":{"id":"d995949957122e62","repo":"microsoft/playwright","slug":"execution-context-was-destroyed-most-likely-becau","errorCode":null,"errorMessage":"Execution context was destroyed, most likely because of a navigation.","messagePattern":"Execution context was destroyed, most likely because of a navigation\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/server/chromium/crExecutionContext.ts","lineNumber":113,"sourceCode":"  }\n\n  shouldPrependErrorPrefix(): boolean {\n    return false;\n  }\n}\n\nfunction rewriteError(error: Error): Protocol.Runtime.evaluateReturnValue {\n  if (error.message.includes('Object reference chain is too long') || error.message.includes('CBOR: stack limit exceeded'))\n    throw new Error('Cannot serialize result: object reference chain is too long.');\n  if (error.message.includes('Object couldn\\'t be returned by value'))\n    return { result: { type: 'undefined' } };\n  if (error.message.includes('Promise was collected'))\n    throw new Error('Resulting promise was garbage collected.');\n\n  if (error instanceof TypeError && error.message.startsWith('Converting circular structure to JSON'))\n    rewriteErrorMessage(error, error.message + ' Are you passing a nested JSHandle?');\n  if (!js.isJavaScriptErrorInEvaluate(error) && !isSessionClosedError(error))\n    throw new Error('Execution context was destroyed, most likely because of a navigation.');\n  throw error;\n}\n\nfunction potentiallyUnserializableValue(remoteObject: Protocol.Runtime.RemoteObject): any {\n  const value = remoteObject.value;\n  const unserializableValue = remoteObject.unserializableValue;\n  return unserializableValue ? js.parseUnserializableValue(unserializableValue) : value;\n}\n\nfunction renderPreview(object: Protocol.Runtime.RemoteObject): string | undefined {\n  if (object.type === 'undefined')\n    return 'undefined';\n  if ('value' in object)\n    return String(object.value);\n  if (object.unserializableValue)\n    return String(object.unserializableValue);\n\n  if (object.description === 'Object' && object.preview) {","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/server/chromium/crExecutionContext.ts#L95-L131","documentation":"The catch-all branch of rewriteError() (crExecutionContext.ts:113): if the failure is neither a JavaScript error raised by the page nor a known session-closed error, Playwright assumes the isolated-world execution context was destroyed — almost always because of a navigation or context disposal mid-evaluate.","triggerScenarios":"Calling page.evaluate / locator.evaluate during a navigation that recreates the frame's context; clicking a link that navigates and then evaluating on the stale context; frame removed mid-script.","commonSituations":"Missing await on a click that triggers navigation, then evaluate runs against the destroyed context; client-side routing that swaps the document; popups that navigate away.","solutions":["Await the navigation (page.waitForLoadState or waitForURL) before evaluating.","Use page.waitForFunction so the predicate is retried in the new context.","Retry the evaluate once when this specific message is caught."],"exampleFix":"// before\nawait page.click('#submit');\nconst v = await page.evaluate(() => window.result);\n// after\nawait Promise.all([ page.waitForNavigation(), page.click('#submit') ]);\nawait page.waitForLoadState('domcontentloaded');\nconst v = await page.evaluate(() => window.result);","handlingStrategy":"retry","validationCode":"// Stabilize before evaluating after a navigation trigger.\nawait page.waitForLoadState('domcontentloaded');\nconst v = await page.evaluate(() => (window as any).result);","typeGuard":null,"tryCatchPattern":"async function evalStable(page: Page, fn: string) {\n  for (let attempt = 0; attempt < 3; attempt++) {\n    try {\n      return await page.evaluate(fn);\n    } catch (e) {\n      if (!/Execution context was destroyed/.test(String((e as Error).message)) || attempt === 2) throw e;\n      await page.waitForLoadState('domcontentloaded');\n    }\n  }\n}","preventionTips":["Await navigations explicitly (waitForNavigation / waitForURL).","Use waitForFunction instead of bare evaluate for post-navigation reads.","Do not run evaluate concurrently with actions that may navigate."],"tags":["evaluate","navigation","race","chromium"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}