Skip to content

Commit 6bbc6c3

Browse files
Fix CapacityLimiter over-granting tokens on asyncio (#1172)
On the asyncio backend, releasing a token notified the next waiter but did not reserve the token for it until that waiter resumed. A non-blocking acquire issued in that window saw an empty wait queue and a borrower count below the limit, so it slipped through, leaving borrowed_tokens greater than total_tokens and available_tokens negative. Reserve the freed token for the woken waiter the moment its event is set (in _notify_next_waiter and when increasing total_tokens), mirroring the Semaphore/Lock hand-off, so the non-blocking acquire correctly raises WouldBlock. The woken waiter no longer re-adds itself on resume, and a waiter cancelled after being granted the token now releases it to the next in line. Fixes #1170. --------- Co-authored-by: Alex Grönholm <[email protected]>
1 parent 6f82b25 commit 6bbc6c3

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

‎docs/versionhistory.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
2727
``CapacityLimiter.acquire_nowait_on_behalf_of()`` raising ``trio.WouldBlock`` instead
2828
of ``anyio.WouldBlock`` on the ``trio`` backend when there are no tokens available
2929
(`#1218 <https://github.com/agronholm/anyio/pull/1218>`_)
30+
- Fixed ``CapacityLimiter`` on the asyncio backend over-granting tokens
31+
(``borrowed_tokens`` exceeding ``total_tokens`` and ``available_tokens`` going
32+
negative) when a non-blocking acquire was made in the window between a token
33+
being released and the notified waiter resuming. The freed token is now
34+
reserved for the woken waiter right away, so the non-blocking acquire correctly
35+
raises ``WouldBlock``
36+
(`#1170 <https://github.com/agronholm/anyio/issues/1170>`_; PR by @gaoflow)
3037

3138
**4.14.1**
3239

‎src/anyio/_backends/_asyncio.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,7 +2077,8 @@ def total_tokens(self, value: float) -> None:
20772077

20782078
# Notify waiting tasks that they have acquired the limiter
20792079
while self._wait_queue and waiters_to_notify:
2080-
event = self._wait_queue.popitem(last=False)[1]
2080+
borrower, event = self._wait_queue.popitem(last=False)
2081+
self._borrowers.add(borrower)
20812082
event.set()
20822083
waiters_to_notify -= 1
20832084

@@ -2090,9 +2091,10 @@ def available_tokens(self) -> float:
20902091
return self._total_tokens - len(self._borrowers)
20912092

20922093
def _notify_next_waiter(self) -> None:
2093-
"""Notify the next task in line if this limiter has free capacity now."""
2094+
"""Hand a free token to the next task in line, if any."""
20942095
if self._wait_queue and len(self._borrowers) < self._total_tokens:
2095-
event = self._wait_queue.popitem(last=False)[1]
2096+
borrower, event = self._wait_queue.popitem(last=False)
2097+
self._borrowers.add(borrower)
20962098
event.set()
20972099

20982100
def acquire_nowait(self) -> None:
@@ -2124,11 +2126,10 @@ async def acquire_on_behalf_of(self, borrower: object) -> None:
21242126
except BaseException:
21252127
self._wait_queue.pop(borrower, None)
21262128
if event.is_set():
2129+
self._borrowers.discard(borrower)
21272130
self._notify_next_waiter()
21282131

21292132
raise
2130-
2131-
self._borrowers.add(borrower)
21322133
else:
21332134
try:
21342135
await AsyncIOBackend.cancel_shielded_checkpoint()

‎tests/test_synchronization.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,3 +993,31 @@ async def test_acquire_on_behalf_of_nowait(self) -> None:
993993
limiter.acquire_on_behalf_of_nowait(borrower1)
994994
with pytest.raises(WouldBlock):
995995
limiter.acquire_on_behalf_of_nowait(borrower2)
996+
997+
async def test_nowait_acquire_after_release_does_not_oversubscribe(self) -> None:
998+
# Regression test for #1170: a non-blocking acquire issued in the window
999+
# between releasing a token (which notifies the next waiter) and that
1000+
# waiter actually resuming must not slip through, as the freed token is
1001+
# already reserved for the woken waiter. The over-granting bug was
1002+
# specific to the asyncio backend, but the invariant holds on both.
1003+
limiter = CapacityLimiter(1)
1004+
limiter.acquire_on_behalf_of_nowait("A")
1005+
1006+
async def waiter() -> None:
1007+
await limiter.acquire_on_behalf_of("B")
1008+
1009+
async with create_task_group() as tg:
1010+
tg.start_soon(waiter)
1011+
await wait_all_tasks_blocked()
1012+
assert limiter.statistics().tasks_waiting == 1
1013+
1014+
# Frees the only token and reserves it for the still-parked "B"; no
1015+
# token is available, so the nowait acquire must raise WouldBlock.
1016+
limiter.release_on_behalf_of("A")
1017+
with pytest.raises(WouldBlock):
1018+
limiter.acquire_on_behalf_of_nowait("X")
1019+
1020+
assert limiter.borrowed_tokens <= limiter.total_tokens
1021+
assert limiter.available_tokens >= 0
1022+
assert limiter.statistics().borrowers == ("B",)
1023+
limiter.release_on_behalf_of("B")

0 commit comments

Comments
 (0)