@@ -2,7 +2,7 @@ import { Page, BrowserContext } from "@browserbasehq/stagehand";
22import type { Config } from "../config.d.ts" ;
33import type { Cookie } from "playwright-core" ;
44import { createStagehandInstance } from "./stagehandStore.js" ;
5- import { retryClearScreenshotsForSession } from "./mcp/resources.js" ;
5+ import { clearScreenshotsForSession } from "./mcp/resources.js" ;
66import type { BrowserSession } from "./types/types.js" ;
77
88// Global state for managing browser sessions
@@ -134,10 +134,18 @@ export async function createNewBrowserSession(
134134 }
135135
136136 // Purge any screenshots associated with both internal and Browserbase IDs
137- retryClearScreenshotsForSession ( newSessionId ) . catch ( ( ) => { } ) ;
138- const bbId = browserbaseSessionId ;
139- if ( bbId ) {
140- retryClearScreenshotsForSession ( bbId ) . catch ( ( ) => { } ) ;
137+ try {
138+ clearScreenshotsForSession ( newSessionId ) ;
139+ const bbId = browserbaseSessionId ;
140+ if ( bbId ) {
141+ clearScreenshotsForSession ( bbId ) ;
142+ }
143+ } catch ( err ) {
144+ process . stderr . write (
145+ `[SessionManager] WARN - Failed to clear screenshots on disconnect for ${ newSessionId } : ${
146+ err instanceof Error ? err . message : String ( err )
147+ } \n`,
148+ ) ;
141149 }
142150 } ) ;
143151
@@ -201,10 +209,18 @@ async function closeBrowserGracefully(
201209 `[SessionManager] Successfully closed Stagehand and browser for session: ${ sessionIdToLog } \n` ,
202210 ) ;
203211 // After close, purge any screenshots associated with both internal and Browserbase IDs
204- retryClearScreenshotsForSession ( sessionIdToLog ) . catch ( ( ) => { } ) ;
205- const bbId = session ?. stagehand ?. browserbaseSessionID ;
206- if ( bbId ) {
207- retryClearScreenshotsForSession ( bbId ) . catch ( ( ) => { } ) ;
212+ try {
213+ clearScreenshotsForSession ( sessionIdToLog ) ;
214+ const bbId = session ?. stagehand ?. browserbaseSessionID ;
215+ if ( bbId ) {
216+ clearScreenshotsForSession ( bbId ) ;
217+ }
218+ } catch ( err ) {
219+ process . stderr . write (
220+ `[SessionManager] WARN - Failed to clear screenshots after close for ${ sessionIdToLog } : ${
221+ err instanceof Error ? err . message : String ( err )
222+ } \n`,
223+ ) ;
208224 }
209225 } catch ( closeError ) {
210226 process . stderr . write (
@@ -350,7 +366,15 @@ export async function cleanupSession(sessionId: string): Promise<void> {
350366 browsers . delete ( sessionId ) ;
351367
352368 // Always purge screenshots for this (internal) session id
353- await retryClearScreenshotsForSession ( sessionId ) . catch ( ( ) => { } ) ;
369+ try {
370+ clearScreenshotsForSession ( sessionId ) ;
371+ } catch ( err ) {
372+ process . stderr . write (
373+ `[SessionManager] WARN - Failed to clear screenshots during cleanup for ${ sessionId } : ${
374+ err instanceof Error ? err . message : String ( err )
375+ } \n`,
376+ ) ;
377+ }
354378
355379 // Clear default session reference if this was the default
356380 if ( sessionId === defaultSessionId && defaultBrowserSession ) {
0 commit comments