@@ -48,7 +48,7 @@ class Stagehand:
4848
4949 # Dictionary to store one lock per session_id
5050 _session_locks = {}
51-
51+
5252 # Flag to track if cleanup has been called
5353 _cleanup_called = False
5454
@@ -194,7 +194,7 @@ def __init__(
194194 raise ValueError (
195195 "browserbase_project_id is required for BROWSERBASE env with existing session_id (or set BROWSERBASE_PROJECT_ID in env)."
196196 )
197-
197+
198198 # Register signal handlers for graceful shutdown
199199 self ._register_signal_handlers ()
200200
@@ -225,13 +225,16 @@ def __init__(
225225
226226 def _register_signal_handlers (self ):
227227 """Register signal handlers for SIGINT and SIGTERM to ensure proper cleanup."""
228+
228229 def cleanup_handler (sig , frame ):
229230 # Prevent multiple cleanup calls
230231 if self .__class__ ._cleanup_called :
231232 return
232233
233234 self .__class__ ._cleanup_called = True
234- print (f"\n [{ signal .Signals (sig ).name } ] received. Ending Browserbase session..." )
235+ print (
236+ f"\n [{ signal .Signals (sig ).name } ] received. Ending Browserbase session..."
237+ )
235238
236239 try :
237240 # Try to get the current event loop
@@ -253,11 +256,11 @@ def cleanup_handler(sig, frame):
253256 def schedule_cleanup ():
254257 task = asyncio .create_task (self ._async_cleanup ())
255258 # Shield the task to prevent it from being cancelled
256- shielded = asyncio .shield (task )
259+ asyncio .shield (task )
257260 # We don't need to await here since we're in call_soon_threadsafe
258-
261+
259262 loop .call_soon_threadsafe (schedule_cleanup )
260-
263+
261264 except Exception as e :
262265 print (f"Error during signal cleanup: { str (e )} " )
263266 sys .exit (1 )
0 commit comments