Repository: milvus-io/milvus
File: internal/core/src/segcore/memory_planner.cpp (C++)

Code before:
        LOG_INFO("[StorageV2] split row groups into blocks: {} for file {}",
                 blocks.size(),
                 file);

        auto reader_memory_limit = std::max<int64_t>(
            memory_limit / blocks.size(), FILE_SLICE_SIZE.load());

        for (const auto& block : blocks) {
            futures.emplace_back(pool.Submit([block,
                                              fs,
                                              file,
                                              file_idx,
                                              schema,
                                              reader_memory_limit,
                                              channel,
                                              remaining_tasks,
                                              op_ctx]() {
                // Use a guard to ensure we always decrement the counter
                // and close the channel when the last task completes,
                // even if an exception is thrown.
                auto task_guard =
                    folly::makeGuard([&channel, &remaining_tasks]() {
                        if (remaining_tasks->fetch_sub(1) == 1) {
                            // This is the last task, close the channel
                            channel->close();
                        }
                    });

                AssertInfo(fs != nullptr, "[StorageV2] file system is nullptr");
                CheckCancellation(op_ctx, -1, "LoadWithStrategyAsync");
                auto result = milvus_storage::FileRowGroupReader::Make(
                    fs,
                    file,
                    schema,
                    reader_memory_limit,
                    milvus::storage::GetReaderProperties());
                AssertInfo(result.ok(),
                           "[StorageV2] Failed to create row group reader: " +
                               result.status().ToString());
                auto row_group_reader = result.ValueOrDie();
                auto status = row_group_reader->SetRowGroupOffsetAndCount(
                    block.offset, block.count);
                AssertInfo(status.ok(),
                           "[StorageV2] Failed to set row group offset "
                           "and count " +
                               std::to_string(block.offset) + " and " +
                               std::to_string(block.count) + " with error " +
                               status.ToString());
                auto ret = std::make_shared<ArrowDataWrapper>();
                for (int64_t i = 0; i < block.count; ++i) {
                    std::shared_ptr<arrow::Table> table;

Review comment (suggestion):
make sure the remote_files reference are always valid and will not be used after free within the thread pool.

Code after:
        }
        current.rg_count += spec.rg_count;
        current.cells.push_back(spec);
    }
    if (!current.cells.empty()) {
        batches.push_back(std::move(current));
    }

    if (batches.empty()) {
        channel->close();
        return {};
    }

    auto& pool = ThreadPools::GetThreadPool(milvus::PriorityForLoad(priority));
    auto remaining = std::make_shared<std::atomic<size_t>>(batches.size());
    auto reader_memory_limit =
        std::max<int64_t>(memory_limit / static_cast<int64_t>(batches.size()),
                          FILE_SLICE_SIZE.load());

    std::vector<std::future<void>> futures;
    futures.reserve(batches.size());

    for (auto& batch : batches) {
        futures.emplace_back(pool.Submit([batch = std::move(batch),
                                          fs,
                                          &remote_files,
                                          reader_memory_limit,
                                          channel,
                                          remaining,
                                          op_ctx]() {
            auto task_guard = folly::makeGuard([&channel, &remaining]() {
                if (remaining->fetch_sub(1) == 1) {
                    channel->close();
                }
            });
            CheckCancellation(op_ctx, -1, "LoadCellBatchAsync");

            auto result = milvus_storage::FileRowGroupReader::Make(
                fs,
                remote_files[batch.file_idx],
                nullptr,
                reader_memory_limit,
                milvus::storage::GetReaderProperties());
            AssertInfo(result.ok(),
                       "[StorageV2] Failed to create reader: " +
                           result.status().ToString());
            auto reader = result.ValueOrDie();
            auto status = reader->SetRowGroupOffsetAndCount(batch.rg_offset,
                                                            batch.rg_count);
            AssertInfo(status.ok(),
                       "[StorageV2] Failed to set row group range: " +