Skip to content

Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.

fix(concurrency): recheck throttling after queued permit admission (#150)

Rudy Mizrahi Celeklibcf24f39ca5d
diff --git a/ifixai/core/concurrency.py b/ifixai/core/concurrency.py
index 2b303da..e29733b 100644
--- a/ifixai/core/concurrency.py
+++ b/ifixai/core/concurrency.py
@@ -101,6 +101,11 @@ class ConcurrencyGovernor:
async with self._throttle_cond:
await self._throttle_cond.wait_for(lambda: not self._throttled)
async with self._semaphore:
+ # This call may have queued before a 429 started the cooldown.
+ # Recheck after admission so releasing an in-flight permit cannot
+ # silently bypass the shared throttle.
+ async with self._throttle_cond:
+ await self._throttle_cond.wait_for(lambda: not self._throttled)
yield
async def reserve_judge_call(self) -> None:
diff --git a/ifixai/tests/test_governor_queued_throttle.py b/ifixai/tests/test_governor_queued_throttle.py
new file mode 100644
index 0000000..f3818c2
--- /dev/null
+++ b/ifixai/tests/test_governor_queued_throttle.py
@@ -0,0 +1,70 @@
+import asyncio
+
+import pytest
+
+from ifixai.core.concurrency import ConcurrencyGovernor
+
+
+async def test_waiter_queued_before_rate_limit_does_not_issue_during_cooldown():
+ governor = ConcurrencyGovernor(1)
+ entered = asyncio.Event()
+
+ async def wait():
+ async with governor.acquire():
+ entered.set()
+
+ async with governor.acquire():
+ waiter = asyncio.create_task(wait())
+ await asyncio.sleep(0)
+ await governor.on_rate_limit()
+ try:
+ await asyncio.sleep(0.01)
+ assert not entered.is_set(), (
+ "a queued request bypassed the active rate-limit cooldown"
+ )
+ async with governor._throttle_cond:
+ governor._throttled = False
+ governor._throttle_cond.notify_all()
+ await asyncio.wait_for(waiter, 1)
+ assert entered.is_set()
+ finally:
+ if governor._recovery_task:
+ governor._recovery_task.cancel()
+ await asyncio.gather(governor._recovery_task, return_exceptions=True)
+ waiter.cancel()
+ await asyncio.gather(waiter, return_exceptions=True)
+
+
+async def test_cancelled_cooldown_waiter_does_not_leak_permit():
+ governor = ConcurrencyGovernor(1)
+ entered = asyncio.Event()
+
+ async def acquire_again():
+ async with governor.acquire():
+ entered.set()
+
+ async with governor.acquire():
+ waiter = asyncio.create_task(acquire_again())
+ await asyncio.sleep(0)
+ await governor.on_rate_limit()
+ try:
+ # The queued waiter acquires the released semaphore, then must wait at
+ # the second throttle gate while it holds the permit.
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+ assert governor._semaphore._value == 0
+ assert not entered.is_set()
+ waiter.cancel()
+ await asyncio.gather(waiter, return_exceptions=True)
+ async with governor._throttle_cond:
+ governor._throttled = False
+ governor._throttle_cond.notify_all()
+ await asyncio.wait_for(acquire_again(), 1)
+ assert entered.is_set()
+ finally:
+ waiter.cancel()
+ await asyncio.gather(waiter, return_exceptions=True)
+ governor._recovery_task.cancel()
+ await asyncio.gather(governor._recovery_task, return_exceptions=True)