1+ import asyncio
12import os
23import weakref
34
@@ -85,20 +86,21 @@ async def init(cls, context: BrowserContext, stagehand):
8586
8687 # Add event listener for new pages (popups, new tabs from window.open, etc.)
8788 def handle_page_event (pw_page ):
88- instance ._handle_new_page (pw_page )
89+ # Playwright expects sync handler, so we schedule the async work
90+ asyncio .create_task (instance ._handle_new_page (pw_page ))
8991
9092 context .on ("page" , handle_page_event )
9193
9294 return instance
9395
94- def _handle_new_page (self , pw_page : Page ):
96+ async def _handle_new_page (self , pw_page : Page ):
9597 """
9698 Handle new pages created by the browser (popups, window.open, etc.).
97- This runs synchronously in the event handler context .
99+ Uses the page switch lock to prevent race conditions with ongoing operations .
98100 """
99-
100- async def _async_handle ():
101- try :
101+ try :
102+ # Use timeout to prevent indefinite blocking
103+ async with asyncio . timeout ( 30 ) :
102104 async with self .stagehand ._page_switch_lock :
103105 self .stagehand .logger .debug (
104106 f"Creating StagehandPage for new page with URL: { pw_page .url } " ,
@@ -109,26 +111,14 @@ async def _async_handle():
109111 self .stagehand .logger .debug (
110112 "New page detected and initialized" , category = "context"
111113 )
112- except Exception as e :
113- self .stagehand .logger .error (
114- f"Failed to initialize new page: { str (e )} " , category = "context"
115- )
116- import traceback
117-
118- self .stagehand .logger .error (
119- f"Traceback: { traceback .format_exc ()} " , category = "context"
120- )
121-
122- # Schedule the async work
123- import asyncio
124-
125- try :
126- loop = asyncio .get_running_loop ()
127- loop .create_task (_async_handle ())
128- except RuntimeError :
129- # No event loop running, which shouldn't happen in normal operation
114+ except asyncio .TimeoutError :
115+ self .stagehand .logger .error (
116+ f"Timeout waiting for page switch lock when handling new page: { pw_page .url } " ,
117+ category = "context" ,
118+ )
119+ except Exception as e :
130120 self .stagehand .logger .error (
131- "No event loop available to handle new page" , category = "context"
121+ f"Failed to initialize new page: { str ( e ) } " , category = "context"
132122 )
133123
134124 def __getattr__ (self , name ):
0 commit comments