Repository navigation
[Store][DO NOT Merge Now](fix) avoid MR resource leak and destructor hang on master failure - #2336
LujhCoconut wants to merge 1 commit into
Conversation
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request modifies the client service to prevent destructor hangs and memory registration leaks by performing local cleanup instead of synchronous RPCs to the master during client destruction, proceeding with local cleanup if master unmounting fails, and rolling back memory registration upon mount failures. The review feedback correctly points out that calling unregisterLocalMemory with a single argument still triggers a synchronous RPC to the master by default, and suggests passing false as the second argument to ensure purely local cleanup.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
Code Review
This pull request updates the client segment cleanup and error handling in mooncake-store/src/client_service.cpp. It skips master RPCs during client destruction to prevent hangs, ensures local memory unregistration proceeds even if master unmount notifications fail, and adds rollback logic for local memory registration when mounting to the master fails. The review feedback identifies critical improvements: resolving a potential deadlock during destruction by releasing mounted_segments_mutex_ before making synchronous RPCs, adding a defensive null check for transfer_engine_ to prevent dereferencing errors in RPC-only mode, and propagating master unmount errors to the caller instead of swallowing them.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
ba1ec38 to
7c0dc7c
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request updates the client service and transfer engine to avoid hangs and deadlocks during segment mounting and unmounting, introducing an update_metadata flag to control metadata synchronization. The review highlights several critical issues: releasing the mounted_segments_mutex_ lock before the synchronous master RPC introduces a race condition and potential use-after-free during destruction; hardcoding false for update_metadata during unmount prevents metadata synchronization even when the master is healthy; and missing updates to the vector overloads of unregisterLocalMemory in header files will cause compilation errors.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
7c0dc7c to
dfd0482
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request optimizes client cleanup and segment mounting by performing local transfer engine cleanup during destruction (skipping master RPCs to avoid hangs), continuing local cleanup on unmount failures, and moving the synchronous MountSegment RPC outside of the mutex lock. It also introduces an update_metadata flag to unregisterLocalMemory to bypass metadata synchronization when appropriate. The review feedback identifies critical issues, including potential null pointer dereferences on transfer_engine_ in UnmountSegmentImpl and MountSegmentAndGetId, and a race condition in MountSegmentAndGetId where concurrent mounts could bypass overlap checks because segments are registered in mounted_segments_ only after the RPC completes.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
dfd0482 to
17a699e
Compare
|
CI check seems abnormal. |
| int rc = 0; | ||
| if (transfer_engine_) { | ||
| rc = transfer_engine_->unregisterLocalMemory( | ||
| reinterpret_cast<void*>(it->second.base), false); | ||
| } |
There was a problem hiding this comment.
The old code called unregisterLocalMemory(addr) which defaulted to update_metadata=true, triggering metadata_->segmentManager().synchronizeLocal(). Now all unmount paths (including the happy path where master is alive) skip metadata sync.
This means even a successful unmount no longer publishes the MR removal to other peers via the metadata service. Remote nodes may still attempt RDMA reads/writes to the now-deregistered MR, causing silent failures or crashes.
There was a problem hiding this comment.
Thanks for catching this! You're right that was a bug.
The latest push fixes it — UnmountSegmentImpl now passes unmount_result.has_value() instead of hardcoded false:
rc = transfer_engine_->unregisterLocalMemory(
reinterpret_cast<void*>(it->second.base),
unmount_result.has_value()); // true when master alive, false when dead So the master-alive path still calls synchronizeLocal() to notify peers, while the failure path (master dead) skips it since there's no point syncing metadata when the master is gone — it will self-cleanup via TTL expiry.
|
|
||
| segment_id = segment.id; | ||
| mounted_segments_[segment_id] = segment; | ||
| auto mount_result = master_client_.MountSegment(segment); |
There was a problem hiding this comment.
Here doing RPC without lock. Could a race condition occur?
There was a problem hiding this comment.
Fixed, thanks for flagging it. The current version keeps the entire operation under mounted_segments_mutex_ — the master RPC, rollback, and segment insertion all happen inside the lock scope.
17a699e to
4073475
Compare
staryxchen
left a comment
There was a problem hiding this comment.
LGTM. Only a mirror issue not blocking: (void*)buffer -> reinterpret_cast<void*>(buffer)
3729958 to
ed59478
Compare
- Skip synchronous master Unmount RPC in Client destructor; master cleans up via ClientMonitorFunc TTL. Prevents hang if master is frozen and MR leak if master is dead. - Continue local TE unregister in UnmountSegmentImpl even when master notification fails. - Rollback TE registration in MountSegmentAndGetId when mount to master fails, preventing orphaned MRs.
ed59478 to
afc097a
Compare
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
| if (!mount_result) { | ||
| ErrorCode err = mount_result.error(); | ||
| LOG(ERROR) << "mount_segment_to_master_failed base=" << buffer | ||
| << " size=" << size << ", error=" << err; | ||
| // Rollback: unregister the TE memory that was just registered | ||
| int unreg_rc = transfer_engine_->unregisterLocalMemory( |
There was a problem hiding this comment.
I'm worried the current handling here isn't robust enough — it runs into the same issue @ykwd raised in PR #2345.
The issue of "RDMA memory leak on MountSegment failure" does indeed exist and is a known problem. The new rollback at client_service.cpp:2726 unregisters the MR whenever MountSegment returns an error:
auto mount_result = master_client_.MountSegment(segment);
if (!mount_result) {
transfer_engine_->unregisterLocalMemory(buffer, false); // unsafe
return tl::unexpected(err);
}
The problem is that !mount_result is ambiguous. invoke_rpc collapses three different outcomes into the same RPC_FAIL:
- request never sent → master didn't mount → rollback safe
- request sent but response lost (timeout / reset / master crashed after commit) → master may have mounted successfully → rollback unsafe
- master rejected → rollback safe
In case (2) the master still considers the segment valid and will route peers to it, but we've just deregistered the MR locally — turning a memory leak into a cross-node inconsistency where peers RDMA to a dead rkey. This is exactly the concern that led to the cleanup being removed in PR #2345 and tracked in Issue #2364.
It's also made worse by update_metadata=false: the descriptor stays published in the TE metadata server even though the MR is gone, so the stale rkey remains discoverable.
There was a problem hiding this comment.
Agreed. But I haven't come up with a particularly elegant approach yet.
|
This PR has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Please update the PR or comment to keep it open. |
|
This PR was closed because it has been inactive for 104 days. Feel free to reopen if you'd like to continue the work. |
Description
This PR fixes RDMA Memory Region (MR) resource leaks in
mooncake-storethat occur when themaster serviceexits or becomes unreachable before thestore servicegracefully unmounts its segments.Root Cause (source code verified)
Three independent code paths leak MRs:
Client::~Client()performs synchronous RPC to master during destruction.When master is dead, each
UnmountSegment()call hitsUnmountSegmentImpl(), which returns early on RPC failure without callingtransfer_engine_->unregisterLocalMemory(). All mounted segments' MRs are leaked.client_service.cpp:355-363(destructor loop)client_service.cpp:2598-2603(early return on RPC failure)UnmountSegmentImplbails out on RPC failure without local cleanup.If
master_client_.UnmountSegment()fails, the function returns immediately, skippingtransfer_engine_->unregisterLocalMemory().client_service.cpp:2598-2603Mount path lacks rollback on RPC failure.
In
MountSegmentAndGetId,registerLocalMemory()succeeds first, but if the subsequentMountSegment()RPC fails, the TE-side MR is never unregistered.client_service.cpp:2670-2696Why RPC is skippable in destructor
MasterService::ClientMonitorFunc(master_service.cpp:5658) handles dead clients via TTL expiry:Pingheartbeatsclient_live_ttl_sec_, master marks client as expiredClientMonitorFuncauto-runsPrepareUnmountSegment → ClearInvalidHandles → CommitUnmountSegmentTherefore, the destructor's
UnmountSegmentRPC is best-effort optimization, not a correctness dependency.What this PR fixes
Client::~Client()unregisterLocalMemory()directlyUnmountSegmentImplskips TE cleanup on RPC failureClient::UnmountSegmentImpl()unregisterLocalMemory()even if master notification failsClient::MountSegmentAndGetId()registerLocalMemory()ifMountSegment()failsChanges
Client::~Client(): Skip all master RPCs during destruction. Perform only localunregisterLocalMemory()cleanup. Master will clean up metadata via TTL.Client::UnmountSegmentImpl(): Continue withunregisterLocalMemory()even if master notification fails. Log the failure but do not leak the MR.Client::MountSegmentAndGetId(): RollbackregisterLocalMemory()ifMountSegment()RPC fails.Module
mooncake-store)mooncake-transfer-engine)mooncake-ep)mooncake-integration)mooncake-p2p-store)mooncake-wheel)mooncake-pg)mooncake-rl)Type of Change
Checklist
./scripts/code_format.shbefore submitting.