{"id":"36619e40dd8f91fd","repo":"vitest-dev/vitest","slug":"session-sessionid-not-found","errorCode":null,"errorMessage":"Session \"${sessionId}\" not found.","messagePattern":"Session \"(.+?)\" not found\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser/src/node/projectParent.ts","lineNumber":199,"sourceCode":"    options: StackTraceParserOptions = {},\n  ): ParsedStack[] {\n    return parseStacktrace(trace, {\n      ...this.stackTraceOptions,\n      ...options,\n    })\n  }\n\n  public readonly cdps: Map<string, BrowserServerCDPHandler> = new Map()\n  private cdpSessionsPromises = new Map<string, Promise<CDPSession>>()\n\n  async ensureCDPHandler(sessionId: string, rpcId: string): Promise<BrowserServerCDPHandler> {\n    const cachedHandler = this.cdps.get(rpcId)\n    if (cachedHandler) {\n      return cachedHandler\n    }\n    const browserSession = this.vitest._browserSessions.getSession(sessionId)\n    if (!browserSession) {\n      throw new Error(`Session \"${sessionId}\" not found.`)\n    }\n\n    const browser = browserSession.project.browser!\n    const provider = browser.provider\n    if (!provider) {\n      throw new Error(`Browser provider is not defined for the project \"${browserSession.project.name}\".`)\n    }\n    if (!provider.getCDPSession) {\n      throw new Error(`CDP is not supported by the provider \"${provider.name}\".`)\n    }\n\n    const session = await this.cdpSessionsPromises.get(rpcId) ?? await (async () => {\n      const promise = provider.getCDPSession!(sessionId).finally(() => {\n        this.cdpSessionsPromises.delete(rpcId)\n      })\n      this.cdpSessionsPromises.set(rpcId, promise)\n      return promise\n    })()","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/browser/src/node/projectParent.ts#L181-L217","documentation":"`ensureCDPHandler` looks up `sessionId` in `vitest._browserSessions`. The session is created when a browser test run begins and destroyed when the orchestrator websocket closes. If the id doesn't match a live session, the CDP handler cannot be created.","triggerScenarios":"Calling `cdp.send(...)` (or any API that routes through `ensureCDPHandler`) with a sessionId that was never created, was already destroyed (page/orchestrator closed), or was passed in wrong.","commonSituations":"Calling CDP after the browser tab was closed; race where CDP is requested before the session is registered; passing a stale sessionId cached across test runs.","solutions":["Ensure the browser test (and its orchestrator page) is still connected when issuing the CDP call.","Re-fetch the current sessionId rather than caching it across runs.","If this happens mid-run, the session may have been torn down by an earlier error — check the test log for connection-close messages."],"exampleFix":"// before\nconst cdp = await ensureCDPHandler(staleSessionId, rpcId)\n\n// after\nconst sessionId = vitest._browserSessions.currentSessionId\nconst cdp = await ensureCDPHandler(sessionId, rpcId)","handlingStrategy":"validation","validationCode":"function sessionActive(vitest: { _browserSessions: { sessionIds: Set<string> } }, id: string): boolean {\n  return vitest._browserSessions.sessionIds.has(id)\n}","typeGuard":null,"tryCatchPattern":"try {\n  await cdp.send('Page.reload')\n} catch (err) {\n  if (err instanceof Error && /Session \".*\" not found/.test(err.message)) {\n    // re-establish the browser session\n  }\n  throw err\n}","preventionTips":["Don't cache sessionId across runs — fetch the live one each time.","Ensure the browser/orchestrator page stays open for the duration of CDP usage.","Check for connection-close messages in the test log if sessions vanish mid-run."],"tags":["cdp","session","browser","rpc"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}