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

Commit 0d744fc

Browse files
authored
feat: improvements to subprocess functionality (#2047)
* bug/feat: improve subprocess * log subprocess command for easier debugging
1 parent fd4edc0 commit 0d744fc

File tree

6 files changed

+272
-56
lines changed

6 files changed

+272
-56
lines changed

engine/cli/commands/server_start_cmd.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ bool ServerStartCmd::Exec(const std::string& host, int port,
119119
commands.push_back(get_data_folder_path());
120120
commands.push_back("--loglevel");
121121
commands.push_back(log_level_);
122-
auto pid = cortex::process::SpawnProcess(commands);
123-
if (pid < 0) {
122+
auto result = cortex::process::SpawnProcess(commands);
123+
if (result.has_error()) {
124124
// Fork failed
125-
std::cerr << "Could not start server: " << std::endl;
125+
std::cerr << "Could not start server: " << result.error() << std::endl;
126126
return false;
127127
} else {
128128
// Parent process

engine/extensions/python-engine/python_engine.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,18 @@ void PythonEngine::LoadModel(
286286

287287
// Add the parsed arguments to the command
288288
command.insert(command.end(), args.begin(), args.end());
289-
pid = cortex::process::SpawnProcess(command);
290-
process_map_[model] = pid;
291-
if (pid == -1) {
289+
auto result = cortex::process::SpawnProcess(command);
290+
291+
if (result.has_error()) {
292+
CTL_ERR("Fail to spawn process. " << result.error());
293+
292294
std::unique_lock lock(models_mutex_);
293295
if (models_.find(model) != models_.end()) {
294296
models_.erase(model);
295297
}
296298

297299
Json::Value error;
298-
error["error"] = "Fail to spawn process with pid -1";
300+
error["error"] = "Fail to spawn process";
299301
Json::Value status;
300302
status["is_done"] = true;
301303
status["has_error"] = true;
@@ -304,6 +306,9 @@ void PythonEngine::LoadModel(
304306
callback(std::move(status), std::move(error));
305307
return;
306308
}
309+
310+
pid = result.value().pid;
311+
process_map_[model] = result.value();
307312
} catch (const std::exception& e) {
308313
std::unique_lock lock(models_mutex_);
309314
if (models_.find(model) != models_.end()) {
@@ -356,7 +361,7 @@ void PythonEngine::UnloadModel(
356361
} else {
357362
Json::Value error;
358363
error["error"] = "Fail to terminate process with id: " +
359-
std::to_string(process_map_[model]);
364+
std::to_string(process_map_[model].pid);
360365
Json::Value status;
361366
status["is_done"] = true;
362367
status["has_error"] = true;

engine/extensions/python-engine/python_engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PythonEngine : public EngineI {
3939
std::unordered_map<std::string, config::PythonModelConfig> models_;
4040
extensions::TemplateRenderer renderer_;
4141
std::unique_ptr<trantor::FileLogger> async_file_logger_;
42-
std::unordered_map<std::string, pid_t> process_map_;
42+
std::unordered_map<std::string, cortex::process::ProcessInfo> process_map_;
4343
trantor::ConcurrentTaskQueue q_;
4444

4545
// Helper functions

engine/services/hardware_service.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ bool HardwareService::Restart(const std::string& host, int port) {
197197
commands.push_back(get_data_folder_path());
198198
commands.push_back("--loglevel");
199199
commands.push_back(luh::LogLevelStr(luh::global_log_level));
200-
auto pid = cortex::process::SpawnProcess(commands);
201-
if (pid < 0) {
200+
auto result = cortex::process::SpawnProcess(commands);
201+
if (result.has_error()) {
202202
// Fork failed
203-
std::cerr << "Could not start server: " << std::endl;
203+
std::cerr << "Could not start server: " << result.error() << std::endl;
204204
return false;
205205
} else {
206206
// Parent process

0 commit comments

Comments
 (0)