Skip to content

Commit 6b1d165

Browse files
authored
Python Logging Seg Fault Fix (triton-inference-server#183)
1 parent d35df5f commit 6b1d165

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/pb_stub.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,18 @@ Stub::LogServiceActive()
939939
return log_thread_;
940940
}
941941

942+
std::unique_ptr<Logger> Logger::log_instance_;
943+
944+
std::unique_ptr<Logger>&
945+
Logger::GetOrCreateInstance()
946+
{
947+
if (Logger::log_instance_.get() == nullptr) {
948+
Logger::log_instance_ = std::make_unique<Logger>();
949+
}
950+
951+
return Logger::log_instance_;
952+
}
953+
942954
// Bound function, called from the python client
943955
void
944956
Logger::Log(const std::string& message, LogLevel level)
@@ -954,7 +966,7 @@ Logger::Log(const std::string& message, LogLevel level)
954966
uint32_t line = lineno.cast<uint32_t>();
955967

956968
if (!stub->LogServiceActive()) {
957-
Logger::GetOrCreateInstance().Log(filename, line, level, message);
969+
Logger::GetOrCreateInstance()->Log(filename, line, level, message);
958970
} else {
959971
std::unique_ptr<PbLog> log_msg(new PbLog(filename, line, message, level));
960972
stub->EnqueueLogRequest(log_msg);
@@ -1195,6 +1207,7 @@ main(int argc, char** argv)
11951207
std::string triton_install_path = argv[6];
11961208
std::string name = argv[8];
11971209

1210+
std::unique_ptr<Logger>& logger = Logger::GetOrCreateInstance();
11981211
std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance();
11991212
try {
12001213
stub->Instantiate(
@@ -1213,7 +1226,7 @@ main(int argc, char** argv)
12131226

12141227
std::atomic<bool> background_thread_running = {true};
12151228
std::thread background_thread =
1216-
std::thread([&parent_pid, &background_thread_running, &stub] {
1229+
std::thread([&parent_pid, &background_thread_running, &stub, &logger] {
12171230
while (background_thread_running) {
12181231
// Every 300ms set the health variable to true. This variable is in
12191232
// shared memory and will be set to false by the parent process.
@@ -1235,6 +1248,7 @@ main(int argc, char** argv)
12351248
non_graceful_exit = true;
12361249

12371250
// Destroy stub and exit.
1251+
logger.reset();
12381252
stub.reset();
12391253
exit(1);
12401254
}
@@ -1264,6 +1278,7 @@ main(int argc, char** argv)
12641278
// objects. If the scoped_interpreter is destroyed before the stub object,
12651279
// this process will no longer hold the GIL lock and destruction of the stub
12661280
// will result in segfault.
1281+
logger.reset();
12671282
stub.reset();
12681283

12691284
return 0;

src/pb_stub.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ namespace triton { namespace backend { namespace python {
7878
class Logger {
7979
public:
8080
Logger(){};
81+
~Logger(){ log_instance_.reset(); };
8182
/// Python client log function
8283
static void Log(const std::string& message, LogLevel level = LogLevel::INFO);
8384

@@ -101,17 +102,16 @@ class Logger {
101102
/// Log format helper function
102103
const std::string LeadingLogChar(const LogLevel& level);
103104

104-
/// Singleton Getter function
105-
static Logger& GetOrCreateInstance()
106-
{
107-
static Logger instance;
108-
return instance;
109-
}
105+
/// Singleton Getter Function
106+
static std::unique_ptr<Logger>& GetOrCreateInstance();
110107

111108
DISALLOW_COPY_AND_ASSIGN(Logger);
112109

113110
/// Flush the log.
114111
void Flush() { std::cerr << std::flush; }
112+
113+
private:
114+
static std::unique_ptr<Logger> log_instance_;
115115
};
116116

117117
class LogMessage {
@@ -130,7 +130,7 @@ class LogMessage {
130130
/// Log message to console or send to backend (see Logger::Log for details)
131131
~LogMessage()
132132
{
133-
Logger::GetOrCreateInstance().Log(file_, line_, level_, stream_.str());
133+
Logger::GetOrCreateInstance()->Log(file_, line_, level_, stream_.str());
134134
}
135135

136136
std::stringstream& stream() { return stream_; }
@@ -146,7 +146,7 @@ class LogMessage {
146146

147147
class Stub {
148148
public:
149-
Stub(){};
149+
Stub(){ log_thread_ = false; };
150150
static std::unique_ptr<Stub>& GetOrCreateInstance();
151151

152152
/// Instantiate a new Python backend Stub.

0 commit comments

Comments
 (0)