File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " stagehand " : patch
3
+ ---
4
+
5
+ simple event loop timeout for strict event loops for async playwright (which has blocking start)
Original file line number Diff line number Diff line change @@ -391,7 +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
- self ._playwright = await async_playwright ().start ()
394
+ # Initialize Playwright with timeout
395
+ self ._playwright = await asyncio .wait_for (
396
+ async_playwright ().start (), timeout = 30.0 # 30 second timeout
397
+ )
395
398
396
399
if self .env == "BROWSERBASE" :
397
400
# Create session if we don't have one
Original file line number Diff line number Diff line change @@ -136,6 +136,31 @@ def test_init_as_context_manager(self):
136
136
# Verify close is called in __aexit__
137
137
assert client .close is not None
138
138
139
+ @pytest .mark .asyncio
140
+ async def test_init_playwright_timeout (self ):
141
+ """Test that init() raises TimeoutError when playwright takes too long to start."""
142
+ config = StagehandConfig (env = "LOCAL" )
143
+ client = Stagehand (config = config )
144
+
145
+ # Mock async_playwright to simulate a hanging start() method
146
+ mock_playwright_instance = mock .AsyncMock ()
147
+ mock_start = mock .AsyncMock ()
148
+
149
+ # Make start() hang indefinitely
150
+ async def hanging_start ():
151
+ await asyncio .sleep (100 ) # Sleep longer than the 30s timeout
152
+
153
+ mock_start .side_effect = hanging_start
154
+ mock_playwright_instance .start = mock_start
155
+
156
+ with mock .patch ("stagehand.main.async_playwright" , return_value = mock_playwright_instance ):
157
+ # The init() method should raise TimeoutError due to the 30-second timeout
158
+ with pytest .raises (asyncio .TimeoutError ):
159
+ await client .init ()
160
+
161
+ # Ensure the client is not marked as initialized
162
+ assert client ._initialized is False
163
+
139
164
@pytest .mark .asyncio
140
165
async def test_create_session (self ):
141
166
"""Test session creation."""
You can’t perform that action at this time.
0 commit comments