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

Commit 027002f

Browse files
fix: cortexrc race condition (#1707)
Co-authored-by: vansangpfiev <sang@jan.ai>
1 parent e57f80c commit 027002f

File tree

8 files changed

+228
-186
lines changed

8 files changed

+228
-186
lines changed

engine/cli/command_line_parser.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,9 @@ void CommandLineParser::SetupSystemCommands() {
602602
<< " to " << cml_data_.port);
603603
auto config_path = file_manager_utils::GetConfigurationPath();
604604
cml_data_.config.apiServerPort = std::to_string(cml_data_.port);
605-
auto result = config_yaml_utils::DumpYamlConfig(cml_data_.config,
606-
config_path.string());
605+
auto result =
606+
config_yaml_utils::CortexConfigMgr::GetInstance().DumpYamlConfig(
607+
cml_data_.config, config_path.string());
607608
if (result.has_error()) {
608609
CLI_LOG("Error update " << config_path.string() << result.error());
609610
}

engine/cli/commands/cortex_upd_cmd.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ std::optional<std::string> CheckNewUpdate(
192192
CTL_INF("Got the latest release, update to the config file: "
193193
<< latest_version)
194194
config.latestRelease = latest_version;
195-
auto result = config_yaml_utils::DumpYamlConfig(
196-
config, file_manager_utils::GetConfigurationPath().string());
195+
auto result =
196+
config_yaml_utils::CortexConfigMgr::GetInstance().DumpYamlConfig(
197+
config, file_manager_utils::GetConfigurationPath().string());
197198
if (result.has_error()) {
198199
CTL_ERR("Error update "
199200
<< file_manager_utils::GetConfigurationPath().string()

engine/cli/main.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ int main(int argc, char* argv[]) {
151151
.count();
152152
config.latestLlamacppRelease = res.value();
153153

154-
auto upd_config_res = config_yaml_utils::DumpYamlConfig(
155-
config, file_manager_utils::GetConfigurationPath().string());
154+
auto upd_config_res =
155+
config_yaml_utils::CortexConfigMgr::GetInstance().DumpYamlConfig(
156+
config, file_manager_utils::GetConfigurationPath().string());
156157
if (upd_config_res.has_error()) {
157158
CTL_ERR("Failed to update config file: " << upd_config_res.error());
158159
} else {

engine/main.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ void RunServer(std::optional<int> port, bool ignore_cout) {
5151
auto config_path = file_manager_utils::GetConfigurationPath();
5252
config.apiServerPort = std::to_string(*port);
5353
auto result =
54-
config_yaml_utils::DumpYamlConfig(config, config_path.string());
54+
config_yaml_utils::CortexConfigMgr::GetInstance().DumpYamlConfig(
55+
config, config_path.string());
5556
if (result.has_error()) {
5657
CTL_ERR("Error update " << config_path.string() << result.error());
5758
}

engine/test/components/test_cortex_config.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "utils/config_yaml_utils.h"
33

44
namespace config_yaml_utils {
5+
namespace cyu = config_yaml_utils;
56
class CortexConfigTest : public ::testing::Test {
67
protected:
78
const std::string test_file_path = "test_config.yaml";
@@ -43,7 +44,8 @@ TEST_F(CortexConfigTest, DumpYamlConfig_WritesCorrectly) {
4344
123456789,
4445
"v1.0.0"};
4546

46-
auto result = DumpYamlConfig(config, test_file_path);
47+
auto result = cyu::CortexConfigMgr::GetInstance().DumpYamlConfig(
48+
config, test_file_path);
4749
EXPECT_FALSE(result.has_error());
4850

4951
// Verify that the file was created and contains the expected data
@@ -72,11 +74,13 @@ TEST_F(CortexConfigTest, FromYaml_ReadsCorrectly) {
7274
123456789,
7375
"v1.0.0"};
7476

75-
auto result = DumpYamlConfig(config, test_file_path);
77+
auto result = cyu::CortexConfigMgr::GetInstance().DumpYamlConfig(
78+
config, test_file_path);
7679
EXPECT_FALSE(result.has_error());
7780

7881
// Now read from the YAML file
79-
CortexConfig loaded_config = FromYaml(test_file_path, default_config);
82+
CortexConfig loaded_config = cyu::CortexConfigMgr::GetInstance().FromYaml(
83+
test_file_path, default_config);
8084

8185
// Verify that the loaded configuration matches what was written
8286
EXPECT_EQ(loaded_config.logFolderPath, config.logFolderPath);
@@ -92,7 +96,10 @@ TEST_F(CortexConfigTest, FromYaml_FileNotFound) {
9296
std::filesystem::remove(test_file_path); // Ensure the file does not exist
9397

9498
EXPECT_THROW(
95-
{ FromYaml(test_file_path, default_config); },
99+
{
100+
cyu::CortexConfigMgr::GetInstance().FromYaml(test_file_path,
101+
default_config);
102+
},
96103
std::runtime_error); // Expect a runtime error due to missing file
97104
}
98105

@@ -102,7 +109,8 @@ TEST_F(CortexConfigTest, FromYaml_IncompleteConfigUsesDefaults) {
102109
out_file << "logFolderPath: log_path\n"; // Missing other fields
103110
out_file.close();
104111

105-
CortexConfig loaded_config = FromYaml(test_file_path, default_config);
112+
CortexConfig loaded_config = cyu::CortexConfigMgr::GetInstance().FromYaml(
113+
test_file_path, default_config);
106114

107115
// Verify that defaults are used where values are missing
108116
EXPECT_EQ(loaded_config.logFolderPath, "log_path");

engine/test/components/test_file_manager_config_yaml_utils.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ TEST_F(FileManagerConfigTest, DumpYamlConfig) {
6363
.apiServerPort = "8080"};
6464

6565
std::string test_file = "test_config.yaml";
66-
auto result = config_yaml_utils::DumpYamlConfig(config, test_file);
66+
auto result =
67+
config_yaml_utils::CortexConfigMgr::GetInstance().DumpYamlConfig(
68+
config, test_file);
6769
EXPECT_FALSE(result.has_error());
6870
EXPECT_TRUE(std::filesystem::exists(test_file));
6971

@@ -83,7 +85,8 @@ TEST_F(FileManagerConfigTest, FromYaml) {
8385
out_file.close();
8486

8587
config_yaml_utils::CortexConfig default_config{};
86-
auto config = config_yaml_utils::FromYaml(test_file, default_config);
88+
auto config = config_yaml_utils::CortexConfigMgr::GetInstance().FromYaml(
89+
test_file, default_config);
8790

8891
EXPECT_EQ(config.logFolderPath, "/path/to/logs");
8992
EXPECT_EQ(config.dataFolderPath, "/path/to/data");

0 commit comments

Comments
 (0)