Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 1f5631d

Browse files
committed
fix: adding mutex for loaded engine map
1 parent 192c4da commit 1f5631d

File tree

2 files changed

+81
-58
lines changed

2 files changed

+81
-58
lines changed

engine/services/engine_service.cc

Lines changed: 78 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <optional>
55
#include "algorithm"
66
#include "utils/archive_utils.h"
7-
#include "utils/cortex_utils.h"
87
#include "utils/engine_constants.h"
98
#include "utils/engine_matcher_utils.h"
109
#include "utils/file_manager_utils.h"
@@ -631,13 +630,15 @@ EngineService::GetInstalledEngineVariants(const std::string& engine) const {
631630
return variants;
632631
}
633632

634-
bool EngineService::IsEngineLoaded(const std::string& engine) const {
633+
bool EngineService::IsEngineLoaded(const std::string& engine) {
634+
std::lock_guard<std::mutex> lock(engines_mutex_);
635635
auto ne = NormalizeEngine(engine);
636636
return engines_.find(ne) != engines_.end();
637637
}
638638

639639
cpp::result<EngineV, std::string> EngineService::GetLoadedEngine(
640640
const std::string& engine_name) {
641+
std::lock_guard<std::mutex> lock(engines_mutex_);
641642
auto ne = NormalizeEngine(engine_name);
642643
if (engines_.find(ne) == engines_.end()) {
643644
return cpp::fail("Engine " + engine_name + " is not loaded yet!");
@@ -708,19 +709,19 @@ cpp::result<void, std::string> EngineService::LoadEngine(
708709
auto add_dll = [this](const std::string& e_type,
709710
const std::filesystem::path& p) {
710711
if (auto cookie = AddDllDirectory(p.c_str()); cookie != 0) {
711-
CTL_DBG("Added dll directory: " << p);
712+
CTL_DBG("Added dll directory: " << p.string());
712713
engines_[e_type].cookie = cookie;
713714
} else {
714-
CTL_WRN("Could not add dll directory: " << p);
715+
CTL_WRN("Could not add dll directory: " << p.string());
715716
}
716717

717718
auto cuda_path = file_manager_utils::GetCudaToolkitPath(e_type);
718719
if (auto cuda_cookie = AddDllDirectory(cuda_path.c_str());
719720
cuda_cookie != 0) {
720-
CTL_DBG("Added cuda dll directory: " << p);
721+
CTL_DBG("Added cuda dll directory: " << p.string());
721722
engines_[e_type].cuda_cookie = cuda_cookie;
722723
} else {
723-
CTL_WRN("Could not add cuda dll directory: " << p);
724+
CTL_WRN("Could not add cuda dll directory: " << p.string());
724725
}
725726
};
726727

@@ -732,16 +733,20 @@ cpp::result<void, std::string> EngineService::LoadEngine(
732733
should_use_dll_search_path) {
733734
if (IsEngineLoaded(kLlamaRepo) && ne == kTrtLlmRepo &&
734735
should_use_dll_search_path) {
735-
// Remove llamacpp dll directory
736-
if (!RemoveDllDirectory(engines_[kLlamaRepo].cookie)) {
737-
CTL_WRN("Could not remove dll directory: " << kLlamaRepo);
738-
} else {
739-
CTL_DBG("Removed dll directory: " << kLlamaRepo);
740-
}
741-
if (!RemoveDllDirectory(engines_[kLlamaRepo].cuda_cookie)) {
742-
CTL_WRN("Could not remove cuda dll directory: " << kLlamaRepo);
743-
} else {
744-
CTL_DBG("Removed cuda dll directory: " << kLlamaRepo);
736+
737+
{
738+
std::lock_guard<std::mutex> lock(engines_mutex_);
739+
// Remove llamacpp dll directory
740+
if (!RemoveDllDirectory(engines_[kLlamaRepo].cookie)) {
741+
CTL_WRN("Could not remove dll directory: " << kLlamaRepo);
742+
} else {
743+
CTL_DBG("Removed dll directory: " << kLlamaRepo);
744+
}
745+
if (!RemoveDllDirectory(engines_[kLlamaRepo].cuda_cookie)) {
746+
CTL_WRN("Could not remove cuda dll directory: " << kLlamaRepo);
747+
} else {
748+
CTL_DBG("Removed cuda dll directory: " << kLlamaRepo);
749+
}
745750
}
746751

747752
add_dll(ne, engine_dir_path);
@@ -752,8 +757,11 @@ cpp::result<void, std::string> EngineService::LoadEngine(
752757
}
753758
}
754759
#endif
755-
engines_[ne].dl =
756-
std::make_unique<cortex_cpp::dylib>(engine_dir_path.string(), "engine");
760+
{
761+
std::lock_guard<std::mutex> lock(engines_mutex_);
762+
engines_[ne].dl = std::make_unique<cortex_cpp::dylib>(
763+
engine_dir_path.string(), "engine");
764+
}
757765
#if defined(__linux__)
758766
const char* name = "LD_LIBRARY_PATH";
759767
auto data = getenv(name);
@@ -774,65 +782,78 @@ cpp::result<void, std::string> EngineService::LoadEngine(
774782

775783
} catch (const cortex_cpp::dylib::load_error& e) {
776784
CTL_ERR("Could not load engine: " << e.what());
777-
engines_.erase(ne);
785+
{
786+
std::lock_guard<std::mutex> lock(engines_mutex_);
787+
engines_.erase(ne);
788+
}
778789
return cpp::fail("Could not load engine " + ne + ": " + e.what());
779790
}
780791

781-
auto func = engines_[ne].dl->get_function<EngineI*()>("get_engine");
782-
engines_[ne].engine = func();
792+
{
793+
std::lock_guard<std::mutex> lock(engines_mutex_);
794+
auto func = engines_[ne].dl->get_function<EngineI*()>("get_engine");
795+
engines_[ne].engine = func();
783796

784-
auto& en = std::get<EngineI*>(engines_[ne].engine);
785-
if (ne == kLlamaRepo) { //fix for llamacpp engine first
786-
auto config = file_manager_utils::GetCortexConfig();
787-
if (en->IsSupported("SetFileLogger")) {
788-
en->SetFileLogger(config.maxLogLines,
789-
(std::filesystem::path(config.logFolderPath) /
790-
std::filesystem::path(config.logLlamaCppPath))
791-
.string());
792-
} else {
793-
CTL_WRN("Method SetFileLogger is not supported yet");
794-
}
795-
if (en->IsSupported("SetLogLevel")) {
796-
en->SetLogLevel(logging_utils_helper::global_log_level);
797-
} else {
798-
CTL_WRN("Method SetLogLevel is not supported yet");
797+
auto& en = std::get<EngineI*>(engines_[ne].engine);
798+
if (ne == kLlamaRepo) { //fix for llamacpp engine first
799+
auto config = file_manager_utils::GetCortexConfig();
800+
if (en->IsSupported("SetFileLogger")) {
801+
en->SetFileLogger(config.maxLogLines,
802+
(std::filesystem::path(config.logFolderPath) /
803+
std::filesystem::path(config.logLlamaCppPath))
804+
.string());
805+
} else {
806+
CTL_WRN("Method SetFileLogger is not supported yet");
807+
}
808+
if (en->IsSupported("SetLogLevel")) {
809+
en->SetLogLevel(logging_utils_helper::global_log_level);
810+
} else {
811+
CTL_WRN("Method SetLogLevel is not supported yet");
812+
}
799813
}
814+
CTL_DBG("loaded engine: " << ne);
800815
}
801-
CTL_DBG("Loaded engine: " << ne);
802816
return {};
803817
}
804818

805819
cpp::result<void, std::string> EngineService::UnloadEngine(
806820
const std::string& engine) {
807821
auto ne = NormalizeEngine(engine);
808-
if (!IsEngineLoaded(ne)) {
809-
return cpp::fail("Engine " + ne + " is not loaded yet!");
810-
}
811-
EngineI* e = std::get<EngineI*>(engines_[ne].engine);
812-
delete e;
822+
{
823+
std::lock_guard<std::mutex> lock(engines_mutex_);
824+
if (!IsEngineLoaded(ne)) {
825+
return cpp::fail("Engine " + ne + " is not loaded yet!");
826+
}
827+
EngineI* e = std::get<EngineI*>(engines_[ne].engine);
828+
delete e;
829+
813830
#if defined(_WIN32)
814-
if (!RemoveDllDirectory(engines_[ne].cookie)) {
815-
CTL_WRN("Could not remove dll directory: " << ne);
816-
} else {
817-
CTL_DBG("Removed dll directory: " << ne);
818-
}
819-
if (!RemoveDllDirectory(engines_[ne].cuda_cookie)) {
820-
CTL_WRN("Could not remove cuda dll directory: " << ne);
821-
} else {
822-
CTL_DBG("Removed cuda dll directory: " << ne);
823-
}
831+
if (!RemoveDllDirectory(engines_[ne].cookie)) {
832+
CTL_WRN("Could not remove dll directory: " << ne);
833+
} else {
834+
CTL_DBG("Removed dll directory: " << ne);
835+
}
836+
if (!RemoveDllDirectory(engines_[ne].cuda_cookie)) {
837+
CTL_WRN("Could not remove cuda dll directory: " << ne);
838+
} else {
839+
CTL_DBG("Removed cuda dll directory: " << ne);
840+
}
824841
#endif
825-
engines_.erase(ne);
842+
engines_.erase(ne);
843+
}
826844
CTL_DBG("Unloaded engine " + ne);
827845
return {};
828846
}
829847

830848
std::vector<EngineV> EngineService::GetLoadedEngines() {
831-
std::vector<EngineV> loaded_engines;
832-
for (const auto& [key, value] : engines_) {
833-
loaded_engines.push_back(value.engine);
849+
{
850+
std::lock_guard<std::mutex> lock(engines_mutex_);
851+
std::vector<EngineV> loaded_engines;
852+
for (const auto& [key, value] : engines_) {
853+
loaded_engines.push_back(value.engine);
854+
}
855+
return loaded_engines;
834856
}
835-
return loaded_engines;
836857
}
837858

838859
cpp::result<github_release_utils::GitHubRelease, std::string>

engine/services/engine_service.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <memory>
4+
#include <mutex>
45
#include <string>
56
#include <string_view>
67
#include <vector>
@@ -51,6 +52,7 @@ class EngineService : public EngineServiceI {
5152
#endif
5253
};
5354

55+
std::mutex engines_mutex_;
5456
std::unordered_map<std::string, EngineInfo> engines_{};
5557

5658
public:
@@ -99,7 +101,7 @@ class EngineService : public EngineServiceI {
99101
cpp::result<std::vector<EngineVariantResponse>, std::string>
100102
GetInstalledEngineVariants(const std::string& engine) const;
101103

102-
bool IsEngineLoaded(const std::string& engine) const;
104+
bool IsEngineLoaded(const std::string& engine);
103105

104106
cpp::result<EngineV, std::string> GetLoadedEngine(
105107
const std::string& engine_name);

0 commit comments

Comments
 (0)