Merge "libsnapshot:snapuserd: Fix memory leak"

This commit is contained in:
Akilesh Kailash 2021-04-02 05:13:11 +00:00 committed by Gerrit Code Review
commit ef8d3c39b5
3 changed files with 17 additions and 2 deletions

View File

@ -158,6 +158,7 @@ class Snapuserd : public std::enable_shared_from_this<Snapuserd> {
bool CommitMerge(int num_merge_ops);
void CloseFds() { cow_fd_ = {}; }
void FreeResources() { worker_threads_.clear(); }
size_t GetMetadataAreaSize() { return vec_.size(); }
void* GetExceptionBuffer(size_t i) { return vec_[i].get(); }

View File

@ -219,7 +219,13 @@ void SnapuserdServer::RunThread(std::shared_ptr<DmUserHandler> handler) {
auto iter = FindHandler(&lock, handler->misc_name());
if (iter == dm_users_.end()) {
// RemoveAndJoinHandler() already removed us from the list, and is
// now waiting on a join(), so just return.
// now waiting on a join(), so just return. Additionally, release
// all the resources held by snapuserd object which are shared
// by worker threads. This should be done when the last reference
// of "handler" is released; but we will explicitly release here
// to make sure snapuserd object is freed as it is the biggest
// consumer of memory in the daemon.
handler->FreeResources();
LOG(INFO) << "Exiting handler thread to allow for join: " << misc_name;
return;
}

View File

@ -49,7 +49,15 @@ class DmUserHandler {
public:
explicit DmUserHandler(std::shared_ptr<Snapuserd> snapuserd);
void FreeResources() { snapuserd_ = nullptr; }
void FreeResources() {
// Each worker thread holds a reference to snapuserd.
// Clear them so that all the resources
// held by snapuserd is released
if (snapuserd_) {
snapuserd_->FreeResources();
snapuserd_ = nullptr;
}
}
const std::shared_ptr<Snapuserd>& snapuserd() const { return snapuserd_; }
std::thread& thread() { return thread_; }