Skip to content

Commit 7ab4368

Browse files
authored
Merge pull request #69 from codesensei-tushar/fix/async-sleep-llm-condition
fix: replace blocking time.sleep with asyncio.sleep in LLM condition
2 parents bdac59c + d8d3cfd commit 7ab4368

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- **Blocking sleep in LLM condition** -- Replaced `time.sleep()` with
12+
`await asyncio.sleep()` in `LLMAssisted` retry backoff to avoid
13+
freezing the event loop during LLM retries.
14+
915
### Added
1016

1117
- **AI-powered reviewer recommendation** -- `/reviewers` slash command suggests

src/rules/conditions/llm_assisted.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
opt-in and clearly documented as having LLM latency in the evaluation path.
66
"""
77

8+
import asyncio
89
import logging
910
import time
1011
from typing import Any
@@ -218,7 +219,7 @@ async def evaluate(self, context: Any) -> list[Violation]:
218219
)
219220

220221
if attempt < max_attempts:
221-
time.sleep(wait_time)
222+
await asyncio.sleep(wait_time)
222223
else:
223224
# All attempts failed - gracefully degrade
224225
logger.error("All LLM retry attempts exhausted; skipping alignment check.")

tests/unit/rules/conditions/test_llm_assisted.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ async def test_graceful_degradation_on_llm_failure(self, mock_get_chat_model, co
196196
assert violations == []
197197

198198
@pytest.mark.asyncio
199-
@patch("time.sleep", return_value=None) # Mock sleep to speed up test
199+
@patch("asyncio.sleep", new_callable=AsyncMock) # Mock async sleep to speed up test
200200
@patch("src.integrations.providers.get_chat_model")
201201
async def test_retry_logic_with_exponential_backoff(self, mock_get_chat_model, mock_sleep, condition):
202202
"""When structured invoke fails, retries with exponential backoff."""
@@ -213,7 +213,7 @@ async def test_retry_logic_with_exponential_backoff(self, mock_get_chat_model, m
213213
# Should have retried 3 times total
214214
assert mock_structured.ainvoke.await_count == 3
215215
# Should have slept twice (2s, 4s)
216-
assert mock_sleep.call_count == 2
216+
assert mock_sleep.await_count == 2
217217

218218
@pytest.mark.asyncio
219219
@patch("src.integrations.providers.get_chat_model")

0 commit comments

Comments
 (0)