@@ -2,7 +2,7 @@ import { Page, BrowserContext } from "@browserbasehq/stagehand";
2
2
import type { Config } from "../config.d.ts" ;
3
3
import type { Cookie } from "playwright-core" ;
4
4
import { createStagehandInstance } from "./stagehandStore.js" ;
5
- import { retryClearScreenshotsForSession } from "./mcp/resources.js" ;
5
+ import { clearScreenshotsForSession } from "./mcp/resources.js" ;
6
6
import type { BrowserSession } from "./types/types.js" ;
7
7
8
8
// Global state for managing browser sessions
@@ -134,10 +134,18 @@ export async function createNewBrowserSession(
134
134
}
135
135
136
136
// 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
+ ) ;
141
149
}
142
150
} ) ;
143
151
@@ -201,10 +209,18 @@ async function closeBrowserGracefully(
201
209
`[SessionManager] Successfully closed Stagehand and browser for session: ${ sessionIdToLog } \n` ,
202
210
) ;
203
211
// 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
+ ) ;
208
224
}
209
225
} catch ( closeError ) {
210
226
process . stderr . write (
@@ -350,7 +366,15 @@ export async function cleanupSession(sessionId: string): Promise<void> {
350
366
browsers . delete ( sessionId ) ;
351
367
352
368
// 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
+ }
354
378
355
379
// Clear default session reference if this was the default
356
380
if ( sessionId === defaultSessionId && defaultBrowserSession ) {
0 commit comments