#5377·LMCache

[Bug][MP] Empty KV buffers get cached when a worker disconnects mid-store

Author: yaojiejiaCreated Sep 27, 2026Updated Sep 27, 2026

Label bug, multiprocess

Describe the bug

If an engine-driven (SHM) worker calls prepare_store and then goes away before commit_store, the server ends up caching those empty slots as real KV. The next request with the same prompt gets a cache hit and loads garbage, with no error.

The cause is in _release_entry (lmcache/v1/multiprocess/modules/engine_driven_transfer.py:236). It cleans up pending writes with storage_manager.finish_write(), which commits them instead of throwing them away. StorageManager doesn't have a way to cancel a reservation.

This happens on both unregister and reap. It doesn't take a crash: if the worker's gather throws (e.g. hybrid KV groups on a non-CUDA device), the next normal shutdown triggers it too.

To Reproduce

Runs on CPU with a real StorageManager:

python
import os, time
import torch  # noqa: F401  (import before lmcache native ext)
from lmcache.v1.distributed.config import (
    EvictionConfig, L1ManagerConfig, L1MemoryManagerConfig, StorageManagerConfig,
)
from lmcache.v1.multiprocess.custom_types import (
    IPCCacheServerKey, RegisterEngineDrivenContextPayload,
)
from lmcache.v1.multiprocess.engine_context import MPCacheServerContext
from lmcache.v1.multiprocess.modules.engine_driven_transfer import EngineDrivenTransferModule
from lmcache.v1.multiprocess.modules.lookup import LookupModule

def worker(iid):
    return RegisterEngineDrivenContextPayload(
        instance_id=iid, model_name="m", world_size=1, block_size=4, num_layers=2,
        hidden_dim_size=16, dtype_str="float32", use_mla=False, num_physical_slots=None)

def key(rid):  # 16 tokens = 2 chunks
    return IPCCacheServerKey.from_token_ids("m", 1, 0, [1] * 16, start=0, end=16, request_id=rid)

ctx = MPCacheServerContext(
    storage_manager_config=StorageManagerConfig(
        l1_manager_config=L1ManagerConfig(memory_config=L1MemoryManagerConfig(
            size_in_bytes=1 << 22, use_lazy=False, shm_name=f"repro_{os.getpid()}")),
        eviction_config=EvictionConfig(eviction_policy="LRU")),
    chunk_size=8)
edt, lookup = EngineDrivenTransferModule(ctx), LookupModule(ctx)

# Worker 1 reserves slots, then leaves without writing or committing.
edt.register_kv_cache_engine_driven_context(worker(1))
edt.prepare_store(key("dying"), 1)
edt.unregister_kv_cache(1)

# Another request for the same prompt.
edt.register_kv_cache_engine_driven_context(worker(2))
lookup.lookup(key("next").no_worker_id_version(), 1)
while (hits := lookup.query_prefetch_status("next")) is None:
    time.sleep(0.01)
print("hit chunks:", hits)
print("retrieve ok:", edt.prepare_retrieve(key("next"), 2).success)
ctx.close()

On dev (33bcad15) this prints:

hit chunks: 2
retrieve ok: True

Expected behavior

An uncommitted store should be dropped. The lookup should miss (hit chunks: 0), and a later store of the same prompt should write every chunk again.

Proposed fix

Add StorageManager.abort_write(keys), a thin wrapper over L1's existing finish_write_and_delete, and call it from _release_entry instead of finish_write. The current unit test mocks storage and asserts the finish_write call, so it needs updating too. PR coming.