From ccdcccd44c1a6b94ac99e6b63f2e1efa32137cda Mon Sep 17 00:00:00 2001 From: LiuBingrun Date: Sat, 11 May 2024 10:29:40 +0800 Subject: [PATCH] Fix DeadLock in FdManager::ChangeEntityToTempPath (#2455) commit e3b50ad introduce smart pointer to manage FdEntity But in ChangeEntityToTempPath, we should not destroy the entity. We should move the entry to the temp ky Signed-off-by: liubingrun --- src/fdcache.cpp | 8 +++++--- src/fdcache.h | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/fdcache.cpp b/src/fdcache.cpp index 2d4c4f8..1fc2ac4 100644 --- a/src/fdcache.cpp +++ b/src/fdcache.cpp @@ -775,7 +775,7 @@ bool FdManager::Close(FdEntity* ent, int fd) return false; } -bool FdManager::ChangeEntityToTempPath(FdEntity* ent, const char* path) +bool FdManager::ChangeEntityToTempPath(const FdEntity* ent, const char* path) { AutoLock auto_lock(&FdManager::fd_manager_lock); @@ -783,8 +783,10 @@ bool FdManager::ChangeEntityToTempPath(FdEntity* ent, const char* path) if(iter->second.get() == ent){ std::string tmppath; FdManager::MakeRandomTempPath(path, tmppath); - iter->second.reset(ent); - break; + // Move the entry to the new key + fent[tmppath] = std::move(iter->second); + iter = fent.erase(iter); + return true; }else{ ++iter; } diff --git a/src/fdcache.h b/src/fdcache.h index b09e508..e294290 100644 --- a/src/fdcache.h +++ b/src/fdcache.h @@ -96,7 +96,7 @@ class FdManager FdEntity* OpenExistFdEntity(const char* path, int& fd, int flags = O_RDONLY); void Rename(const std::string &from, const std::string &to); bool Close(FdEntity* ent, int fd); - bool ChangeEntityToTempPath(FdEntity* ent, const char* path); + bool ChangeEntityToTempPath(const FdEntity* ent, const char* path); void CleanupCacheDir(); bool CheckAllCache();