LMCache / LMCache/LMCache

[Bug] AddressManager accepts size 0, breaks free list / ZeroDivisionError

Open Beginner friendly
#5,115 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
11.9k
Forks
1.9k
Avg merge
4d 4h
Merged PRs (30d)
141

Description

Hey team,

I found `AddressManager.allocate()` and `batched_allocate()` say size should be > 0 but don't check it. On dev (68b7e5f):

```python
from lmcache.v1.memory_management import AddressManager

am = AddressManager(1 << 20)
a = am.allocate(4096)
z = am.allocate(0)
b = am.allocate(4096)
am.free(*z); am.free(*b); am.free(*a)
print([(x.start, x.size) for x in am._explicit_list])
# [(0, 4096), (4096, 4096), (8192, 1040384)] <- never coalesced
am.allocate(1 << 20)
# RuntimeError: no memory is available (everything is free)

AddressManager(1 << 20).batched_allocate(0, 3)
# ZeroDivisionError (block.size // aligned_size)
```

The ZeroDivisionError also gets past `TensorMemoryAllocator.batched_allocate`, which only catches RuntimeError.

I hit this through `SegmentTokenDatabase`: a leading/trailing/double separator gives an empty range (e.g. `(4, 4)`), and `LMCacheEngine.store()` allocates 0 bytes for it.

Fix I'd suggest: raise RuntimeError for size <= 0 in both (same as #5061 did for batch_size) and add tests in `tests/v1/test_address_manager.py`. Happy to send a PR.

Contributor guide

Open the contributing guide

Research direction

Start with AddressManager.allocate() and batched_allocate() in lmcache/v1/memory_management, then read the related tests in tests/v1/test_address_manager.py. Add coverage for zero and negative sizes, including batched allocation, and verify invalid sizes raise RuntimeError without corrupting the free list or causing ZeroDivisionError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
88/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.