From e28c61209756a02c07634b589b869ff297d808a0 Mon Sep 17 00:00:00 2001 From: codex Date: Fri, 21 Aug 2026 18:34:16 +0800 Subject: [PATCH 1/2] [TENT] Preserve ownership after failed memory free --- .../tent/src/runtime/transfer_engine_impl.cpp | 3 +- .../tent/tests/CMakeLists.txt | 12 +++ .../tests/local_memory_lifecycle_test.cpp | 95 +++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 mooncake-transfer-engine/tent/tests/local_memory_lifecycle_test.cpp diff --git a/mooncake-transfer-engine/tent/src/runtime/transfer_engine_impl.cpp b/mooncake-transfer-engine/tent/src/runtime/transfer_engine_impl.cpp index 22a1167ef46..244bec99fa4 100644 --- a/mooncake-transfer-engine/tent/src/runtime/transfer_engine_impl.cpp +++ b/mooncake-transfer-engine/tent/src/runtime/transfer_engine_impl.cpp @@ -690,8 +690,9 @@ Status TransferEngineImpl::freeLocalMemory(void* addr) { ++it) { if (it->addr == addr) { auto status = it->transport->freeLocalMemory(addr, it->size); + if (!status.ok()) return status; allocated_memory_.erase(it); - return status; + return Status::OK(); } } return Status::InvalidArgument("Address region not registered" LOC_MARK); diff --git a/mooncake-transfer-engine/tent/tests/CMakeLists.txt b/mooncake-transfer-engine/tent/tests/CMakeLists.txt index 3a30328a4e3..bd434fb68a5 100644 --- a/mooncake-transfer-engine/tent/tests/CMakeLists.txt +++ b/mooncake-transfer-engine/tent/tests/CMakeLists.txt @@ -428,6 +428,18 @@ target_include_directories(tent_runtime_queue_dispatch_test add_test(NAME tent_runtime_queue_dispatch_test COMMAND tent_runtime_queue_dispatch_test) +add_executable(tent_local_memory_lifecycle_test + local_memory_lifecycle_test.cpp) +target_link_libraries(tent_local_memory_lifecycle_test + PRIVATE gtest gtest_main tent_link_group) +if(TARGET asio_shared) + target_link_libraries(tent_local_memory_lifecycle_test PRIVATE asio_shared) +endif() +target_include_directories(tent_local_memory_lifecycle_test + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include) +add_test(NAME tent_local_memory_lifecycle_test + COMMAND tent_local_memory_lifecycle_test) + # Causal chain stage decomposition: validates that dispatch_time and post_time # timestamps are populated on both queue and direct-commit paths. add_executable(causal_chain_test causal_chain_test.cpp) diff --git a/mooncake-transfer-engine/tent/tests/local_memory_lifecycle_test.cpp b/mooncake-transfer-engine/tent/tests/local_memory_lifecycle_test.cpp new file mode 100644 index 00000000000..4a3859f64d8 --- /dev/null +++ b/mooncake-transfer-engine/tent/tests/local_memory_lifecycle_test.cpp @@ -0,0 +1,95 @@ +// Copyright 2026 KVCache.AI +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include "tent/common/config.h" +#include "tent/runtime/transfer_engine_impl.h" +#include "tent/runtime/transport.h" + +namespace mooncake { +namespace tent { +namespace { + +class FailOnceFreeTransport : public Transport { + public: + Status allocateLocalMemory(void** addr, size_t size, + MemoryOptions&) override { + *addr = std::malloc(size); + return *addr ? Status::OK() + : Status::InternalError("malloc failed" LOC_MARK); + } + + Status freeLocalMemory(void* addr, size_t) override { + ++free_calls; + if (free_calls == 1) { + return Status::InternalError("injected free failure" LOC_MARK); + } + std::free(addr); + return Status::OK(); + } + + const char* getName() const override { return "fail-once-free"; } + + int free_calls{0}; +}; + +std::shared_ptr makeConfig() { + auto config = std::make_shared(); + config->set("metadata_type", "p2p"); + config->set("metadata_servers", ""); + config->set("rpc_server_hostname", "127.0.0.1"); + config->set("rpc_server_port", "0"); + config->set("log_level", "warning"); + + for (const char* transport : {"tcp", "shm", "rdma", "io_uring", "nvlink", + "mnnvl", "gds", "ascend_direct"}) { + config->set(std::string("transports/") + transport + "/enable", false); + } + return config; +} + +TEST(LocalMemoryLifecycle, RetainsOwnershipWhenTransportFreeFails) { + TransferEngineImpl engine(makeConfig()); + ASSERT_TRUE(engine.available()); + + auto transport = std::make_shared(); + engine.swapTransportForTest(RDMA, transport); + + MemoryOptions options; + options.type = RDMA; + options.location = "cpu:0"; + void* addr = nullptr; + ASSERT_TRUE(engine.allocateLocalMemory(&addr, 4096, options).ok()); + + auto first = engine.freeLocalMemory(addr); + EXPECT_FALSE(first.ok()); + EXPECT_EQ(transport->free_calls, 1); + + auto second = engine.freeLocalMemory(addr); + EXPECT_TRUE(second.ok()) << second.ToString(); + EXPECT_EQ(transport->free_calls, 2); + + auto third = engine.freeLocalMemory(addr); + EXPECT_TRUE(third.IsInvalidArgument()); + EXPECT_EQ(transport->free_calls, 2); +} + +} // namespace +} // namespace tent +} // namespace mooncake From ce87244dfb62d78a9ce8abc587ab51e627942692 Mon Sep 17 00:00:00 2001 From: codex Date: Tue, 25 Aug 2026 15:58:57 +0800 Subject: [PATCH 2/2] fix(tent): warn when local memory free fails --- .../tent/src/runtime/transfer_engine_impl.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mooncake-transfer-engine/tent/src/runtime/transfer_engine_impl.cpp b/mooncake-transfer-engine/tent/src/runtime/transfer_engine_impl.cpp index 244bec99fa4..02489f54e93 100644 --- a/mooncake-transfer-engine/tent/src/runtime/transfer_engine_impl.cpp +++ b/mooncake-transfer-engine/tent/src/runtime/transfer_engine_impl.cpp @@ -690,7 +690,12 @@ Status TransferEngineImpl::freeLocalMemory(void* addr) { ++it) { if (it->addr == addr) { auto status = it->transport->freeLocalMemory(addr, it->size); - if (!status.ok()) return status; + if (!status.ok()) { + LOG(WARNING) + << "Failed to free local memory, addr=" << addr + << ", size=" << it->size << ": " << status.ToString(); + return status; + } allocated_memory_.erase(it); return Status::OK(); }