22import json
33import os
44import shutil
5- import tempfile
65import signal
76import sys
7+ import tempfile
88import time
99from pathlib import Path
1010from typing import Any , Literal , Optional
2323from .config import StagehandConfig , default_config
2424from .context import StagehandContext
2525from .llm import LLMClient
26+ from .logging import StagehandLogger , default_log_handler
2627from .metrics import StagehandFunctionName , StagehandMetrics
2728from .page import StagehandPage
2829from .schemas import AgentConfig
2930from .utils import (
30- StagehandLogger ,
3131 convert_dict_keys_to_camel_case ,
32- default_log_handler ,
3332 make_serializable ,
3433)
3534
@@ -47,7 +46,7 @@ class Stagehand:
4746
4847 # Dictionary to store one lock per session_id
4948 _session_locks = {}
50-
49+
5150 # Flag to track if cleanup has been called
5251 _cleanup_called = False
5352
@@ -193,7 +192,7 @@ def __init__(
193192 raise ValueError (
194193 "browserbase_project_id is required for BROWSERBASE env with existing session_id (or set BROWSERBASE_PROJECT_ID in env)."
195194 )
196-
195+
197196 # Register signal handlers for graceful shutdown
198197 self ._register_signal_handlers ()
199198
@@ -224,13 +223,16 @@ def __init__(
224223
225224 def _register_signal_handlers (self ):
226225 """Register signal handlers for SIGINT and SIGTERM to ensure proper cleanup."""
226+
227227 def cleanup_handler (sig , frame ):
228228 # Prevent multiple cleanup calls
229229 if self .__class__ ._cleanup_called :
230230 return
231231
232232 self .__class__ ._cleanup_called = True
233- print (f"\n [{ signal .Signals (sig ).name } ] received. Ending Browserbase session..." )
233+ print (
234+ f"\n [{ signal .Signals (sig ).name } ] received. Ending Browserbase session..."
235+ )
234236
235237 try :
236238 # Try to get the current event loop
@@ -252,11 +254,11 @@ def cleanup_handler(sig, frame):
252254 def schedule_cleanup ():
253255 task = asyncio .create_task (self ._async_cleanup ())
254256 # Shield the task to prevent it from being cancelled
255- shielded = asyncio .shield (task )
257+ asyncio .shield (task )
256258 # We don't need to await here since we're in call_soon_threadsafe
257-
259+
258260 loop .call_soon_threadsafe (schedule_cleanup )
259-
261+
260262 except Exception as e :
261263 print (f"Error during signal cleanup: { str (e )} " )
262264 sys .exit (1 )
0 commit comments