From e5de787a85fcb088d8c6cc4948c9efcb88b455c8 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 7 May 2026 20:21:55 +0300 Subject: [PATCH 01/41] almostly finished fixing envs overfitting --- src/pb_env.cc | 43 +++++++++++++++++++++++++++++++------------ src/pb_env.h | 41 ++++++++++++++++++++++++++++++++--------- src/stub_launcher.cc | 18 ++++++++---------- src/stub_launcher.h | 5 ++++- 4 files changed, 75 insertions(+), 32 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index d9643a62..89203018 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -228,6 +228,9 @@ RecursiveDirectoryDelete(const char* dir) EnvironmentManager::EnvironmentManager() { + LOG_MESSAGE( + TRITONSERVER_LOG_VERBOSE, + "EnvironmentManager constructor: initializing Python env manager"); char tmp_dir_template[PATH_MAX + 1]; strcpy(tmp_dir_template, "/tmp/python_env_XXXXXX"); @@ -239,13 +242,19 @@ EnvironmentManager::EnvironmentManager() strcpy(base_path_, tmp_dir_template); } -std::string -EnvironmentManager::ExtractIfNotExtracted(std::string env_path) +std::shared_ptr // TODO: write logic with shared and weak ptrs in this method +EnvironmentManager::GetEnvironment(ModelState* model_state) { // Lock the mutex. Only a single thread should modify the map. std::lock_guard lk(mutex_); + + std::string env_path = model_state.GetEnvironmentPath(); char canonical_env_path[PATH_MAX + 1]; + std::string env_key = env_path; + RETURN_IF_ERROR(model_state->ModelConfig().MemberAsString( + "name", &env_key)); + char* err = realpath(env_path.c_str(), canonical_env_path); if (err == nullptr) { throw PythonBackendException( @@ -272,21 +281,24 @@ EnvironmentManager::ExtractIfNotExtracted(std::string env_path) .c_str()); return canonical_env_path; } - const auto env_itr = env_map_.find(canonical_env_path); + + std::string model_id = model_state.GetModelId(); + const auto env_itr = env_map_.find(env_key); if (env_itr != env_map_.end()) { // Check if the environment has been modified and would - // need to be extracted again. - if (env_itr->second.second == last_modified_time) { + // need to be extracted again (or the current environment has no owners anymore). + if (env_itr->second.first.expired()) { + env_map_.erase(env_itr); + } else if (env_itr->second.second == last_modified_time) { env_extracted = true; } else { // Environment file has been updated. Need to clear // the previously extracted environment and extract // the environment to the same destination directory. - RecursiveDirectoryDelete(env_itr->second.first.c_str()); re_extraction = true; } } - + // Extract only if the env has not been extracted yet. if (!env_extracted) { LOG_MESSAGE( @@ -295,7 +307,7 @@ EnvironmentManager::ExtractIfNotExtracted(std::string env_path) .c_str()); std::string dst_env_path; if (re_extraction) { - dst_env_path = env_map_[canonical_env_path].first; + dst_env_path = env_map_[env_key].first; } else { dst_env_path = std::string(base_path_) + "/" + std::to_string(env_map_.size()); @@ -314,21 +326,28 @@ EnvironmentManager::ExtractIfNotExtracted(std::string env_path) } if (re_extraction) { // Just update the last modified timestamp - env_map_[canonical_env_path].second = last_modified_time; + env_map_[env_key].second = last_modified_time; } else { // Add the path to the list of environments - env_map_.insert({canonical_env_path, {dst_env_path, last_modified_time}}); + env_map_.insert({env_key, {dst_env_path, last_modified_time}}); } return dst_env_path; - } else { - return env_map_.find(canonical_env_path)->second.first; } + return env_map_.find(env_key)->second.first; } EnvironmentManager::~EnvironmentManager() { RecursiveDirectoryDelete(base_path_); } + +Environment::Environment(const std::string& source, const std::string& path) : source_(source), path_(path) { + ExtractTarFile(source, path); +} +Environment::~Environment() { + RecursiveDirectoryDelete(path_.c_str()); +} + #endif }}} // namespace triton::backend::python diff --git a/src/pb_env.h b/src/pb_env.h index 04e01fa3..4d71e063 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -27,9 +27,12 @@ #pragma once #include #include +#include #include #include +#include "python_be.h" + #ifdef WIN32 #include #undef PATH_MAX @@ -46,17 +49,37 @@ bool FileExists(std::string& path); // #ifndef _WIN32 class EnvironmentManager { - std::map> env_map_; - char base_path_[PATH_MAX + 1]; - std::mutex mutex_; + public: + class Environment { + public: + Environment(const std::string& source, const std::string& path); + ~Environment(); + + const std::string& Source() const { + return source_; + } + const std::string& Path() const { + return path_; + } + explicit operator std::string() const { + return Path(); + } - public: - EnvironmentManager(); + private: + std::string source_; + std::string path_; + }; + + EnvironmentManager(); + // Extracts the tar.gz file in the 'env_path' if it has not been + // already extracted. + std::shared_ptr GetEnvironment(ModelState* model_state); + ~EnvironmentManager(); - // Extracts the tar.gz file in the 'env_path' if it has not been - // already extracted. - std::string ExtractIfNotExtracted(std::string env_path); - ~EnvironmentManager(); + private: + std::map, time_t>> env_map_; + char base_path_[PATH_MAX + 1]; + std::mutex mutex_; }; #endif diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index 6a1c8f2b..fc8d8d21 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -64,7 +64,7 @@ StubLauncher::Initialize(ModelState* model_state) shm_growth_byte_size_ = model_state->StateForBackend()->shm_growth_byte_size; shm_message_queue_size_ = model_state->StateForBackend()->shm_message_queue_size; - python_execution_env_ = model_state->PythonExecutionEnv(); + python_execution_env_source_ = model_state->PythonExecutionEnv(); python_lib_ = model_state->StateForBackend()->python_lib; model_state->ModelConfig().Write(&model_config_buffer_); is_decoupled_ = model_state->IsDecoupled(); @@ -104,7 +104,7 @@ StubLauncher::Initialize(ModelState* model_state) // FIXME [DLIS-5969]: Enable for Windows when custom execution environments // are supported. - if (python_execution_env_ != "") { + if (python_execution_env_source_ != "") { #ifndef _WIN32 RETURN_IF_ERROR(GetPythonEnvironment(model_state)); #else @@ -405,7 +405,7 @@ StubLauncher::Launch() // executables and libraries. ipc_control_->uses_env = false; - if (python_execution_env_ != "") { + if (python_execution_env_source_ != "") { ipc_control_->uses_env = true; // Parse environment variables from activation script @@ -592,20 +592,18 @@ StubLauncher::Launch() TRITONSERVER_Error* StubLauncher::GetPythonEnvironment(ModelState* model_state) { - std::string python_execution_env = ""; try { - python_execution_env = - model_state->StateForBackend()->env_manager->ExtractIfNotExtracted( - python_execution_env_); + python_execution_env_ = + model_state->StateForBackend()->env_manager->GetEnvironment(model_state); } catch (PythonBackendException& pb_exception) { return TRITONSERVER_ErrorNew( TRITONSERVER_ERROR_INTERNAL, pb_exception.what()); } - path_to_activate_ = python_execution_env + "/bin/activate"; - path_to_libpython_ = python_execution_env + "/lib"; - if (python_execution_env.length() > 0 && !FileExists(path_to_activate_)) { + path_to_activate_ = python_execution_env_->Path() + "/bin/activate"; + path_to_libpython_ = python_execution_env_->Path() + "/lib"; + if (python_execution_env_->Path().length() > 0 && !FileExists(path_to_activate_)) { return TRITONSERVER_ErrorNew( TRITONSERVER_ERROR_INTERNAL, ("Path " + path_to_activate_ + diff --git a/src/stub_launcher.h b/src/stub_launcher.h index fba116df..4c9ad139 100644 --- a/src/stub_launcher.h +++ b/src/stub_launcher.h @@ -50,6 +50,8 @@ #include "triton/core/tritonbackend.h" #include "triton/core/tritonserver.h" +#include "pb_env.h" + namespace triton { namespace backend { namespace python { class ModelState; @@ -203,7 +205,8 @@ class StubLauncher { // Path to python execution environment std::string path_to_libpython_; std::string path_to_activate_; - std::string python_execution_env_; + std::shared_ptr python_execution_env_; + std::string python_execution_env_source_; common::TritonJson::WriteBuffer model_config_buffer_; common::TritonJson::Value auto_complete_config_; From 25b2e2574156b0ec6edf74d918b5745c3ffc33f6 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Fri, 8 May 2026 14:47:14 +0300 Subject: [PATCH 02/41] finished amends --- src/pb_env.cc | 41 ++++++++++++++++++++++++++--------------- src/pb_env.h | 7 +++++-- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 89203018..92e11da9 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -248,12 +248,9 @@ EnvironmentManager::GetEnvironment(ModelState* model_state) // Lock the mutex. Only a single thread should modify the map. std::lock_guard lk(mutex_); - std::string env_path = model_state.GetEnvironmentPath(); + std::string env_path = model_state->GetEnvironmentPath(); char canonical_env_path[PATH_MAX + 1]; - std::string env_key = env_path; - RETURN_IF_ERROR(model_state->ModelConfig().MemberAsString( - "name", &env_key)); char* err = realpath(env_path.c_str(), canonical_env_path); if (err == nullptr) { @@ -279,10 +276,11 @@ EnvironmentManager::GetEnvironment(ModelState* model_state) "not contain compressed path. Path: ") + canonical_env_path) .c_str()); - return canonical_env_path; + return std::make_shared(canonical_env_path, canonical_env_path); } std::string model_id = model_state.GetModelId(); + std::string env_key = model_state->Name() + "-" + model_id; const auto env_itr = env_map_.find(env_key); if (env_itr != env_map_.end()) { // Check if the environment has been modified and would @@ -317,23 +315,24 @@ EnvironmentManager::GetEnvironment(ModelState* model_state) int status = mkdir(dst_env_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if (status == 0) { - ExtractTarFile(canonical_env_path_str, dst_env_path); - } else { + if (status != 0) { throw PythonBackendException( - std::string("Failed to create environment directory for '") + - dst_env_path.c_str() + "'."); + std::string("Failed to create environment directory for '") + + dst_env_path.c_str() + "'."); } + if (re_extraction) { // Just update the last modified timestamp + env_map_[env_key].first->Update(); env_map_[env_key].second = last_modified_time; } else { - // Add the path to the list of environments - env_map_.insert({env_key, {dst_env_path, last_modified_time}}); + // Add the environment to the list of environments + auto new_environment = std::make_shared(canonical_env_path_str, dst_env_path); + env_map_.insert({env_key, {new_environment, last_modified_time}}); } - return dst_env_path; } - return env_map_.find(env_key)->second.first; + + return env_map_.find(env_key)->second.first.lock(); } EnvironmentManager::~EnvironmentManager() @@ -342,8 +341,20 @@ EnvironmentManager::~EnvironmentManager() } Environment::Environment(const std::string& source, const std::string& path) : source_(source), path_(path) { - ExtractTarFile(source, path); + if (source == path) { + return; + } + Extract(); } + +void Environment::Extract() { + ExtractTarFile(source_, path_); +} + +void Environment::Update() { + Extract(); +} + Environment::~Environment() { RecursiveDirectoryDelete(path_.c_str()); } diff --git a/src/pb_env.h b/src/pb_env.h index 4d71e063..5de8c314 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -52,8 +52,8 @@ class EnvironmentManager { public: class Environment { public: - Environment(const std::string& source, const std::string& path); - ~Environment(); + Environment(const std::string& source, const std::string& path); + void Update(); const std::string& Source() const { return source_; @@ -66,11 +66,14 @@ class EnvironmentManager { } private: + void Extract(); + std::string source_; std::string path_; }; EnvironmentManager(); + // Extracts the tar.gz file in the 'env_path' if it has not been // already extracted. std::shared_ptr GetEnvironment(ModelState* model_state); From 689333c1af0bc6ac1e482cb1f6b39b78e3c30eaf Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Fri, 8 May 2026 14:56:06 +0300 Subject: [PATCH 03/41] added improvements --- src/stub_launcher.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index fc8d8d21..39409fc3 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -594,16 +594,17 @@ StubLauncher::GetPythonEnvironment(ModelState* model_state) { try { python_execution_env_ = - model_state->StateForBackend()->env_manager->GetEnvironment(model_state); + std::move(model_state->StateForBackend()->env_manager->GetEnvironment(model_state)); } catch (PythonBackendException& pb_exception) { return TRITONSERVER_ErrorNew( TRITONSERVER_ERROR_INTERNAL, pb_exception.what()); } - path_to_activate_ = python_execution_env_->Path() + "/bin/activate"; - path_to_libpython_ = python_execution_env_->Path() + "/lib"; - if (python_execution_env_->Path().length() > 0 && !FileExists(path_to_activate_)) { + std::string python_execution_env_path = python_execution_env_->Path(); + path_to_activate_ = python_execution_env_path + "/bin/activate"; + path_to_libpython_ = python_execution_env_path + "/lib"; + if (python_execution_env_path.length() > 0 && !FileExists(path_to_activate_)) { return TRITONSERVER_ErrorNew( TRITONSERVER_ERROR_INTERNAL, ("Path " + path_to_activate_ + From c0db109f29647d8cb87558e807aedc779840f1b2 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Tue, 12 May 2026 15:59:23 +0300 Subject: [PATCH 04/41] added little improves and also fixed ExtractIfNotExtracted --- src/pb_env.cc | 105 +++++++++++++++++++++++++++---------------- src/pb_env.h | 64 +++++++++++++------------- src/stub_launcher.cc | 5 ++- 3 files changed, 100 insertions(+), 74 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 92e11da9..766d79e2 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -242,97 +242,98 @@ EnvironmentManager::EnvironmentManager() strcpy(base_path_, tmp_dir_template); } -std::shared_ptr // TODO: write logic with shared and weak ptrs in this method -EnvironmentManager::GetEnvironment(ModelState* model_state) +std::shared_ptr< + Environment> // TODO: write logic with shared and weak ptrs in this method +EnvironmentManager::ExtractIfNotExtracted(const std::string& source_env_path) { // Lock the mutex. Only a single thread should modify the map. std::lock_guard lk(mutex_); - std::string env_path = model_state->GetEnvironmentPath(); - char canonical_env_path[PATH_MAX + 1]; + std::string source_env_path = model_state->GetEnvironmentPath(); + char canonical_source_env_path[PATH_MAX + 1]; - char* err = realpath(env_path.c_str(), canonical_env_path); + char* err = realpath(source_env_path.c_str(), canonical_source_env_path); if (err == nullptr) { throw PythonBackendException( - std::string("Failed to get the canonical path for ") + env_path + "."); + std::string("Failed to get the canonical path for ") + source_env_path + + "."); } time_t last_modified_time; - LastModifiedTime(canonical_env_path, &last_modified_time); + LastModifiedTime(canonical_source_env_path, &last_modified_time); bool env_extracted = false; bool re_extraction = false; // If the path is not a conda-packed file, then bypass the extraction process struct stat info; - if (stat(canonical_env_path, &info) != 0) { + if (stat(canonical_source_env_path, &info) != 0) { throw PythonBackendException( - std::string("stat() of : ") + canonical_env_path + " returned error."); + std::string("stat() of : ") + canonical_source_env_path + + " returned error."); } else if (S_ISDIR(info.st_mode)) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, (std::string("Returning canonical path since EXECUTION_ENV_PATH does " "not contain compressed path. Path: ") + - canonical_env_path) + canonical_source_env_path) .c_str()); - return std::make_shared(canonical_env_path, canonical_env_path); + return nullptr; } - std::string model_id = model_state.GetModelId(); - std::string env_key = model_state->Name() + "-" + model_id; + std::string canonical_source_env_path_str(canonical_source_env_path); + std::string env_key = canonical_source_env_path_str; const auto env_itr = env_map_.find(env_key); + const auto env_path_itr = env_path_map_.find(env_key); if (env_itr != env_map_.end()) { // Check if the environment has been modified and would - // need to be extracted again (or the current environment has no owners anymore). + // need to be extracted again (or the current environment has no owners + // anymore). if (env_itr->second.first.expired()) { env_map_.erase(env_itr); + env_itr = env_map_.end(); } else if (env_itr->second.second == last_modified_time) { env_extracted = true; - } else { + } + if (env_path_itr != env_path_itr.end()) { // Environment file has been updated. Need to clear // the previously extracted environment and extract // the environment to the same destination directory. + // Or environment is expired (no model owners) re_extraction = true; } } - + // Extract only if the env has not been extracted yet. if (!env_extracted) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, - (std::string("Extracting Python execution env ") + canonical_env_path) + (std::string("Extracting Python execution env ") + + canonical_source_env_path) .c_str()); + std::string dst_env_path; if (re_extraction) { - dst_env_path = env_map_[env_key].first; + dst_env_path = env_path_map_[env_key]; } else { dst_env_path = std::string(base_path_) + "/" + std::to_string(env_map_.size()); + env_path_map_.insert({env_key, dst_env_path}); } - std::string canonical_env_path_str(canonical_env_path); - - int status = - mkdir(dst_env_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if (status != 0) { - throw PythonBackendException( - std::string("Failed to create environment directory for '") + - dst_env_path.c_str() + "'."); - } - - if (re_extraction) { - // Just update the last modified timestamp - env_map_[env_key].first->Update(); - env_map_[env_key].second = last_modified_time; + if (re_extraction && env_itr != env_map_.end()) { + // Just replace with new environment (by new source) + env_map_[env_key].second->Update(last_modified_time); } else { // Add the environment to the list of environments - auto new_environment = std::make_shared(canonical_env_path_str, dst_env_path); - env_map_.insert({env_key, {new_environment, last_modified_time}}); + auto new_environment = std::make_shared( + canonical_source_env_path_str, dst_env_path, last_modified_time); + env_map_.insert({env_key, new_environment}); } } - return env_map_.find(env_key)->second.first.lock(); + return env_map_.find(env_key)->second.lock(); } EnvironmentManager::~EnvironmentManager() @@ -340,25 +341,51 @@ EnvironmentManager::~EnvironmentManager() RecursiveDirectoryDelete(base_path_); } -Environment::Environment(const std::string& source, const std::string& path) : source_(source), path_(path) { +Environment::Environment( + const std::string& source, const std::string& path, + const time_t& last_modified_time) + : source_(source), path_(path), + last_modified_time_(std::to_string(last_modified_time)) +{ if (source == path) { return; } Extract(); } -void Environment::Extract() { +void +Environment::Extract() +{ + int status = + mkdir(dst_env_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (status != 0) { + throw PythonBackendException( + std::string("Failed to create environment directory for '") + + dst_env_path.c_str() + "'."); + } ExtractTarFile(source_, path_); } -void Environment::Update() { +void +Environment::Update(const time_t& last_modified_time) +{ + Delete(); Extract(); + last_modified_time_ = last_modified_time; } -Environment::~Environment() { +void +Environment::Delete() +{ RecursiveDirectoryDelete(path_.c_str()); } +Environment::~Environment() +{ + Delete(); +} + + #endif }}} // namespace triton::backend::python diff --git a/src/pb_env.h b/src/pb_env.h index 5de8c314..ce2c202f 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -31,8 +31,6 @@ #include #include -#include "python_be.h" - #ifdef WIN32 #include #undef PATH_MAX @@ -49,40 +47,40 @@ bool FileExists(std::string& path); // #ifndef _WIN32 class EnvironmentManager { - public: - class Environment { - public: - Environment(const std::string& source, const std::string& path); - void Update(); + public: + class Environment { + public: + Environment( + const std::string& source, const std::string& path, + const time_t& last_modified_time); + void Update(const time_t& last_modified_time); + + const std::string& Source() const { return source_; } + const std::string& Path() const { return path_; } + explicit operator std::string() const { return Path(); } + + private: + void Extract(); + void Delete(); + + std::string source_; + std::string path_; + std::string last_modified_time_; + }; - const std::string& Source() const { - return source_; - } - const std::string& Path() const { - return path_; - } - explicit operator std::string() const { - return Path(); - } + EnvironmentManager(); - private: - void Extract(); - - std::string source_; - std::string path_; - }; - - EnvironmentManager(); - - // Extracts the tar.gz file in the 'env_path' if it has not been - // already extracted. - std::shared_ptr GetEnvironment(ModelState* model_state); - ~EnvironmentManager(); + // Extracts the tar.gz file in the 'env_path' if it has not been + // already extracted. + std::shared_ptr ExtractIfNotExtracted( + const std::string& source_env_path); + ~EnvironmentManager(); - private: - std::map, time_t>> env_map_; - char base_path_[PATH_MAX + 1]; - std::mutex mutex_; + private: + std::map env_path_map_; + std::map> env_map_; + char base_path_[PATH_MAX + 1]; + std::mutex mutex_; }; #endif diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index 39409fc3..ba0909fd 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -594,7 +594,7 @@ StubLauncher::GetPythonEnvironment(ModelState* model_state) { try { python_execution_env_ = - std::move(model_state->StateForBackend()->env_manager->GetEnvironment(model_state)); + model_state->StateForBackend()->env_manager->GetEnvironment(model_state); } catch (PythonBackendException& pb_exception) { return TRITONSERVER_ErrorNew( @@ -604,7 +604,8 @@ StubLauncher::GetPythonEnvironment(ModelState* model_state) std::string python_execution_env_path = python_execution_env_->Path(); path_to_activate_ = python_execution_env_path + "/bin/activate"; path_to_libpython_ = python_execution_env_path + "/lib"; - if (python_execution_env_path.length() > 0 && !FileExists(path_to_activate_)) { + if (python_execution_env_path.length() > 0 && + !FileExists(path_to_activate_)) { return TRITONSERVER_ErrorNew( TRITONSERVER_ERROR_INTERNAL, ("Path " + path_to_activate_ + From 02e935748c7b33c212c81bf4e706cd91a00d3f44 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Tue, 12 May 2026 17:46:49 +0300 Subject: [PATCH 05/41] added final amends to env logic --- src/pb_env.cc | 68 +++++++++++++++++++++++---------------------------- src/pb_env.h | 12 ++++++--- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 766d79e2..7f725bfe 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -244,63 +244,54 @@ EnvironmentManager::EnvironmentManager() std::shared_ptr< Environment> // TODO: write logic with shared and weak ptrs in this method -EnvironmentManager::ExtractIfNotExtracted(const std::string& source_env_path) +EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) { // Lock the mutex. Only a single thread should modify the map. std::lock_guard lk(mutex_); - std::string source_env_path = model_state->GetEnvironmentPath(); - char canonical_source_env_path[PATH_MAX + 1]; - - - char* err = realpath(source_env_path.c_str(), canonical_source_env_path); + char canonical_env_path[PATH_MAX + 1]; + char* err = realpath(env_path.c_str(), canonical_env_path); if (err == nullptr) { throw PythonBackendException( - std::string("Failed to get the canonical path for ") + source_env_path + - "."); + std::string("Failed to get the canonical path for ") + env_path + "."); } time_t last_modified_time; - LastModifiedTime(canonical_source_env_path, &last_modified_time); + LastModifiedTime(canonical_env_path, &last_modified_time); bool env_extracted = false; bool re_extraction = false; // If the path is not a conda-packed file, then bypass the extraction process struct stat info; - if (stat(canonical_source_env_path, &info) != 0) { + if (stat(canonical_env_path, &info) != 0) { throw PythonBackendException( - std::string("stat() of : ") + canonical_source_env_path + - " returned error."); + std::string("stat() of : ") + canonical_env_path + " returned error."); } else if (S_ISDIR(info.st_mode)) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, (std::string("Returning canonical path since EXECUTION_ENV_PATH does " "not contain compressed path. Path: ") + - canonical_source_env_path) + canonical_env_path) .c_str()); return nullptr; } - std::string canonical_source_env_path_str(canonical_source_env_path); - std::string env_key = canonical_source_env_path_str; - const auto env_itr = env_map_.find(env_key); - const auto env_path_itr = env_path_map_.find(env_key); + std::string canonical_env_path_str(canonical_env_path); + std::string env_key = canonical_env_path_str; + const auto env_itr = env_map_[env_key]; + std::shared_ptr env; if (env_itr != env_map_.end()) { + env = env_itr->second.lock(); // Check if the environment has been modified and would // need to be extracted again (or the current environment has no owners // anymore). - if (env_itr->second.first.expired()) { - env_map_.erase(env_itr); - env_itr = env_map_.end(); - } else if (env_itr->second.second == last_modified_time) { + if (env->LastModifiedTime() == last_modified_time) { env_extracted = true; - } - if (env_path_itr != env_path_itr.end()) { + } else { // Environment file has been updated. Need to clear // the previously extracted environment and extract // the environment to the same destination directory. - // Or environment is expired (no model owners) re_extraction = true; } } @@ -310,30 +301,31 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& source_env_path) LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, (std::string("Extracting Python execution env ") + - canonical_source_env_path) + canonical_env_path) .c_str()); std::string dst_env_path; if (re_extraction) { - dst_env_path = env_path_map_[env_key]; + dst_env_path = env->Path(); } else { dst_env_path = - std::string(base_path_) + "/" + std::to_string(env_map_.size()); - env_path_map_.insert({env_key, dst_env_path}); + std::string(base_path_) + "/" + std::to_string(env_path_counter_); + ++env_path_counter_; } - if (re_extraction && env_itr != env_map_.end()) { - // Just replace with new environment (by new source) - env_map_[env_key].second->Update(last_modified_time); + if (re_extraction ) { + // Just replace with new environment (by updated source) + env->Update(last_modified_time); } else { // Add the environment to the list of environments - auto new_environment = std::make_shared( - canonical_source_env_path_str, dst_env_path, last_modified_time); - env_map_.insert({env_key, new_environment}); + env = std::make_shared( + canonical_env_path_str, dst_env_path, last_modified_time); + env->SetManager(this); + env_map_.insert({env_key, new_env}); } } - return env_map_.find(env_key)->second.lock(); + return env; } EnvironmentManager::~EnvironmentManager() @@ -347,9 +339,6 @@ Environment::Environment( : source_(source), path_(path), last_modified_time_(std::to_string(last_modified_time)) { - if (source == path) { - return; - } Extract(); } @@ -382,6 +371,9 @@ Environment::Delete() Environment::~Environment() { + if (manager_ != nullptr) { + manager_->env_map_.erase(source_); + } Delete(); } diff --git a/src/pb_env.h b/src/pb_env.h index ce2c202f..c9d2e889 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -50,13 +50,17 @@ class EnvironmentManager { public: class Environment { public: + friend class EnvironmentManager; Environment( const std::string& source, const std::string& path, const time_t& last_modified_time); + void SetManager(EnvironmentManager* manager) { manager_ = manager; } + void Update(const time_t& last_modified_time); const std::string& Source() const { return source_; } const std::string& Path() const { return path_; } + const time_t& LastModifiedTime() const { return last_modified_time_; } explicit operator std::string() const { return Path(); } private: @@ -65,7 +69,9 @@ class EnvironmentManager { std::string source_; std::string path_; - std::string last_modified_time_; + time_t last_modified_time_; + + EnvironmentManager* manager_ = nullptr; }; EnvironmentManager(); @@ -73,11 +79,11 @@ class EnvironmentManager { // Extracts the tar.gz file in the 'env_path' if it has not been // already extracted. std::shared_ptr ExtractIfNotExtracted( - const std::string& source_env_path); + const std::string& env_path); ~EnvironmentManager(); private: - std::map env_path_map_; + size_t env_path_counter_ = 0; std::map> env_map_; char base_path_[PATH_MAX + 1]; std::mutex mutex_; From 5c4a52c606be90f1aa033b594e9a5f7f1c8b8e7f Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Tue, 12 May 2026 18:38:16 +0300 Subject: [PATCH 06/41] added another fixes --- src/pb_env.cc | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 7f725bfe..46bb999f 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -242,6 +242,13 @@ EnvironmentManager::EnvironmentManager() strcpy(base_path_, tmp_dir_template); } +void +EnvironmentManager::EraseEnvironment(const std::string& canoniical_env_path) +{ + std::lock_guard lk(mutex_); + env_map_.erase(canoniical_env_path); +} + std::shared_ptr< Environment> // TODO: write logic with shared and weak ptrs in this method EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) @@ -300,27 +307,20 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) if (!env_extracted) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, - (std::string("Extracting Python execution env ") + - canonical_env_path) + (std::string("Extracting Python execution env ") + canonical_env_path) .c_str()); - std::string dst_env_path; if (re_extraction) { - dst_env_path = env->Path(); + // Just replace with new environment (by updated source) + env->Update(last_modified_time); } else { - dst_env_path = + std::string dst_env_path = std::string(base_path_) + "/" + std::to_string(env_path_counter_); ++env_path_counter_; - } - if (re_extraction ) { - // Just replace with new environment (by updated source) - env->Update(last_modified_time); - } else { - // Add the environment to the list of environments env = std::make_shared( - canonical_env_path_str, dst_env_path, last_modified_time); - env->SetManager(this); + *this, canonical_env_path_str, dst_env_path, last_modified_time); + // Add the environment to the list of environments env_map_.insert({env_key, new_env}); } } @@ -334,9 +334,9 @@ EnvironmentManager::~EnvironmentManager() } Environment::Environment( - const std::string& source, const std::string& path, - const time_t& last_modified_time) - : source_(source), path_(path), + EnvironmentManager& manager, const std::string& source, + const std::string& path, const time_t& last_modified_time) + : manager_(manager), source_(source), path_(path), last_modified_time_(std::to_string(last_modified_time)) { Extract(); @@ -372,7 +372,7 @@ Environment::Delete() Environment::~Environment() { if (manager_ != nullptr) { - manager_->env_map_.erase(source_); + manager_->EraseEnvironment(source_); } Delete(); } From d4a0c77ac964420033fbd5fb66cc229bee9964fb Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Tue, 12 May 2026 19:25:23 +0300 Subject: [PATCH 07/41] added final amends --- src/pb_env.cc | 19 ++++++++----------- src/pb_env.h | 4 ---- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 46bb999f..3fdd718c 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -242,12 +242,6 @@ EnvironmentManager::EnvironmentManager() strcpy(base_path_, tmp_dir_template); } -void -EnvironmentManager::EraseEnvironment(const std::string& canoniical_env_path) -{ - std::lock_guard lk(mutex_); - env_map_.erase(canoniical_env_path); -} std::shared_ptr< Environment> // TODO: write logic with shared and weak ptrs in this method @@ -290,10 +284,16 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) std::shared_ptr env; if (env_itr != env_map_.end()) { env = env_itr->second.lock(); + // Check if the environment has been modified and would // need to be extracted again (or the current environment has no owners // anymore). - if (env->LastModifiedTime() == last_modified_time) { + + if (env == nullptr) { + // refer to case when env was not loaded + env_map_.erase(env_itr); + env_itr = env_map_.end(); + } else if (env->LastModifiedTime() == last_modified_time) { env_extracted = true; } else { // Environment file has been updated. Need to clear @@ -319,7 +319,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) ++env_path_counter_; env = std::make_shared( - *this, canonical_env_path_str, dst_env_path, last_modified_time); + canonical_env_path_str, dst_env_path, last_modified_time); // Add the environment to the list of environments env_map_.insert({env_key, new_env}); } @@ -371,9 +371,6 @@ Environment::Delete() Environment::~Environment() { - if (manager_ != nullptr) { - manager_->EraseEnvironment(source_); - } Delete(); } diff --git a/src/pb_env.h b/src/pb_env.h index c9d2e889..0bd90fb3 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -54,14 +54,12 @@ class EnvironmentManager { Environment( const std::string& source, const std::string& path, const time_t& last_modified_time); - void SetManager(EnvironmentManager* manager) { manager_ = manager; } void Update(const time_t& last_modified_time); const std::string& Source() const { return source_; } const std::string& Path() const { return path_; } const time_t& LastModifiedTime() const { return last_modified_time_; } - explicit operator std::string() const { return Path(); } private: void Extract(); @@ -70,8 +68,6 @@ class EnvironmentManager { std::string source_; std::string path_; time_t last_modified_time_; - - EnvironmentManager* manager_ = nullptr; }; EnvironmentManager(); From f4c2fcd55984833815c00011aef2eba08e4eee1f Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Wed, 13 May 2026 16:36:00 +0300 Subject: [PATCH 08/41] added last improvements to env code --- src/pb_env.cc | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 3fdd718c..03488d83 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -243,19 +243,22 @@ EnvironmentManager::EnvironmentManager() } -std::shared_ptr< - Environment> // TODO: write logic with shared and weak ptrs in this method +std::shared_ptr EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) { // Lock the mutex. Only a single thread should modify the map. std::lock_guard lk(mutex_); - char canonical_env_path[PATH_MAX + 1]; - char* err = realpath(env_path.c_str(), canonical_env_path); - if (err == nullptr) { - throw PythonBackendException( - std::string("Failed to get the canonical path for ") + env_path + "."); - } + std::string canonical_env_path = [&] { + char canonical_env_path[PATH_MAX + 1]; + char* err = realpath(env_path.c_str(), canonical_env_path); + if (err == nullptr) { + throw PythonBackendException( + std::string("Failed to get the canonical path for ") + env_path + + "."); + } + return std::string(canonical_env_path); + }(); time_t last_modified_time; LastModifiedTime(canonical_env_path, &last_modified_time); @@ -267,19 +270,18 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) struct stat info; if (stat(canonical_env_path, &info) != 0) { throw PythonBackendException( - std::string("stat() of : ") + canonical_env_path + " returned error."); + "stat() of : " + canonical_env_path + " returned error."); } else if (S_ISDIR(info.st_mode)) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, - (std::string("Returning canonical path since EXECUTION_ENV_PATH does " - "not contain compressed path. Path: ") + + ("Returning canonical path since EXECUTION_ENV_PATH does " + "not contain compressed path. Path: " + canonical_env_path) .c_str()); return nullptr; } - std::string canonical_env_path_str(canonical_env_path); - std::string env_key = canonical_env_path_str; + std::string& env_key = canonical_env_path; const auto env_itr = env_map_[env_key]; std::shared_ptr env; if (env_itr != env_map_.end()) { @@ -307,7 +309,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) if (!env_extracted) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, - (std::string("Extracting Python execution env ") + canonical_env_path) + ("Extracting Python execution env " + canonical_env_path) .c_str()); if (re_extraction) { @@ -319,7 +321,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) ++env_path_counter_; env = std::make_shared( - canonical_env_path_str, dst_env_path, last_modified_time); + canonical_env_path, dst_env_path, last_modified_time); // Add the environment to the list of environments env_map_.insert({env_key, new_env}); } From fcff38c84e15f97d09a8c7fb8c60fde4cdf8f1e7 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Wed, 13 May 2026 17:54:56 +0300 Subject: [PATCH 09/41] fixed some compilation erros in env code --- src/pb_env.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 03488d83..01326f2f 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -335,7 +335,7 @@ EnvironmentManager::~EnvironmentManager() RecursiveDirectoryDelete(base_path_); } -Environment::Environment( +EnvironmentManager::Environment::Environment( EnvironmentManager& manager, const std::string& source, const std::string& path, const time_t& last_modified_time) : manager_(manager), source_(source), path_(path), @@ -345,7 +345,7 @@ Environment::Environment( } void -Environment::Extract() +EnvironmentManager::Environment::Extract() { int status = mkdir(dst_env_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); @@ -358,7 +358,7 @@ Environment::Extract() } void -Environment::Update(const time_t& last_modified_time) +EnvironmentManager::Environment::Update(const time_t& last_modified_time) { Delete(); Extract(); @@ -366,12 +366,12 @@ Environment::Update(const time_t& last_modified_time) } void -Environment::Delete() +EnvironmentManager::Environment::Delete() { RecursiveDirectoryDelete(path_.c_str()); } -Environment::~Environment() +EnvironmentManager::Environment::~Environment() { Delete(); } From d7b10fbe6e4cc0c39b4d31d07404fa868a271156 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Wed, 13 May 2026 18:02:10 +0300 Subject: [PATCH 10/41] added another fixes to env code --- src/pb_env.cc | 6 +++--- src/pb_env.h | 1 + src/stub_launcher.cc | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 01326f2f..aeb65a0a 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -243,7 +243,7 @@ EnvironmentManager::EnvironmentManager() } -std::shared_ptr +std::shared_ptr EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) { // Lock the mutex. Only a single thread should modify the map. @@ -348,11 +348,11 @@ void EnvironmentManager::Environment::Extract() { int status = - mkdir(dst_env_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + mkdir(path_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (status != 0) { throw PythonBackendException( std::string("Failed to create environment directory for '") + - dst_env_path.c_str() + "'."); + path_.c_str() + "'."); } ExtractTarFile(source_, path_); } diff --git a/src/pb_env.h b/src/pb_env.h index 0bd90fb3..8e2f9b44 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -54,6 +54,7 @@ class EnvironmentManager { Environment( const std::string& source, const std::string& path, const time_t& last_modified_time); + ~Environment(); void Update(const time_t& last_modified_time); diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index ba0909fd..3cf4a362 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -594,7 +594,7 @@ StubLauncher::GetPythonEnvironment(ModelState* model_state) { try { python_execution_env_ = - model_state->StateForBackend()->env_manager->GetEnvironment(model_state); + model_state->StateForBackend()->env_manager->ExtractIfNotExtracted(model_state); } catch (PythonBackendException& pb_exception) { return TRITONSERVER_ErrorNew( From 013684e5d538e028236d8ee1777da6fd9297368c Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Wed, 13 May 2026 18:16:58 +0300 Subject: [PATCH 11/41] finished all compilation errors in env code --- src/stub_launcher.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index 3cf4a362..8af30865 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -593,8 +593,10 @@ TRITONSERVER_Error* StubLauncher::GetPythonEnvironment(ModelState* model_state) { try { + auto python_execution_env_source = model_state->PythonExecutionEnv(); python_execution_env_ = - model_state->StateForBackend()->env_manager->ExtractIfNotExtracted(model_state); + model_state->StateForBackend()->env_manager->ExtractIfNotExtracted( + python_execution_env_source); } catch (PythonBackendException& pb_exception) { return TRITONSERVER_ErrorNew( From 913da36c1cb214243412bd6596137482dc51f2cf Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Wed, 13 May 2026 18:51:48 +0300 Subject: [PATCH 12/41] added numerous fixes to env code --- src/pb_env.cc | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index aeb65a0a..497fe827 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -268,21 +268,21 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) // If the path is not a conda-packed file, then bypass the extraction process struct stat info; - if (stat(canonical_env_path, &info) != 0) { + if (stat(canonical_env_path.c_str(), &info) != 0) { throw PythonBackendException( "stat() of : " + canonical_env_path + " returned error."); } else if (S_ISDIR(info.st_mode)) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, ("Returning canonical path since EXECUTION_ENV_PATH does " - "not contain compressed path. Path: " + + "not contain compressed path. Path: " + canonical_env_path) .c_str()); return nullptr; } - std::string& env_key = canonical_env_path; - const auto env_itr = env_map_[env_key]; + const std::string& env_key = canonical_env_path; + auto env_itr = env_map_.find(env_key); std::shared_ptr env; if (env_itr != env_map_.end()) { env = env_itr->second.lock(); @@ -309,8 +309,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) if (!env_extracted) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, - ("Extracting Python execution env " + canonical_env_path) - .c_str()); + ("Extracting Python execution env " + canonical_env_path).c_str()); if (re_extraction) { // Just replace with new environment (by updated source) @@ -323,7 +322,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) env = std::make_shared( canonical_env_path, dst_env_path, last_modified_time); // Add the environment to the list of environments - env_map_.insert({env_key, new_env}); + env_map_.insert({env_key, env}); } } @@ -336,10 +335,9 @@ EnvironmentManager::~EnvironmentManager() } EnvironmentManager::Environment::Environment( - EnvironmentManager& manager, const std::string& source, - const std::string& path, const time_t& last_modified_time) - : manager_(manager), source_(source), path_(path), - last_modified_time_(std::to_string(last_modified_time)) + const std::string& source, const std::string& path, + const time_t& last_modified_time) + : source_(source), path_(path), last_modified_time_(last_modified_time) { Extract(); } @@ -347,12 +345,11 @@ EnvironmentManager::Environment::Environment( void EnvironmentManager::Environment::Extract() { - int status = - mkdir(path_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + int status = mkdir(path_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (status != 0) { throw PythonBackendException( - std::string("Failed to create environment directory for '") + - path_.c_str() + "'."); + "Failed to create environment directory for '" + path_ + + "'."); } ExtractTarFile(source_, path_); } From ce296e662a719aa83e699d7278ad679d0abd6f88 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 14:06:33 +0300 Subject: [PATCH 13/41] deleted unuseful friend in env code --- src/pb_env.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pb_env.h b/src/pb_env.h index 8e2f9b44..1f4b3a6a 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -50,7 +50,6 @@ class EnvironmentManager { public: class Environment { public: - friend class EnvironmentManager; Environment( const std::string& source, const std::string& path, const time_t& last_modified_time); From cb627f553b8b6c0073d0a5f590c5e8a78c62bb17 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 15:39:17 +0300 Subject: [PATCH 14/41] added guard principle to the env code --- src/pb_env.cc | 83 ++++++++++++++++++++++++++++++++------------------- src/pb_env.h | 25 ++++++++++++++-- 2 files changed, 74 insertions(+), 34 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 497fe827..ec03d0b7 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -243,12 +243,9 @@ EnvironmentManager::EnvironmentManager() } -std::shared_ptr +EnvironmentManager::EnvironmentGuard EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) { - // Lock the mutex. Only a single thread should modify the map. - std::lock_guard lk(mutex_); - std::string canonical_env_path = [&] { char canonical_env_path[PATH_MAX + 1]; char* err = realpath(env_path.c_str(), canonical_env_path); @@ -260,12 +257,6 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) return std::string(canonical_env_path); }(); - time_t last_modified_time; - LastModifiedTime(canonical_env_path, &last_modified_time); - - bool env_extracted = false; - bool re_extraction = false; - // If the path is not a conda-packed file, then bypass the extraction process struct stat info; if (stat(canonical_env_path.c_str(), &info) != 0) { @@ -281,21 +272,32 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) return nullptr; } - const std::string& env_key = canonical_env_path; + return EnvironmentGuard(*this, canonical_env_path); +} + +const EnvironmentManager::Environment& +EnvironmentManager::GetEnvironment(const std::string& env_path) +{ + // Lock the mutex. Only a single thread should modify the map. + std::lock_guard lk(mutex_); + + time_t last_modified_time; + LastModifiedTime(env_path, &last_modified_time); + + bool env_extracted = false; + bool re_extraction = false; + + const std::string& env_key = env_path; auto env_itr = env_map_.find(env_key); - std::shared_ptr env; + Environment* env = nullptr; if (env_itr != env_map_.end()) { - env = env_itr->second.lock(); + env = &env_itr->second; // Check if the environment has been modified and would // need to be extracted again (or the current environment has no owners // anymore). - if (env == nullptr) { - // refer to case when env was not loaded - env_map_.erase(env_itr); - env_itr = env_map_.end(); - } else if (env->LastModifiedTime() == last_modified_time) { + if (env->LastModifiedTime() == last_modified_time) { env_extracted = true; } else { // Environment file has been updated. Need to clear @@ -309,7 +311,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) if (!env_extracted) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, - ("Extracting Python execution env " + canonical_env_path).c_str()); + ("Extracting Python execution env " + env_path).c_str()); if (re_extraction) { // Just replace with new environment (by updated source) @@ -319,14 +321,29 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) std::string(base_path_) + "/" + std::to_string(env_path_counter_); ++env_path_counter_; - env = std::make_shared( - canonical_env_path, dst_env_path, last_modified_time); + auto new_env = + Environment(env_path, dst_env_path, last_modified_time); // Add the environment to the list of environments - env_map_.insert({env_key, env}); + insert({env_key, new_env}); + env = &env_map_[env_key] } } - return env; + if (!re_extraction) { + env->AddOwner(); + } + + return *env; +} + +void EnvironmentManager::DropEnvironment(const EnvironmentManager::Environment&) +{ + std::lock_guard lk(mutex_); + + size_t env_owners_counter = env->RemoveOwner(); + if (env_owners_counter == 0) { + env_map_.erase(env_key); + } } EnvironmentManager::~EnvironmentManager() @@ -342,28 +359,24 @@ EnvironmentManager::Environment::Environment( Extract(); } -void -EnvironmentManager::Environment::Extract() +void EnvironmentManager::Environment::Extract() { int status = mkdir(path_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (status != 0) { throw PythonBackendException( - "Failed to create environment directory for '" + path_ + - "'."); + "Failed to create environment directory for '" + path_ + "'."); } ExtractTarFile(source_, path_); } -void -EnvironmentManager::Environment::Update(const time_t& last_modified_time) +void EnvironmentManager::Environment::Update(const time_t& last_modified_time) { Delete(); Extract(); last_modified_time_ = last_modified_time; } -void -EnvironmentManager::Environment::Delete() +void EnvironmentManager::Environment::Delete() { RecursiveDirectoryDelete(path_.c_str()); } @@ -373,6 +386,14 @@ EnvironmentManager::Environment::~Environment() Delete(); } +EnvironmentManager::EnvironmentGuard::EnvironmentGuard( + EnvironmentManager & manager, const std::string& env_path) + : manager_(manager), environment_(GetEnvironment(env_path)) {} + +EnvironmentManager::EnvironmentGuard::~EnvironmentGuard() { + manager_.DropEnvironment(environment_); +} + #endif diff --git a/src/pb_env.h b/src/pb_env.h index 1f4b3a6a..6503bc6f 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -56,6 +56,8 @@ class EnvironmentManager { ~Environment(); void Update(const time_t& last_modified_time); + void AddOwner() { ++owners_counter_; } + size_t RemoveOwner() { return --owners_counter_; } const std::string& Source() const { return source_; } const std::string& Path() const { return path_; } @@ -68,19 +70,36 @@ class EnvironmentManager { std::string source_; std::string path_; time_t last_modified_time_; + + size_t owners_counter_ = 0; }; + class EnvironmentGuard { + public: + EnvironmentGuard(EnvironmentManager& manager, const Environment& environment); + ~EnvironmentGuard(); + const std::string& Path() const { return environment_.Path(); } + + private: + EnvironmentManager& manager_; + const Environment& environment_; + } + EnvironmentManager(); + friend class EnvironmentGuard; // Extracts the tar.gz file in the 'env_path' if it has not been // already extracted. - std::shared_ptr ExtractIfNotExtracted( - const std::string& env_path); + EnvironmentGuard ExtractIfNotExtracted(const std::string& env_path); + ~EnvironmentManager(); private: + void DropEnvironment(const Environment& environment); + const Environment& GetEnvironment(const std::string& env_path); + size_t env_path_counter_ = 0; - std::map> env_map_; + std::map env_map_; char base_path_[PATH_MAX + 1]; std::mutex mutex_; }; From 9527d1c1ec2fbc3ff60d5b027ecd3ab5f608ad81 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 15:43:53 +0300 Subject: [PATCH 15/41] fixed stub launcher after previous remarks --- src/stub_launcher.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stub_launcher.h b/src/stub_launcher.h index 4c9ad139..a2b27403 100644 --- a/src/stub_launcher.h +++ b/src/stub_launcher.h @@ -205,7 +205,7 @@ class StubLauncher { // Path to python execution environment std::string path_to_libpython_; std::string path_to_activate_; - std::shared_ptr python_execution_env_; + EnvironmentManager::EnvironmentGuard python_execution_env_; std::string python_execution_env_source_; common::TritonJson::WriteBuffer model_config_buffer_; From 597c92ddf30b75a812dd4f9b61e4a475bb0aeccc Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 15:50:34 +0300 Subject: [PATCH 16/41] added environment proxy to isolate Environment in EnvironmentManager --- src/pb_env.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/pb_env.h b/src/pb_env.h index 6503bc6f..eff911cc 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -74,16 +74,29 @@ class EnvironmentManager { size_t owners_counter_ = 0; }; + class EnvironmentProxy { + public: + EnvironmentProxy(const Environment& env) : env_(env) {} + ~EnvironmentProxy(); + + const std::string& Source() const { return env_.Source(); } + const std::string& Path() const { return env_.Path(); } + const time_t& LastModifiedTime() const { return env_.LastModifiedTime(); } + + private: + const Environment& env_; + }; + class EnvironmentGuard { public: - EnvironmentGuard(EnvironmentManager& manager, const Environment& environment); + EnvironmentGuard(EnvironmentManager& manager, const std::string& env_path); + EnvironmentProxy operator->() const { return EnvironmentProxy(environment_); } ~EnvironmentGuard(); - const std::string& Path() const { return environment_.Path(); } private: EnvironmentManager& manager_; const Environment& environment_; - } + }; EnvironmentManager(); friend class EnvironmentGuard; From 20f86ebb21bb69951501b170b10690de3f9e0120 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 16:40:21 +0300 Subject: [PATCH 17/41] added numerous compile time fixes to env code --- src/pb_env.cc | 12 +++++++----- src/pb_env.h | 18 +++++++++++------- src/stub_launcher.h | 1 + 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index ec03d0b7..1b129806 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -243,7 +243,7 @@ EnvironmentManager::EnvironmentManager() } -EnvironmentManager::EnvironmentGuard +std::optional EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) { std::string canonical_env_path = [&] { @@ -269,9 +269,10 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) "not contain compressed path. Path: " + canonical_env_path) .c_str()); - return nullptr; + return std::nullopt; } + const auto& env = GetEnvironment(canonical_env_path); return EnvironmentGuard(*this, canonical_env_path); } @@ -387,10 +388,11 @@ EnvironmentManager::Environment::~Environment() } EnvironmentManager::EnvironmentGuard::EnvironmentGuard( - EnvironmentManager & manager, const std::string& env_path) - : manager_(manager), environment_(GetEnvironment(env_path)) {} + EnvironmentManager & manager, const Environment& env) + : manager_(manager), environment_(env) {} -EnvironmentManager::EnvironmentGuard::~EnvironmentGuard() { +EnvironmentManager::EnvironmentGuard::~EnvironmentGuard() +{ manager_.DropEnvironment(environment_); } diff --git a/src/pb_env.h b/src/pb_env.h index eff911cc..509c4cbb 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #ifdef WIN32 @@ -89,21 +90,24 @@ class EnvironmentManager { class EnvironmentGuard { public: - EnvironmentGuard(EnvironmentManager& manager, const std::string& env_path); - EnvironmentProxy operator->() const { return EnvironmentProxy(environment_); } + EnvironmentGuard(EnvironmentManager& manager, const Environment& env); + const Environment* operator->() const { return &environment_; } + const Environment& operator*() const { return environment_; } ~EnvironmentGuard(); - private: - EnvironmentManager& manager_; - const Environment& environment_; + private: + EnvironmentManager& manager_; + const Environment& environment_; }; EnvironmentManager(); friend class EnvironmentGuard; // Extracts the tar.gz file in the 'env_path' if it has not been - // already extracted. - EnvironmentGuard ExtractIfNotExtracted(const std::string& env_path); + // already extracted. Returns nullopt when env_path is an uncompressed + // directory (caller uses that path directly). + std::optional ExtractIfNotExtracted( + const std::string& env_path); ~EnvironmentManager(); diff --git a/src/stub_launcher.h b/src/stub_launcher.h index a2b27403..07c89552 100644 --- a/src/stub_launcher.h +++ b/src/stub_launcher.h @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include From be0d50f13074162e9e0e542bf7a3122857d66960 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 16:46:55 +0300 Subject: [PATCH 18/41] added fixes to stub launcher code --- src/stub_launcher.cc | 6 +++++- src/stub_launcher.h | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index 8af30865..ddc3f096 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -603,7 +603,11 @@ StubLauncher::GetPythonEnvironment(ModelState* model_state) TRITONSERVER_ERROR_INTERNAL, pb_exception.what()); } - std::string python_execution_env_path = python_execution_env_->Path(); + std::string python_execution_env_path = python_execution_env_source; + if (python_execution_env_.has_value()) { + python_execution_env_path = python_execution_env_->Path(); + } + path_to_activate_ = python_execution_env_path + "/bin/activate"; path_to_libpython_ = python_execution_env_path + "/lib"; if (python_execution_env_path.length() > 0 && diff --git a/src/stub_launcher.h b/src/stub_launcher.h index 07c89552..72906798 100644 --- a/src/stub_launcher.h +++ b/src/stub_launcher.h @@ -206,7 +206,7 @@ class StubLauncher { // Path to python execution environment std::string path_to_libpython_; std::string path_to_activate_; - EnvironmentManager::EnvironmentGuard python_execution_env_; + std::optional python_execution_env_; std::string python_execution_env_source_; common::TritonJson::WriteBuffer model_config_buffer_; From 9eb01128f5a39c331ebda15452ceac7898e9ed3b Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 16:52:24 +0300 Subject: [PATCH 19/41] added another fixes to stub launcher code --- src/stub_launcher.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index ddc3f096..080d54d9 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -592,8 +592,8 @@ StubLauncher::Launch() TRITONSERVER_Error* StubLauncher::GetPythonEnvironment(ModelState* model_state) { + auto python_execution_env_source = model_state->PythonExecutionEnv(); try { - auto python_execution_env_source = model_state->PythonExecutionEnv(); python_execution_env_ = model_state->StateForBackend()->env_manager->ExtractIfNotExtracted( python_execution_env_source); @@ -605,7 +605,7 @@ StubLauncher::GetPythonEnvironment(ModelState* model_state) std::string python_execution_env_path = python_execution_env_source; if (python_execution_env_.has_value()) { - python_execution_env_path = python_execution_env_->Path(); + python_execution_env_path = (*python_execution_env)->Path(); } path_to_activate_ = python_execution_env_path + "/bin/activate"; From 70fb4692024653d99ec6a754beb922f5cc2cdacf Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 17:38:27 +0300 Subject: [PATCH 20/41] added another fixes to env code --- src/pb_env.cc | 6 +++--- src/pb_env.h | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 1b129806..36620605 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -273,7 +273,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) } const auto& env = GetEnvironment(canonical_env_path); - return EnvironmentGuard(*this, canonical_env_path); + return EnvironmentGuard(this, &canonical_env_path); } const EnvironmentManager::Environment& @@ -388,8 +388,8 @@ EnvironmentManager::Environment::~Environment() } EnvironmentManager::EnvironmentGuard::EnvironmentGuard( - EnvironmentManager & manager, const Environment& env) - : manager_(manager), environment_(env) {} + EnvironmentManager* manager, const Environment* env) + : manager_(manager), environment_(env), environment_proxy_(env) {} EnvironmentManager::EnvironmentGuard::~EnvironmentGuard() { diff --git a/src/pb_env.h b/src/pb_env.h index 509c4cbb..e2135264 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -90,14 +90,15 @@ class EnvironmentManager { class EnvironmentGuard { public: - EnvironmentGuard(EnvironmentManager& manager, const Environment& env); - const Environment* operator->() const { return &environment_; } - const Environment& operator*() const { return environment_; } + EnvironmentGuard(EnvironmentManager* manager, const Environment* environment); + const EnvironmentProxy* operator->() const { return &environment_proxy_; } + const EnvironmentProxy& operator*() const { return environment_proxy_; } ~EnvironmentGuard(); - private: - EnvironmentManager& manager_; - const Environment& environment_; + private: + EnvironmentManager* manager_; + const Environment* environment_; + EnvironmentProxy environment_proxy_; }; EnvironmentManager(); @@ -106,8 +107,7 @@ class EnvironmentManager { // Extracts the tar.gz file in the 'env_path' if it has not been // already extracted. Returns nullopt when env_path is an uncompressed // directory (caller uses that path directly). - std::optional ExtractIfNotExtracted( - const std::string& env_path); + std::optional ExtractIfNotExtracted(const std::string& env_path); ~EnvironmentManager(); From 15a5cd5e66d5c67827d0ec80a5bce656836af781 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 17:45:04 +0300 Subject: [PATCH 21/41] added another fixes --- src/pb_env.cc | 2 +- src/pb_env.h | 10 +++++----- src/stub_launcher.cc | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 36620605..c238f0f1 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -325,7 +325,7 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) auto new_env = Environment(env_path, dst_env_path, last_modified_time); // Add the environment to the list of environments - insert({env_key, new_env}); + env_map_.insert({env_key, new_env}); env = &env_map_[env_key] } } diff --git a/src/pb_env.h b/src/pb_env.h index e2135264..de70bbb3 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -77,15 +77,15 @@ class EnvironmentManager { class EnvironmentProxy { public: - EnvironmentProxy(const Environment& env) : env_(env) {} + EnvironmentProxy(const Environment* env) : env_(env) {} ~EnvironmentProxy(); - const std::string& Source() const { return env_.Source(); } - const std::string& Path() const { return env_.Path(); } - const time_t& LastModifiedTime() const { return env_.LastModifiedTime(); } + const std::string& Source() const { return env_->Source(); } + const std::string& Path() const { return env_->Path(); } + const time_t& LastModifiedTime() const { return env_->LastModifiedTime(); } private: - const Environment& env_; + const Environment* env_; }; class EnvironmentGuard { diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index 080d54d9..5e8c6519 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -605,7 +605,7 @@ StubLauncher::GetPythonEnvironment(ModelState* model_state) std::string python_execution_env_path = python_execution_env_source; if (python_execution_env_.has_value()) { - python_execution_env_path = (*python_execution_env)->Path(); + python_execution_env_path = (*python_execution_env_)->Path(); } path_to_activate_ = python_execution_env_path + "/bin/activate"; From 9745dfdd66d7ef5878c31d08c48af70f91fbc223 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 17:56:55 +0300 Subject: [PATCH 22/41] added last fixes to the env code --- src/pb_env.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index c238f0f1..cc515181 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -322,11 +322,9 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) std::string(base_path_) + "/" + std::to_string(env_path_counter_); ++env_path_counter_; - auto new_env = - Environment(env_path, dst_env_path, last_modified_time); // Add the environment to the list of environments - env_map_.insert({env_key, new_env}); - env = &env_map_[env_key] + env_itr = env_map_.try_emplace({env_key, env_path, dst_env_path, last_modified_time}); + env = &env_itr->second; } } From 534f20cdce87aff3b91d975c78004f333ed82189 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 18:01:05 +0300 Subject: [PATCH 23/41] added last fixes to the env code (i hope) --- src/pb_env.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index cc515181..29ef45fa 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -323,7 +323,7 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) ++env_path_counter_; // Add the environment to the list of environments - env_itr = env_map_.try_emplace({env_key, env_path, dst_env_path, last_modified_time}); + env_itr = env_map_.try_emplace(env_key, env_path, dst_env_path, last_modified_time); env = &env_itr->second; } } @@ -335,13 +335,13 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) return *env; } -void EnvironmentManager::DropEnvironment(const EnvironmentManager::Environment&) +void EnvironmentManager::DropEnvironment(const Environment& env) { std::lock_guard lk(mutex_); size_t env_owners_counter = env->RemoveOwner(); if (env_owners_counter == 0) { - env_map_.erase(env_key); + env_map_.erase(env.Source()); } } @@ -391,7 +391,7 @@ EnvironmentManager::EnvironmentGuard::EnvironmentGuard( EnvironmentManager::EnvironmentGuard::~EnvironmentGuard() { - manager_.DropEnvironment(environment_); + manager_->DropEnvironment(environment_); } From d7658c5702581224e75b5d21fca9ac0cf771ea64 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 18:10:15 +0300 Subject: [PATCH 24/41] add last fixes to the env code --- src/pb_env.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 29ef45fa..258914b3 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -273,7 +273,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) } const auto& env = GetEnvironment(canonical_env_path); - return EnvironmentGuard(this, &canonical_env_path); + return EnvironmentGuard(this, &env); } const EnvironmentManager::Environment& @@ -339,7 +339,7 @@ void EnvironmentManager::DropEnvironment(const Environment& env) { std::lock_guard lk(mutex_); - size_t env_owners_counter = env->RemoveOwner(); + size_t env_owners_counter = env.RemoveOwner(); if (env_owners_counter == 0) { env_map_.erase(env.Source()); } @@ -391,7 +391,7 @@ EnvironmentManager::EnvironmentGuard::EnvironmentGuard( EnvironmentManager::EnvironmentGuard::~EnvironmentGuard() { - manager_->DropEnvironment(environment_); + manager_->DropEnvironment(*environment_); } From 9104caff76cd006115c8aca9a7cf444023f4f338 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 18:27:15 +0300 Subject: [PATCH 25/41] added i hope last fixes to env code --- src/pb_env.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 258914b3..e4fd73a9 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -323,7 +323,7 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) ++env_path_counter_; // Add the environment to the list of environments - env_itr = env_map_.try_emplace(env_key, env_path, dst_env_path, last_modified_time); + env_itr = env_map_.try_emplace(env_key, env_path, dst_env_path, last_modified_time).first; env = &env_itr->second; } } @@ -335,7 +335,7 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) return *env; } -void EnvironmentManager::DropEnvironment(const Environment& env) +void EnvironmentManager::DropEnvironment(Environment& env) { std::lock_guard lk(mutex_); From a72f9750dd347c5ef9c81bffbbb7c9f768208be2 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 18:40:21 +0300 Subject: [PATCH 26/41] fixed signatures in env code --- src/pb_env.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pb_env.h b/src/pb_env.h index de70bbb3..75fcbffe 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -112,7 +112,7 @@ class EnvironmentManager { ~EnvironmentManager(); private: - void DropEnvironment(const Environment& environment); + void DropEnvironment(Environment& environment); const Environment& GetEnvironment(const std::string& env_path); size_t env_path_counter_ = 0; From 3bf8ae10462b804c66024ca2dd7e03158c03b973 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 18:44:32 +0300 Subject: [PATCH 27/41] added types fixes in the env code --- src/pb_env.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index e4fd73a9..9af25270 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -386,7 +386,7 @@ EnvironmentManager::Environment::~Environment() } EnvironmentManager::EnvironmentGuard::EnvironmentGuard( - EnvironmentManager* manager, const Environment* env) + EnvironmentManager* manager, Environment* env) : manager_(manager), environment_(env), environment_proxy_(env) {} EnvironmentManager::EnvironmentGuard::~EnvironmentGuard() From b58cee81b6a26b2d9a7949ede4f034ca1db477b2 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 18:58:48 +0300 Subject: [PATCH 28/41] fixed types in the env code --- src/pb_env.cc | 4 ++-- src/pb_env.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 9af25270..48995eab 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -272,11 +272,11 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) return std::nullopt; } - const auto& env = GetEnvironment(canonical_env_path); + auto& env = GetEnvironment(canonical_env_path); return EnvironmentGuard(this, &env); } -const EnvironmentManager::Environment& +EnvironmentManager::Environment& EnvironmentManager::GetEnvironment(const std::string& env_path) { // Lock the mutex. Only a single thread should modify the map. diff --git a/src/pb_env.h b/src/pb_env.h index 75fcbffe..41873ca8 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -90,14 +90,14 @@ class EnvironmentManager { class EnvironmentGuard { public: - EnvironmentGuard(EnvironmentManager* manager, const Environment* environment); + EnvironmentGuard(EnvironmentManager* manager, Environment* environment); const EnvironmentProxy* operator->() const { return &environment_proxy_; } const EnvironmentProxy& operator*() const { return environment_proxy_; } ~EnvironmentGuard(); private: EnvironmentManager* manager_; - const Environment* environment_; + Environment* environment_; EnvironmentProxy environment_proxy_; }; @@ -113,7 +113,7 @@ class EnvironmentManager { private: void DropEnvironment(Environment& environment); - const Environment& GetEnvironment(const std::string& env_path); + Environment& GetEnvironment(const std::string& env_path); size_t env_path_counter_ = 0; std::map env_map_; From aa90bd526a07e94cb362c3c45a75bb3db70d440e Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 19:23:27 +0300 Subject: [PATCH 29/41] fixed EnvironmentProxy --- src/pb_env.cc | 2 ++ src/pb_env.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 48995eab..4ee7729b 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -385,6 +385,8 @@ EnvironmentManager::Environment::~Environment() Delete(); } +EnvironmentManager::EnvironmentProxy::~EnvironmentProxy() = default; + EnvironmentManager::EnvironmentGuard::EnvironmentGuard( EnvironmentManager* manager, Environment* env) : manager_(manager), environment_(env), environment_proxy_(env) {} diff --git a/src/pb_env.h b/src/pb_env.h index 41873ca8..c3985f1f 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -78,7 +78,7 @@ class EnvironmentManager { class EnvironmentProxy { public: EnvironmentProxy(const Environment* env) : env_(env) {} - ~EnvironmentProxy(); + ~EnvironmentProxy() {} const std::string& Source() const { return env_->Source(); } const std::string& Path() const { return env_->Path(); } From a1c6a025de86e39aaf95d52f235c6dd81c228fec Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 19:25:59 +0300 Subject: [PATCH 30/41] added fixes to EnvironmentProxy --- src/pb_env.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pb_env.h b/src/pb_env.h index c3985f1f..8db5c494 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -78,7 +78,7 @@ class EnvironmentManager { class EnvironmentProxy { public: EnvironmentProxy(const Environment* env) : env_(env) {} - ~EnvironmentProxy() {} + ~EnvironmentProxy() = default; const std::string& Source() const { return env_->Source(); } const std::string& Path() const { return env_->Path(); } From b6d124c302048538617a1664842fa1522fe5af54 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 19:29:55 +0300 Subject: [PATCH 31/41] added fixes to EnvironmentProxy --- src/pb_env.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 4ee7729b..48995eab 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -385,8 +385,6 @@ EnvironmentManager::Environment::~Environment() Delete(); } -EnvironmentManager::EnvironmentProxy::~EnvironmentProxy() = default; - EnvironmentManager::EnvironmentGuard::EnvironmentGuard( EnvironmentManager* manager, Environment* env) : manager_(manager), environment_(env), environment_proxy_(env) {} From 0fb628bba014a906cc45979d377d4fadb42eecbf Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 20:59:13 +0300 Subject: [PATCH 32/41] fixed logs in the env code --- src/pb_env.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 48995eab..74f2bfb3 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -265,10 +265,9 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) } else if (S_ISDIR(info.st_mode)) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, - ("Returning canonical path since EXECUTION_ENV_PATH does " + "Returning canonical path since EXECUTION_ENV_PATH does " "not contain compressed path. Path: " + - canonical_env_path) - .c_str()); + canonical_env_path); return std::nullopt; } From e270a415a9ec020bad19e7736cc3275a1a234e8e Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 21:09:33 +0300 Subject: [PATCH 33/41] added fixes to lgs in the env code --- src/pb_env.cc | 5 +++-- src/stub_launcher.cc | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 74f2bfb3..48995eab 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -265,9 +265,10 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) } else if (S_ISDIR(info.st_mode)) { LOG_MESSAGE( TRITONSERVER_LOG_VERBOSE, - "Returning canonical path since EXECUTION_ENV_PATH does " + ("Returning canonical path since EXECUTION_ENV_PATH does " "not contain compressed path. Path: " + - canonical_env_path); + canonical_env_path) + .c_str()); return std::nullopt; } diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index 5e8c6519..9aab4e3f 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -614,10 +614,9 @@ StubLauncher::GetPythonEnvironment(ModelState* model_state) !FileExists(path_to_activate_)) { return TRITONSERVER_ErrorNew( TRITONSERVER_ERROR_INTERNAL, - ("Path " + path_to_activate_ + + "Path " + path_to_activate_ + " does not exist. The Python environment should contain an " - "'activate' script.") - .c_str()); + "'activate' script."); } return nullptr; } From d9fbe30fdf6d789b3d27078e642cb353a1620ba4 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 21:20:28 +0300 Subject: [PATCH 34/41] returned logs code in the nev code --- src/stub_launcher.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stub_launcher.cc b/src/stub_launcher.cc index 9aab4e3f..1978afe8 100644 --- a/src/stub_launcher.cc +++ b/src/stub_launcher.cc @@ -614,9 +614,9 @@ StubLauncher::GetPythonEnvironment(ModelState* model_state) !FileExists(path_to_activate_)) { return TRITONSERVER_ErrorNew( TRITONSERVER_ERROR_INTERNAL, - "Path " + path_to_activate_ + + ("Path " + path_to_activate_ + " does not exist. The Python environment should contain an " - "'activate' script."); + "'activate' script.").c_str()); } return nullptr; } From 91697dd0cd72b1aa27d94a6bb813521d3e11730a Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Thu, 14 May 2026 21:30:33 +0300 Subject: [PATCH 35/41] fixed ref counting in the env code --- src/pb_env.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 48995eab..7065bb88 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -328,10 +328,7 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) } } - if (!re_extraction) { - env->AddOwner(); - } - + env->AddOwner(); return *env; } From 3a7ba799aab4a0b48cdf780fdd655cf55d4b95c8 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Fri, 15 May 2026 11:51:52 +0300 Subject: [PATCH 36/41] added some debug logs --- src/pb_env.cc | 105 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 96 insertions(+), 9 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 7065bb88..1ed38fee 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -36,6 +36,7 @@ #include #include #include +#include #include "pb_utils.h" @@ -246,17 +247,26 @@ EnvironmentManager::EnvironmentManager() std::optional EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) { + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] ExtractIfNotExtracted: env_path='" + env_path + "'").c_str()); + std::string canonical_env_path = [&] { char canonical_env_path[PATH_MAX + 1]; char* err = realpath(env_path.c_str(), canonical_env_path); if (err == nullptr) { throw PythonBackendException( - std::string("Failed to get the canonical path for ") + env_path + - "."); + "Failed to get the canonical path for " + env_path + "."); } - return std::string(canonical_env_path); + return canonical_env_path; }(); + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] ExtractIfNotExtracted: canonical_env_path='" + + canonical_env_path + "'") + .c_str()); + // If the path is not a conda-packed file, then bypass the extraction process struct stat info; if (stat(canonical_env_path.c_str(), &info) != 0) { @@ -272,7 +282,22 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) return std::nullopt; } + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] ExtractIfNotExtracted: stat OK, st_mode=" + + std::to_string(static_cast(info.st_mode)) + + " S_ISDIR=0 S_ISREG=" + std::string(S_ISREG(info.st_mode) ? "1" : "0") + + " -> call GetEnvironment then return EnvironmentGuard") + .c_str()); + auto& env = GetEnvironment(canonical_env_path); + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] ExtractIfNotExtracted: GetEnvironment returned " + "Environment& Source='" + + env.Source() + "' Path='" + env.Path() + "' LastModifiedTime=" + + std::to_string(static_cast(env.LastModifiedTime()))) + .c_str()); return EnvironmentGuard(this, &env); } @@ -282,9 +307,23 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) // Lock the mutex. Only a single thread should modify the map. std::lock_guard lk(mutex_); + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] GetEnvironment: enter env_path='" + env_path + + "' env_map_.size()=" + std::to_string(env_map_.size()) + + " base_path_='" + std::string(base_path_) + + "' env_path_counter_=" + std::to_string(env_path_counter_)) + .c_str()); + time_t last_modified_time; LastModifiedTime(env_path, &last_modified_time); + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] GetEnvironment: LastModifiedTime ok mtime=" + + std::to_string(static_cast(last_modified_time))) + .c_str()); + bool env_extracted = false; bool re_extraction = false; @@ -306,6 +345,23 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) // the environment to the same destination directory. re_extraction = true; } + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] GetEnvironment: found existing env env_key='" + env_key + + "' cached_mtime=" + + std::to_string(static_cast(env->LastModifiedTime())) + + " current_mtime=" + + std::to_string(static_cast(last_modified_time)) + + " env_extracted=" + (env_extracted ? "true" : "false") + + " re_extraction=" + (re_extraction ? "true" : "false") + + " cached_Path='" + env->Path() + "'") + .c_str()); + } else { + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] GetEnvironment: no map entry for env_key='" + env_key + + "' env_extracted=false re_extraction=false") + .c_str()); } // Extract only if the env has not been extracted yet. @@ -315,6 +371,11 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) ("Extracting Python execution env " + env_path).c_str()); if (re_extraction) { + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] GetEnvironment: re_extraction Update on Path='" + + env->Path() + "'") + .c_str()); // Just replace with new environment (by updated source) env->Update(last_modified_time); } else { @@ -322,17 +383,38 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) std::string(base_path_) + "/" + std::to_string(env_path_counter_); ++env_path_counter_; + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] GetEnvironment: try_emplace new env env_key='" + env_key + + "' dst_env_path='" + dst_env_path + "' source='" + env_path + + "' mtime=" + std::to_string(last_modified_time)) + .c_str()); + // Add the environment to the list of environments - env_itr = env_map_.try_emplace(env_key, env_path, dst_env_path, last_modified_time).first; + env_itr = + env_map_ + .try_emplace(env_key, env_path, dst_env_path, last_modified_time) + .first; env = &env_itr->second; + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] GetEnvironment: after try_emplace Path='" + env->Path() + + "' Source='" + env->Source() + "'") + .c_str()); } } env->AddOwner(); + LOG_MESSAGE( + TRITONSERVER_LOG_INFO, + ("[pb_env] GetEnvironment: after AddOwner return Path='" + + env->Path() + "' Source='" + env->Source() + "'") + .c_str()); return *env; } -void EnvironmentManager::DropEnvironment(Environment& env) +void +EnvironmentManager::DropEnvironment(Environment& env) { std::lock_guard lk(mutex_); @@ -355,7 +437,8 @@ EnvironmentManager::Environment::Environment( Extract(); } -void EnvironmentManager::Environment::Extract() +void +EnvironmentManager::Environment::Extract() { int status = mkdir(path_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (status != 0) { @@ -365,14 +448,16 @@ void EnvironmentManager::Environment::Extract() ExtractTarFile(source_, path_); } -void EnvironmentManager::Environment::Update(const time_t& last_modified_time) +void +EnvironmentManager::Environment::Update(const time_t& last_modified_time) { Delete(); Extract(); last_modified_time_ = last_modified_time; } -void EnvironmentManager::Environment::Delete() +void +EnvironmentManager::Environment::Delete() { RecursiveDirectoryDelete(path_.c_str()); } @@ -384,7 +469,9 @@ EnvironmentManager::Environment::~Environment() EnvironmentManager::EnvironmentGuard::EnvironmentGuard( EnvironmentManager* manager, Environment* env) - : manager_(manager), environment_(env), environment_proxy_(env) {} + : manager_(manager), environment_(env), environment_proxy_(env) +{ +} EnvironmentManager::EnvironmentGuard::~EnvironmentGuard() { From 06e564658feed79d2062fa795aa2cec97e374c27 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Fri, 15 May 2026 12:05:38 +0300 Subject: [PATCH 37/41] fixed bug with canonical env path --- src/pb_env.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 1ed38fee..1aa1a769 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -258,7 +258,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) throw PythonBackendException( "Failed to get the canonical path for " + env_path + "."); } - return canonical_env_path; + return std::string(canonical_env_path); }(); LOG_MESSAGE( From f97e8f69f8c835f925d417ec2afe6ec0ffcb3c7e Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Fri, 15 May 2026 12:36:58 +0300 Subject: [PATCH 38/41] maybe fixed environment guard --- src/pb_env.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/pb_env.h b/src/pb_env.h index 8db5c494..254feca1 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -91,8 +91,16 @@ class EnvironmentManager { class EnvironmentGuard { public: EnvironmentGuard(EnvironmentManager* manager, Environment* environment); + + EnvironmentGuard(const EnvironmentGuard&) = delete; + EnvironmentGuard(EnvironmentGuard&&) = default; + + EnvironmentGuard& operator=(const EnvironmentGuard&) = delete; + EnvironmentGuard& operator=(EnvironmentGuard&&) = delete; + const EnvironmentProxy* operator->() const { return &environment_proxy_; } const EnvironmentProxy& operator*() const { return environment_proxy_; } + ~EnvironmentGuard(); private: From 163c241ea695ccc434bac1f0b2e90c709fbe7395 Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Fri, 15 May 2026 12:42:19 +0300 Subject: [PATCH 39/41] fixed environment guard --- src/pb_env.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pb_env.h b/src/pb_env.h index 254feca1..ca872e7b 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -96,11 +96,11 @@ class EnvironmentManager { EnvironmentGuard(EnvironmentGuard&&) = default; EnvironmentGuard& operator=(const EnvironmentGuard&) = delete; - EnvironmentGuard& operator=(EnvironmentGuard&&) = delete; + EnvironmentGuard& operator=(EnvironmentGuard&&) = default; const EnvironmentProxy* operator->() const { return &environment_proxy_; } const EnvironmentProxy& operator*() const { return environment_proxy_; } - + ~EnvironmentGuard(); private: From 1c2b2230a465548e089d7bd86f9aee53016952ae Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Fri, 15 May 2026 13:22:25 +0300 Subject: [PATCH 40/41] added fixes with move semantics to the env code --- src/pb_env.cc | 25 +++++++++++++++++++++--- src/pb_env.h | 53 +++++++++++++++++++++++++++++---------------------- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 1aa1a769..8d6ea3d8 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -407,8 +407,8 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) env->AddOwner(); LOG_MESSAGE( TRITONSERVER_LOG_INFO, - ("[pb_env] GetEnvironment: after AddOwner return Path='" + - env->Path() + "' Source='" + env->Source() + "'") + ("[pb_env] GetEnvironment: after AddOwner return Path='" + env->Path() + + "' Source='" + env->Source() + "'") .c_str()); return *env; } @@ -473,9 +473,28 @@ EnvironmentManager::EnvironmentGuard::EnvironmentGuard( { } +EnvironmentManager::EnvironmentGuard::EnvironmentGuard( + EnvironmentGuard&& other_guard) + : manager_(other_guard.manager_), environment_(other_guard.environment_), + environment_proxy_(std::move(other_guard.environment_proxy_)) +{ + other_guard.manager_ = nullptr; + other_guard.environment_ = nullptr; +} + +EnvironmentManager::EnvironmentGuard& +EnvironmentManager::EnvironmentGuard::operator=(EnvironmentGuard&& other_guard) +{ + EnvironmentGuard new_guard(std::move(other_guard)); + std::swap(*this, new_guard); + return *this; +} + EnvironmentManager::EnvironmentGuard::~EnvironmentGuard() { - manager_->DropEnvironment(*environment_); + if (environment_ != nullptr && manager_ != nullptr) { + manager_->DropEnvironment(*environment_); + } } diff --git a/src/pb_env.h b/src/pb_env.h index ca872e7b..0c0f20d0 100644 --- a/src/pb_env.h +++ b/src/pb_env.h @@ -57,7 +57,7 @@ class EnvironmentManager { ~Environment(); void Update(const time_t& last_modified_time); - void AddOwner() { ++owners_counter_; } + void AddOwner() { ++owners_counter_; } size_t RemoveOwner() { return --owners_counter_; } const std::string& Source() const { return source_; } @@ -76,34 +76,40 @@ class EnvironmentManager { }; class EnvironmentProxy { - public: - EnvironmentProxy(const Environment* env) : env_(env) {} - ~EnvironmentProxy() = default; - - const std::string& Source() const { return env_->Source(); } - const std::string& Path() const { return env_->Path(); } - const time_t& LastModifiedTime() const { return env_->LastModifiedTime(); } - - private: - const Environment* env_; - }; + public: + EnvironmentProxy(const Environment* env) : env_(env) {} + + EnvironmentProxy(EnvironmentProxy&& other_proxy) : env_(other_proxy.env_) + { + other_proxy.env_ = nullptr; + } + + ~EnvironmentProxy() = default; + + const std::string& Source() const & { return env_->Source(); } + const std::string& Path() const & { return env_->Path(); } + const time_t& LastModifiedTime() const & { return env_->LastModifiedTime(); } + + private: + const Environment* env_; + }; class EnvironmentGuard { - public: - EnvironmentGuard(EnvironmentManager* manager, Environment* environment); + public: + EnvironmentGuard(EnvironmentManager* manager, Environment* environment); - EnvironmentGuard(const EnvironmentGuard&) = delete; - EnvironmentGuard(EnvironmentGuard&&) = default; + EnvironmentGuard(const EnvironmentGuard&) = delete; + EnvironmentGuard(EnvironmentGuard&&); - EnvironmentGuard& operator=(const EnvironmentGuard&) = delete; - EnvironmentGuard& operator=(EnvironmentGuard&&) = default; + EnvironmentGuard& operator=(const EnvironmentGuard&) = delete; + EnvironmentGuard& operator=(EnvironmentGuard&&); - const EnvironmentProxy* operator->() const { return &environment_proxy_; } - const EnvironmentProxy& operator*() const { return environment_proxy_; } + const EnvironmentProxy* operator->() const { return &environment_proxy_; } + const EnvironmentProxy& operator*() const { return environment_proxy_; } - ~EnvironmentGuard(); + ~EnvironmentGuard(); - private: + private: EnvironmentManager* manager_; Environment* environment_; EnvironmentProxy environment_proxy_; @@ -115,7 +121,8 @@ class EnvironmentManager { // Extracts the tar.gz file in the 'env_path' if it has not been // already extracted. Returns nullopt when env_path is an uncompressed // directory (caller uses that path directly). - std::optional ExtractIfNotExtracted(const std::string& env_path); + std::optional ExtractIfNotExtracted( + const std::string& env_path); ~EnvironmentManager(); From b67e0818c5ba3b3361dd2fba9a453a1ba442df5c Mon Sep 17 00:00:00 2001 From: Denis Kolesnikov Date: Fri, 15 May 2026 15:09:26 +0300 Subject: [PATCH 41/41] removed debug logs from the env code --- src/pb_env.cc | 78 --------------------------------------------------- 1 file changed, 78 deletions(-) diff --git a/src/pb_env.cc b/src/pb_env.cc index 8d6ea3d8..b67b455f 100644 --- a/src/pb_env.cc +++ b/src/pb_env.cc @@ -247,10 +247,6 @@ EnvironmentManager::EnvironmentManager() std::optional EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) { - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] ExtractIfNotExtracted: env_path='" + env_path + "'").c_str()); - std::string canonical_env_path = [&] { char canonical_env_path[PATH_MAX + 1]; char* err = realpath(env_path.c_str(), canonical_env_path); @@ -261,12 +257,6 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) return std::string(canonical_env_path); }(); - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] ExtractIfNotExtracted: canonical_env_path='" + - canonical_env_path + "'") - .c_str()); - // If the path is not a conda-packed file, then bypass the extraction process struct stat info; if (stat(canonical_env_path.c_str(), &info) != 0) { @@ -282,22 +272,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path) return std::nullopt; } - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] ExtractIfNotExtracted: stat OK, st_mode=" + - std::to_string(static_cast(info.st_mode)) + - " S_ISDIR=0 S_ISREG=" + std::string(S_ISREG(info.st_mode) ? "1" : "0") + - " -> call GetEnvironment then return EnvironmentGuard") - .c_str()); - auto& env = GetEnvironment(canonical_env_path); - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] ExtractIfNotExtracted: GetEnvironment returned " - "Environment& Source='" + - env.Source() + "' Path='" + env.Path() + "' LastModifiedTime=" + - std::to_string(static_cast(env.LastModifiedTime()))) - .c_str()); return EnvironmentGuard(this, &env); } @@ -307,23 +282,9 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) // Lock the mutex. Only a single thread should modify the map. std::lock_guard lk(mutex_); - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] GetEnvironment: enter env_path='" + env_path + - "' env_map_.size()=" + std::to_string(env_map_.size()) + - " base_path_='" + std::string(base_path_) + - "' env_path_counter_=" + std::to_string(env_path_counter_)) - .c_str()); - time_t last_modified_time; LastModifiedTime(env_path, &last_modified_time); - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] GetEnvironment: LastModifiedTime ok mtime=" + - std::to_string(static_cast(last_modified_time))) - .c_str()); - bool env_extracted = false; bool re_extraction = false; @@ -345,23 +306,6 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) // the environment to the same destination directory. re_extraction = true; } - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] GetEnvironment: found existing env env_key='" + env_key + - "' cached_mtime=" + - std::to_string(static_cast(env->LastModifiedTime())) + - " current_mtime=" + - std::to_string(static_cast(last_modified_time)) + - " env_extracted=" + (env_extracted ? "true" : "false") + - " re_extraction=" + (re_extraction ? "true" : "false") + - " cached_Path='" + env->Path() + "'") - .c_str()); - } else { - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] GetEnvironment: no map entry for env_key='" + env_key + - "' env_extracted=false re_extraction=false") - .c_str()); } // Extract only if the env has not been extracted yet. @@ -371,11 +315,6 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) ("Extracting Python execution env " + env_path).c_str()); if (re_extraction) { - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] GetEnvironment: re_extraction Update on Path='" + - env->Path() + "'") - .c_str()); // Just replace with new environment (by updated source) env->Update(last_modified_time); } else { @@ -383,33 +322,16 @@ EnvironmentManager::GetEnvironment(const std::string& env_path) std::string(base_path_) + "/" + std::to_string(env_path_counter_); ++env_path_counter_; - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] GetEnvironment: try_emplace new env env_key='" + env_key + - "' dst_env_path='" + dst_env_path + "' source='" + env_path + - "' mtime=" + std::to_string(last_modified_time)) - .c_str()); - // Add the environment to the list of environments env_itr = env_map_ .try_emplace(env_key, env_path, dst_env_path, last_modified_time) .first; env = &env_itr->second; - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] GetEnvironment: after try_emplace Path='" + env->Path() + - "' Source='" + env->Source() + "'") - .c_str()); } } env->AddOwner(); - LOG_MESSAGE( - TRITONSERVER_LOG_INFO, - ("[pb_env] GetEnvironment: after AddOwner return Path='" + env->Path() + - "' Source='" + env->Source() + "'") - .c_str()); return *env; }