[Bugfix][KV Cache] Prevent negative external block allocation (#52707)

Signed-off-by: haic0 <149741444+haic0@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
haic0
2026-08-27 20:10:45 -07:00
committed by GitHub
co-authored by Cursor
parent b74ac0bc61
commit 53bf990502
2 changed files with 46 additions and 2 deletions
+3 -2
View File
@@ -317,9 +317,10 @@ class SingleTypeKVCacheManager(ABC):
return
req_blocks = self.req_to_blocks[request_id]
allocated_blocks = self.block_pool.get_new_blocks(
cdiv(num_total_computed_tokens, self.block_size) - len(req_blocks)
num_new_blocks = max(
0, cdiv(num_total_computed_tokens, self.block_size) - len(req_blocks)
)
allocated_blocks = self.block_pool.get_new_blocks(num_new_blocks)
req_blocks.extend(allocated_blocks)
if self._record_new_block_ids:
self.new_block_ids.extend(b.block_id for b in allocated_blocks)