[Bugfix][Multimodal] Fix reference counting of the SHM processor cache (#59695)

Signed-off-by: tenotek <50898317+hdq66666@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
tenotek
2026-10-02 22:14:13 +08:00
committed by GitHub
co-authored by Claude Fable 5.1
parent c91dccc080
commit 443dccd3c9
5 changed files with 124 additions and 29 deletions
@@ -527,6 +527,9 @@ class SingleWriterShmObjectStorage:
self.id_index: dict[int, str] = {}
# Writer flag to track in-use status: monotonic_id -> count
self.writer_flag: dict[int, int] = {}
# Items touch() keeps from being freed until they are looked up
# or release_touches() is called
self._touched: set[int] = set()
else:
if reader_lock is None:
raise ValueError("Lock must be provided for readers.")
@@ -540,6 +543,7 @@ class SingleWriterShmObjectStorage:
self.key_index.clear()
self.id_index.clear()
self.writer_flag.clear()
self._touched.clear()
logger.debug("Object storage cleared and reinitialized.")
def copy_to_buffer(
@@ -664,8 +668,13 @@ class SingleWriterShmObjectStorage:
"""Get the cached object by key if it exists."""
address, monotonic_id = self.key_index[key]
self.increment_writer_flag(monotonic_id)
self._touched.discard(monotonic_id)
return address, monotonic_id
def release_touches(self) -> None:
"""Stop protecting touched items that were not looked up."""
self._touched.clear()
def get_signature(self, key: str) -> list[int]:
"""Sign the handle of a cached object so readers can verify it."""
address, monotonic_id = self.key_index[key]
@@ -756,10 +765,11 @@ class SingleWriterShmObjectStorage:
monotonic_id: int = 0,
signature: list[int] | None = None,
) -> None:
"""Touch an existing cached item to update its eviction status.
"""Touch an existing cached item.
For writers (ShmObjectStoreSenderCache): Increment writer_flag
For readers (ShmObjectStoreReceiverCache): Increment reader_count
For writers (ShmObjectStoreSenderCache): Protect the item from
eviction until get_cached or release_touches
For readers (ShmObjectStoreReceiverCache): Validate the handle
Args:
key: String key of the object to touch
@@ -771,25 +781,12 @@ class SingleWriterShmObjectStorage:
if self._reader_lock is None:
if key not in self.key_index:
return None
address, monotonic_id = self.key_index[key]
# Writer side: increment writer_flag to raise eviction threshold
self.increment_writer_flag(monotonic_id)
self._touched.add(self.key_index[key][1])
else:
# Reject forged handles before dereferencing the supplied address.
self.verify_signature(key, address, monotonic_id, signature)
with (
self._reader_lock,
self.ring_buffer.access_buf(address) as (data_view, buf_metadata),
):
with self.ring_buffer.access_buf(address) as (_, buf_metadata):
self._validate_monotonic_id(address, monotonic_id, buf_metadata)
reader_count = self.ring_buffer.byte2int(data_view[: self.flag_bytes])
# NOTE(Long):
# Avoid increasing flag on newly added item (sync with sender)
# Since when a new item is added
# pre-touch has no effect on writer side
if reader_count >= self.n_readers:
self.increment_reader_flag(data_view[: self.flag_bytes])
def close(self) -> None:
"""Close the shared memory."""
@@ -823,6 +820,8 @@ class SingleWriterShmObjectStorage:
"""Default is_free function that checks if the first 4 bytes are zero.
This indicates that the buffer is free.
"""
if id in self._touched:
return False
reader_count = int.from_bytes(buf[0:4], "little", signed=True)
writer_count = self.writer_flag[id]
return reader_count >= writer_count * self.n_readers
+4
View File
@@ -377,6 +377,10 @@ class BaseMultiModalProcessorCache(
"""
raise NotImplementedError
def release_sender_touches(self) -> None:
"""Release the items that were touched but not updated afterwards."""
return None
@abstractmethod
def make_stats(self, *, delta: bool = False) -> CacheInfo:
"""Get (and reset) the multi-modal cache stats.
+6 -4
View File
@@ -170,10 +170,13 @@ class ShmObjectStoreSenderCache(BaseMultiModalProcessorCache):
@override
def touch_sender_cache_item(self, mm_hash: str) -> None:
"""Touch the item in shared memory cache to prevent eviction.
Increments writer_flag on sender side."""
"""Touch the item in shared memory cache to prevent eviction."""
self._shm_cache.touch(mm_hash)
@override
def release_sender_touches(self) -> None:
self._shm_cache.release_touches()
@override
def validate_input_item(
self,
@@ -305,8 +308,7 @@ class ShmObjectStoreReceiverCache(BaseMultiModalReceiverCache):
mm_hash: str,
mm_item: MultiModalKwargsItem | None = None,
) -> None:
"""Touch the item in shared memory cache to prevent eviction.
Increments reader_count on receiver side."""
"""Validate the item's handle in shared memory cache."""
assert mm_item is not None
if (handle := _get_shm_handle(mm_item)) is not None:
address, monotonic_id, signature = handle
+10 -7
View File
@@ -1534,13 +1534,16 @@ class BaseMultiModalProcessor(ABC, Generic[_I]):
)
with timing_ctx.record("merge_mm_kwargs"):
mm_kwargs, mm_prompt_updates = self._merge_mm_kwargs(
cache,
mm_hashes=mm_hashes,
mm_is_cached=mm_is_cached,
mm_missing_kwargs=mm_missing_kwargs,
mm_missing_prompt_updates=mm_missing_prompt_updates,
)
try:
mm_kwargs, mm_prompt_updates = self._merge_mm_kwargs(
cache,
mm_hashes=mm_hashes,
mm_is_cached=mm_is_cached,
mm_missing_kwargs=mm_missing_kwargs,
mm_missing_prompt_updates=mm_missing_prompt_updates,
)
finally:
cache.release_sender_touches()
return MultiModalProcessingResult(
prompt_ids=self._postprocess_prompt(inputs.prompt),