Repository navigation
Conversation
Signed-off-by: Mikhail Basov <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request refactors the token TTL reaping logic in capacity_queue.py by extracting _reap_expired_tokens_once to facilitate testing, and updates the acquire method to rely on _fulfill_waiters for token accounting. It also adds comprehensive unit tests to verify FIFO ordering and expiration behavior. A critical feedback item highlights a potential race condition in acquire where a waiter cancellation or timeout after token allocation could lead to a token leak; a code suggestion is provided to safely release the token in such cases.
| try: | ||
| replica_id = await asyncio.wait_for(future, timeout=timeout) | ||
| self._record_acquire_timestamp(replica_id) | ||
| return replica_id | ||
| # _fulfill_waiters() accounts for the token before waking this waiter. | ||
| return await asyncio.wait_for(future, timeout=timeout) | ||
| except asyncio.TimeoutError: | ||
| self._total_timeouts += 1 | ||
| self._waiters = deque((f, t) for f, t in self._waiters if f is not future) |
There was a problem hiding this comment.
There is a potential race condition where a waiter is cancelled (or times out) after _fulfill_waiters() has already allocated a token and called future.set_result(replica_id), but before the waiter task actually resumes and returns. In this scenario, asyncio.wait_for raises CancelledError (or TimeoutError), causing the token to be leaked because the caller never receives the replica_id to release it.\n\nTo prevent this leak, we should check if the future was completed successfully and release the allocated token if an exception or cancellation occurs.
try:\n try:\n # _fulfill_waiters() accounts for the token before waking this waiter.\n return await asyncio.wait_for(future, timeout=timeout)\n except BaseException:\n if future.done() and not future.cancelled():\n self.release(future.result())\n raise\n except asyncio.TimeoutError:\n self._total_timeouts += 1\n self._waiters = deque((f, t) for f, t in self._waiters if f is not future)
Description
In
CapacityQueue.acquire(), when a caller waits on the slow path,_fulfill_waiters()already incrementsin_flightand records the acquire timestamp before resolving the future. Calling_record_acquire_timestamp(replica_id)again afterawait asyncio.wait_for(future, ...)left an extra timestamp in_in_flight_timestamps[replica_id].After that caller calls
release(), the extra timestamp stays behind and is later popped by_reap_expired_tokens(), decrementingin_flighton a subsequent live request and allowing concurrent requests to exceedmax_capacity.This removes the duplicate
_record_acquire_timestamp()call on slow-path resumption, splits out_reap_expired_tokens_once()for deterministic clock-based tests, and adds unit tests covering slow-path release (idle and follow-up active acquire) as well as multiple queued FIFO waiters under TTL.Related issues
Fixes #68640
Additional information
Ran
python/ray/serve/tests/unit/test_capacity_queue.py(43 passed) andruff checkon both modified files.