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

Commit 835e68f

Browse files
authored
chore: consolidate subprocess utils (#2011)
1 parent 0250ac7 commit 835e68f

File tree

5 files changed

+73
-83
lines changed

5 files changed

+73
-83
lines changed

engine/extensions/python-engine/python_engine.cc

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,11 @@ bool PythonEngine::TerminateModelProcess(const std::string& model) {
8989
return false;
9090
}
9191

92-
#if defined(_WIN32)
93-
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, it->second);
94-
if (hProcess == NULL) {
95-
LOG_ERROR << "Failed to open process";
96-
return false;
97-
}
98-
99-
bool terminated = TerminateProcess(hProcess, 0) == TRUE;
100-
CloseHandle(hProcess);
101-
102-
if (terminated) {
92+
bool success = cortex::process::KillProcess(it->second);
93+
if (success) {
10394
process_map_.erase(it);
104-
return true;
10595
}
106-
107-
#elif defined(__APPLE__) || defined(__linux__)
108-
int result = kill(it->second, SIGTERM);
109-
if (result == 0) {
110-
process_map_.erase(it);
111-
return true;
112-
}
113-
#endif
114-
115-
return false;
96+
return success;
11697
}
11798

11899
CurlResponse PythonEngine::MakeGetRequest(const std::string& model,
@@ -823,7 +804,7 @@ void PythonEngine::GetModelStatus(
823804
auto model_config = models_[model];
824805
auto health_endpoint = model_config.heath_check;
825806
auto pid = process_map_[model];
826-
auto is_process_live = process_status_utils::IsProcessRunning(pid);
807+
auto is_process_live = cortex::process::IsProcessAlive(pid);
827808
auto response_health = MakeGetRequest(model, health_endpoint.path);
828809

829810
if (response_health.error && is_process_live) {

engine/extensions/python-engine/python_engine.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "extensions/template_renderer.h"
1515
#include "utils/file_logger.h"
1616
#include "utils/file_manager_utils.h"
17-
#include "utils/process_status_utils.h"
1817
#include "utils/curl_utils.h"
1918
#include "utils/process/utils.h"
2019

engine/utils/process/utils.cc

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "utils/process/utils.h"
22
#include "utils/logging_utils.h"
33

4-
#if defined(__APPLE__) || defined(__linux__)
4+
#if defined(_WIN32)
5+
#include <tlhelp32.h>
6+
#elif defined(__APPLE__) || defined(__linux__)
57
extern char **environ; // environment variables
68
#endif
79

@@ -103,4 +105,66 @@ pid_t SpawnProcess(const std::vector<std::string>& command) {
103105
}
104106
}
105107

106-
} // namespace cortex::process
108+
bool IsProcessAlive(pid_t pid) {
109+
#ifdef _WIN32
110+
// Windows implementation
111+
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
112+
if (snapshot == INVALID_HANDLE_VALUE) {
113+
return false;
114+
}
115+
116+
PROCESSENTRY32 processEntry = {0};
117+
processEntry.dwSize = sizeof(processEntry);
118+
119+
if (Process32First(snapshot, &processEntry)) {
120+
do {
121+
if (processEntry.th32ProcessID == pid) {
122+
CloseHandle(snapshot);
123+
return true;
124+
}
125+
} while (Process32Next(snapshot, &processEntry));
126+
}
127+
128+
CloseHandle(snapshot);
129+
return false;
130+
131+
#elif defined(__APPLE__) || defined(__linux__)
132+
// Unix-like systems (Linux and macOS) implementation
133+
if (pid <= 0) {
134+
return false;
135+
}
136+
137+
// Try to send signal 0 to the process
138+
// This doesn't actually send a signal but checks if we can send signals to the process
139+
int result = kill(pid, 0);
140+
141+
if (result == 0) {
142+
return true; // Process exists and we have permission to send it signals
143+
}
144+
145+
return errno != ESRCH; // ESRCH means "no such process"
146+
#else
147+
#error "Unsupported platform"
148+
#endif
149+
}
150+
151+
bool KillProcess(pid_t pid) {
152+
#if defined(_WIN32)
153+
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
154+
if (hProcess == NULL) {
155+
LOG_ERROR << "Failed to open process";
156+
return false;
157+
}
158+
159+
bool is_success = TerminateProcess(hProcess, 0) == TRUE;
160+
CloseHandle(hProcess);
161+
return is_success;
162+
#elif defined(__APPLE__) || defined(__linux__)
163+
// NOTE: should we use SIGKILL here to be consistent with Windows?
164+
return kill(pid, SIGTERM) == 0;
165+
#else
166+
#error "Unsupported platform"
167+
#endif
168+
}
169+
170+
} // namespace cortex::process

engine/utils/process/utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ std::string ConstructWindowsCommandLine(const std::vector<std::string>& args);
2121
std::vector<char*> ConvertToArgv(const std::vector<std::string>& args);
2222

2323
pid_t SpawnProcess(const std::vector<std::string>& command);
24+
bool IsProcessAlive(pid_t pid);
25+
bool KillProcess(pid_t pid);
2426

25-
}
27+
}

engine/utils/process_status_utils.h

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)