Skip to content

Commit 1793f5d

Browse files
Validate async retry operations return awaitables
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 3e385a9 commit 1793f5d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

hyperbrowser/client/polling.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,24 @@ async def retry_operation_async(
298298
failures = 0
299299
while True:
300300
try:
301-
return await operation()
301+
operation_result = operation()
302+
except Exception as exc:
303+
failures += 1
304+
if failures >= max_attempts:
305+
raise HyperbrowserError(
306+
f"{operation_name} failed after {max_attempts} attempts: {exc}"
307+
) from exc
308+
await asyncio.sleep(retry_delay_seconds)
309+
continue
310+
311+
operation_awaitable = _ensure_awaitable(
312+
operation_result,
313+
callback_name="operation",
314+
operation_name=operation_name,
315+
)
316+
317+
try:
318+
return await operation_awaitable
302319
except Exception as exc:
303320
failures += 1
304321
if failures >= max_attempts:

tests/test_polling.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ async def operation() -> str:
177177
asyncio.run(run())
178178

179179

180+
def test_retry_operation_async_rejects_non_awaitable_operation_result() -> None:
181+
async def run() -> None:
182+
with pytest.raises(
183+
HyperbrowserError, match="operation must return an awaitable"
184+
):
185+
await retry_operation_async(
186+
operation_name="invalid-async-retry-awaitable",
187+
operation=lambda: "ok", # type: ignore[return-value]
188+
max_attempts=2,
189+
retry_delay_seconds=0.0001,
190+
)
191+
192+
asyncio.run(run())
193+
194+
180195
def test_async_poll_until_terminal_status_allows_immediate_terminal_on_zero_max_wait():
181196
async def run() -> None:
182197
status = await poll_until_terminal_status_async(

0 commit comments

Comments
 (0)