-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[TENT] Fix TaskInfo dangling pointers, data races, stage buffer leaks #3380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RuiqingFeng
wants to merge
2
commits into
kvcache-ai:main
Choose a base branch
from
RuiqingFeng:bugfix/te-tent-staging-dangling-ptr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+293
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,8 @@ Status ProxyManager::deconstruct() { | |
| shards_[i].cv.notify_all(); | ||
| shards_[i].thread.join(); | ||
| } | ||
| for (auto entry : stage_buffers_) { | ||
| std::lock_guard<std::recursive_mutex> lock(stage_buffers_mu_); | ||
| for (auto& entry : stage_buffers_) { | ||
| impl_->unregisterLocalMemory(entry.second.chunks); | ||
| impl_->freeLocalMemory(entry.second.chunks); | ||
| delete[] entry.second.bitmap; | ||
|
|
@@ -155,7 +156,9 @@ Status ProxyManager::submit(TaskInfo* task, BatchID batch, | |
|
|
||
| Status ProxyManager::getStatus(TaskInfo* task, TransferStatus& task_status) { | ||
| if (!task || !task->staging) return Status::InvalidArgument("Invalid task"); | ||
| task_status.s = task->staging_status; | ||
| TransferStatusEnum staging_status; | ||
| __atomic_load(&task->staging_status, &staging_status, __ATOMIC_ACQUIRE); | ||
| task_status.s = staging_status; | ||
| if (task_status.s == COMPLETED) { | ||
| task_status.transferred_bytes = task->request.length; | ||
| } | ||
|
|
@@ -173,7 +176,7 @@ struct StageBufferCache { | |
| uint64_t addr = 0; | ||
| auto status = mgr.pinStageBuffer(location, addr); | ||
| if (!status.ok()) { | ||
| LOG(FATAL) << "Failed to pin local stage buffer: " << status | ||
| LOG(ERROR) << "Failed to pin local stage buffer: " << status | ||
| << ", location " << location; | ||
| return 0; | ||
| } | ||
|
|
@@ -191,7 +194,7 @@ struct StageBufferCache { | |
| auto status = | ||
| ControlClient::pinStageBuffer(server_addr, location, addr); | ||
| if (!status.ok()) { | ||
| LOG(FATAL) << "Failed to pin remote stage buffer: " << status | ||
| LOG(ERROR) << "Failed to pin remote stage buffer: " << status | ||
| << ", location " << location; | ||
| return 0; | ||
| } | ||
|
|
@@ -220,7 +223,6 @@ struct StageBufferCache { | |
| }; | ||
|
|
||
| void ProxyManager::runner(size_t id) { | ||
| StageBufferCache cache(*this); | ||
| auto& shard = shards_[id]; | ||
| while (running_) { | ||
| StagingTask task; | ||
|
|
@@ -239,17 +241,26 @@ void ProxyManager::runner(size_t id) { | |
| } | ||
|
|
||
| if (!task.native) continue; | ||
| auto status = transferEventLoop(task, &cache); | ||
| StageBufferCache cache(*this); | ||
| bool buffers_safe_to_release; | ||
| auto status = transferEventLoop(task, &cache, buffers_safe_to_release); | ||
| if (buffers_safe_to_release) { | ||
| cache.reset(); | ||
| } else { | ||
| LOG(ERROR) << "Staging cleanup could not drain all in-flight " | ||
| "operations; keeping stage buffers pinned"; | ||
| } | ||
|
Comment on lines
+250
to
+252
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to put |
||
| auto staging_status = status.ok() ? COMPLETED : FAILED; | ||
| __atomic_store(&task.native->staging_status, &staging_status, | ||
| __ATOMIC_RELEASE); | ||
| impl_->notifyBatchMaybeReady(task.batch); | ||
| } | ||
| cache.reset(); | ||
| } | ||
|
|
||
| Status ProxyManager::transferEventLoop(StagingTask& task, | ||
| StageBufferCache* cache) { | ||
| StageBufferCache* cache, | ||
| bool& buffers_safe_to_release) { | ||
| buffers_safe_to_release = true; | ||
| auto& request = task.native->request; | ||
| auto server_addr = task.params[0]; | ||
| bool local_staging = !task.params[1].empty(); | ||
|
|
@@ -318,6 +329,44 @@ Status ProxyManager::transferEventLoop(StagingTask& task, | |
|
|
||
| for (size_t i = 0; i < chunks.size(); ++i) event_queue.push(i); | ||
| std::vector<std::future<Status>> remote_futures(chunks.size()); | ||
| auto drain_batch = [&](BatchID batch) { | ||
| while (true) { | ||
| TransferStatus xfer_status; | ||
| auto status = impl_->progressBatch(batch, xfer_status); | ||
| if (!status.ok()) { | ||
| LOG(ERROR) << "Failed to poll in-flight staging batch: " | ||
| << status; | ||
| return false; | ||
| } | ||
| if (xfer_status.s == PENDING) continue; | ||
| auto free_status = impl_->freeBatch(batch); | ||
| if (!free_status.ok()) { | ||
| LOG(WARNING) | ||
| << "Failed to free drained staging batch: " << free_status; | ||
| } | ||
| return true; | ||
| } | ||
| }; | ||
| auto cleanup_inflight = [&]() { | ||
|
Comment on lines
+332
to
+350
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this function run indefinitely? This may block the dedicated workers. |
||
| bool all_drained = true; | ||
| for (auto& chunk : chunks) { | ||
| if (!chunk.batch) continue; | ||
| if (drain_batch(chunk.batch)) { | ||
| chunk.batch = 0; | ||
| } else { | ||
| all_drained = false; | ||
| } | ||
| } | ||
| for (auto& future : remote_futures) { | ||
| if (!future.valid()) continue; | ||
| auto status = future.get(); | ||
| if (!status.ok()) { | ||
| LOG(WARNING) | ||
| << "Failed to drain remote staging request: " << status; | ||
| } | ||
| } | ||
| return all_drained; | ||
| }; | ||
|
|
||
| while (!event_queue.empty()) { | ||
| auto id = event_queue.front(); | ||
|
|
@@ -416,7 +465,11 @@ Status ProxyManager::transferEventLoop(StagingTask& task, | |
|
|
||
| case StageState::INFLIGHT: { | ||
| TransferStatus xfer_status; | ||
| CHECK_STATUS(impl_->progressBatch(chunk.batch, xfer_status)); | ||
| auto status = impl_->progressBatch(chunk.batch, xfer_status); | ||
| if (!status.ok()) { | ||
| buffers_safe_to_release = cleanup_inflight(); | ||
| return status; | ||
| } | ||
| if (xfer_status.s == PENDING) { | ||
| event_queue.push(id); | ||
| break; | ||
|
|
@@ -453,10 +506,7 @@ Status ProxyManager::transferEventLoop(StagingTask& task, | |
| } | ||
|
|
||
| case StageState::FAILED: { | ||
| // Drain the queue to avoid losing chunks | ||
| while (!event_queue.empty()) { | ||
| event_queue.pop(); | ||
| } | ||
| buffers_safe_to_release = cleanup_inflight(); | ||
| return Status::InternalError( | ||
| "Proxy event loop in failed state"); | ||
| } | ||
|
|
@@ -561,6 +611,7 @@ Status ProxyManager::transferSync(StagingTask& task, StageBufferCache* cache) { | |
| } | ||
|
|
||
| Status ProxyManager::allocateStageBuffers(const std::string& location) { | ||
| std::lock_guard<std::recursive_mutex> lock(stage_buffers_mu_); | ||
| if (stage_buffers_.count(location)) return Status::OK(); | ||
| StageBuffers buf; | ||
| auto total_size = chunk_size_ * chunk_count_; | ||
|
|
@@ -575,6 +626,7 @@ Status ProxyManager::allocateStageBuffers(const std::string& location) { | |
| } | ||
|
|
||
| Status ProxyManager::freeStageBuffers(const std::string& location) { | ||
| std::lock_guard<std::recursive_mutex> lock(stage_buffers_mu_); | ||
| auto it = stage_buffers_.find(location); | ||
| if (it == stage_buffers_.end()) | ||
| return Status::InvalidArgument("Stage buffer not allocated" LOC_MARK); | ||
|
|
@@ -587,6 +639,7 @@ Status ProxyManager::freeStageBuffers(const std::string& location) { | |
|
|
||
| Status ProxyManager::pinStageBuffer(const std::string& location, | ||
| uint64_t& addr) { | ||
| std::lock_guard<std::recursive_mutex> lock(stage_buffers_mu_); | ||
| auto it = stage_buffers_.find(location); | ||
| if (it == stage_buffers_.end()) { | ||
| CHECK_STATUS(allocateStageBuffers(location)); | ||
|
|
@@ -605,6 +658,7 @@ Status ProxyManager::pinStageBuffer(const std::string& location, | |
| } | ||
|
|
||
| Status ProxyManager::unpinStageBuffer(uint64_t addr) { | ||
| std::lock_guard<std::recursive_mutex> lock(stage_buffers_mu_); | ||
| for (auto& [location, buf] : stage_buffers_) { | ||
| auto base = reinterpret_cast<uint64_t>(buf.chunks); | ||
| auto end = base + chunk_size_ * chunk_count_; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend to use std::atomic for task->staging_status