Repository: LMCache/LMCache
File: lmcache/v1/multiprocess/mp_storage_manager.py (Python)

Code before:
                shape, dtype, num_objects_to_allocate, fmt
            )

        if objects is not None:
            return handle, _confirm_reserve_objects(keys, ret_mask, objects, handle)

        # Failed to allocate, try to evict once
        # NOTE: we are doing very aggressive eviction here: every time
        # we will try to evict num_objects_to_allocate objects and try
        # to allocate again, until we cannot evict any more objects.
        # NOTE: we cannot directly recycle the allocated objects in
        # multi-process mode, because there could be multiple different
        # models connecting to the same storage manager
        with self._allocator_lock, self._buffer_lock:
            while objects is None:
                candidates = [
                    c
                    for c in self._cache_policy.get_evict_candidates(
                        self._commited_memory_objects,
                        num_objects_to_allocate,
                    )
                    if not self._obj_lock_manager.is_locked(c)
                ]

                # If the candidates are not enough, break
                if len(candidates) == 0:
                    break

                for key in candidates:
                    obj = self._commited_memory_objects.pop(key)
                    obj.ref_count_down()

                logger.info(
                    "Recycled %d committed memory objects to free up space.",
                    len(candidates),
                )

                # Try to allocate again
                objects = self._memory_allocator.batched_allocate(
                    shape, dtype, num_objects_to_allocate, fmt
                )

        if objects is not None:
            return handle, _confirm_reserve_objects(keys, ret_mask, objects, handle)

        raise MemoryExhaustedError(
            f"Memory allocation for {num_objects_to_allocate} objects "
            "failed due to insufficient memory."
        )

    def commit(

Review comment (suggestion):
```suggestion
                if not candidates:
```

Code after:
                shape, dtype, num_objects_to_allocate, fmt
            )

        if objects is not None:
            return handle, _confirm_reserve_objects(keys, ret_mask, objects, handle)

        # Failed to allocate, try to evict once
        # NOTE: we are doing very aggressive eviction here: every time
        # we will try to evict num_objects_to_allocate objects and try
        # to allocate again, until we cannot evict any more objects.
        # NOTE: we cannot directly recycle the allocated objects in
        # multi-process mode, because there could be multiple different
        # models connecting to the same storage manager
        with self._allocator_lock, self._buffer_lock:
            while objects is None:
                candidates = self._cache_policy.get_evict_candidates(
                    self._commited_memory_objects,
                    num_objects_to_allocate,
                )

                # If the candidates are not enough, break
                if not candidates:
                    break

                for key in candidates:
                    obj = self._commited_memory_objects.pop(key)
                    obj.ref_count_down()

                logger.info(
                    "Recycled %d committed memory objects to free up space.",
                    len(candidates),
                )

                # Try to allocate again
                objects = self._memory_allocator.batched_allocate(
                    shape, dtype, num_objects_to_allocate, fmt
                )

        if objects is not None:
            return handle, _confirm_reserve_objects(keys, ret_mask, objects, handle)

        raise MemoryExhaustedError(
            f"Memory allocation for {num_objects_to_allocate} objects "
            "failed due to insufficient memory."
        )

    def commit(
        self,
        reserve_handle: ReserveHandle,
    ) -> None:
        """Mark the reserved memory objects as "ready to be used/retrieved".