{"record":{"id":"7d827b3c694640ea","repo":"mastra-ai/mastra","slug":"shared-browser-not-launched-call-createsharedsess","errorCode":null,"errorMessage":"Shared browser not launched. Call createSharedSession() first.","messagePattern":"Shared browser not launched\\. Call createSharedSession\\(\\) first\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"browser/browser-viewer/src/thread-manager.ts","lineNumber":682,"sourceCode":"    await this.closeSharedBrowser();\n  }\n\n  /**\n   * Get the manager for a session.\n   * Required by base class.\n   */\n  protected getManagerForSession(session: ThreadSession): Browser {\n    const viewerSession = session as BrowserViewerSession;\n    return viewerSession.browser;\n  }\n\n  /**\n   * Get the shared manager.\n   * Required by base class.\n   */\n  protected getSharedManager(): Browser {\n    if (!this.sharedSession) {\n      throw new Error('Shared browser not launched. Call createSharedSession() first.');\n    }\n    return this.sharedSession.browser;\n  }\n\n  /**\n   * Destroy a session and clean up resources.\n   * Required by base class.\n   */\n  protected async doDestroySession(session: ThreadSession): Promise<void> {\n    const viewerSession = this.getViewerSession(session.threadId);\n    if (!viewerSession) {\n      return;\n    }\n    await this.cleanupSession(viewerSession, session.threadId);\n  }\n\n  /**\n   * Check if browser is running for a thread.","sourceCodeStart":664,"sourceCodeEnd":700,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/browser/browser-viewer/src/thread-manager.ts#L664-L700","documentation":"getSharedManager is the base-class hook that must return the shared Browser instance. The shared browser is only created by createSharedSession(); if shared-manager APIs are reached before that launch (or after destroy cleared sharedSession), the internal invariant is violated and this guard error is thrown.","triggerScenarios":"Invoking shared-browser operations through getSharedManager before createSharedSession() was called, mixing shared-scope APIs into a thread-scoped manager, or accessing after destroy() cleared sharedSession.","commonSituations":"Lifecycle ordering bugs where shared APIs run before initialization completes; code assuming shared scope on a thread-scope manager; access during/after teardown.","solutions":["Call createSharedSession() before any shared-browser operations.","Check shared-session readiness first and lazily create the session if absent.","Ensure destroy() isn't invoked before or during shared access.","Use thread-session methods instead of shared-manager APIs for thread-scoped browsers."],"exampleFix":"// before\nconst browser = tm.getSharedManager(); // throws: not launched\n// after\nif (!tm.hasSharedSession?.()) {\n  await tm.createSharedSession();\n}\nconst browser = tm.getSharedManager();","handlingStrategy":"validation","validationCode":"if (!tm.hasSharedSession?.()) {\n  await tm.createSharedSession();\n}\nconst browser = tm.getSharedManager(); // safe: shared session exists","typeGuard":"function sharedSessionReady(tm: { hasSharedSession?: () => boolean }): boolean {\n  return tm.hasSharedSession ? tm.hasSharedSession() : false;\n}","tryCatchPattern":"try {\n  return tm.getSharedManager();\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Shared browser not launched')) {\n    await tm.createSharedSession();\n    return tm.getSharedManager();\n  }\n  throw err;\n}","preventionTips":["Create the shared session during startup before any shared-browser access.","Expose and use a readiness check (e.g. hasSharedSession) before shared APIs.","Never access shared-manager methods after destroy() without re-creating the session.","Keep thread-scope and shared-scope APIs clearly separated to avoid cross-scope misuse."],"tags":["browser","lifecycle","initialization-order","shared-session"],"backgroundTag":"session-not-initialized","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}