@@ -391,9 +391,10 @@ async def init(self):
391
391
self .logger .debug ("Initializing Stagehand..." )
392
392
self .logger .debug (f"Environment: { self .env } " )
393
393
394
- # Always initialize playwright with timeout to avoid hanging
395
- # This ensures compatibility with strict event loop environments
396
- self ._playwright = await self ._init_playwright_with_timeout ()
394
+ # Initialize Playwright with timeout
395
+ self ._playwright = await asyncio .wait_for (
396
+ async_playwright ().start (), timeout = 30.0 # 30 second timeout
397
+ )
397
398
398
399
if self .env == "BROWSERBASE" :
399
400
# Create session if we don't have one
@@ -452,37 +453,11 @@ async def init(self):
452
453
453
454
self ._initialized = True
454
455
455
- async def _init_playwright_with_timeout (self ):
456
- """
457
- Initialize playwright with a timeout to avoid hanging in strict event loop environments.
458
-
459
- This method adds a timeout to the regular async_playwright().start() to prevent
460
- hanging in environments that restrict blocking operations.
461
- """
462
- self .logger .debug ("Starting playwright initialization with timeout..." )
463
-
464
- try :
465
- # Use asyncio.wait_for to add a timeout to prevent hanging
466
- # If the environment doesn't allow blocking operations, this will fail fast
467
- playwright_instance = await asyncio .wait_for (
468
- async_playwright ().start (), timeout = 30.0 # 30 second timeout
469
- )
470
-
471
- self .logger .debug ("Playwright initialized successfully" )
472
- return playwright_instance
473
-
474
- except asyncio .TimeoutError :
475
- self .logger .error ("Playwright initialization timed out" )
476
- raise RuntimeError (
477
- "Playwright initialization timed out after 30 seconds. This may indicate "
478
- "your environment has strict event loop restrictions."
479
- ) from None
480
- except Exception as e :
481
- self .logger .error (f"Failed to initialize playwright: { e } " )
482
- raise RuntimeError (
483
- "Failed to initialize Playwright. This may indicate your environment "
484
- "has restrictions on subprocess creation or event loop operations."
485
- ) from e
456
+ async def _connect_local_browser_threaded (self , playwright , launch_options , stagehand , logger ):
457
+ """Connect to local browser using the threaded Playwright instance."""
458
+ return await self ._playwright_runner .run_coroutine_async (
459
+ connect_local_browser (playwright , launch_options , stagehand , logger )
460
+ )
486
461
487
462
def agent (self , ** kwargs ) -> Agent :
488
463
"""
0 commit comments