Skip to content

Commit 195874e

Browse files
committed
Fix drop environment mismatch bug
1 parent 8cf2408 commit 195874e

3 files changed

Lines changed: 36 additions & 37 deletions

File tree

src/pb_env.cc

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -242,54 +242,43 @@ EnvironmentManager::EnvironmentManager()
242242
std::string
243243
EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path)
244244
{
245-
std::string canonical_env_path = [&] {
246-
char canonical_env_path[PATH_MAX + 1];
247-
char* err = realpath(env_path.c_str(), canonical_env_path);
248-
if (err == nullptr) {
249-
throw PythonBackendException(
250-
"Failed to get the canonical path for " + env_path + ".");
251-
}
252-
return std::string(canonical_env_path);
253-
}();
254-
255245
// If the path is not a conda-packed file, then bypass the extraction process
256246
struct stat info;
257-
if (stat(canonical_env_path.c_str(), &info) != 0) {
247+
if (stat(env_path.c_str(), &info) != 0) {
258248
throw PythonBackendException(
259-
"stat() of : " + canonical_env_path + " returned error.");
249+
"stat() of : " + env_path + " returned error.");
260250
} else if (S_ISDIR(info.st_mode)) {
261251
LOG_MESSAGE(
262252
TRITONSERVER_LOG_VERBOSE,
263-
("Returning canonical path since EXECUTION_ENV_PATH does "
253+
("Returning path since EXECUTION_ENV_PATH does "
264254
"not contain compressed path. Path: " +
265-
canonical_env_path)
255+
env_path)
266256
.c_str());
267-
return canonical_env_path;
257+
return env_path;
268258
}
269259

270260
// Lock the mutex. Only a single thread should modify the map.
271261
std::lock_guard<std::mutex> lk(mutex_);
272262

273263
time_t last_modified_time;
274-
LastModifiedTime(canonical_env_path, &last_modified_time);
264+
LastModifiedTime(env_path, &last_modified_time);
275265

276-
auto env_itr = env_map_.find(canonical_env_path);
266+
auto env_itr = env_map_.find(env_path);
277267
// Extract only if the env has not been extracted yet.
278268
if (env_itr == env_map_.end()) {
279269
LOG_MESSAGE(
280270
TRITONSERVER_LOG_VERBOSE,
281-
("Extracting Python execution env " + canonical_env_path).c_str());
271+
("Extracting Python execution env " + env_path).c_str());
282272

283273
std::string dst_env_path =
284274
std::string(base_path_) + "/" + std::to_string(env_path_counter_);
285275
++env_path_counter_;
286276

287277
// Add the environment to the list of environments.
288-
env_itr = env_map_
289-
.try_emplace(
290-
canonical_env_path, canonical_env_path, dst_env_path,
291-
last_modified_time)
292-
.first;
278+
env_itr =
279+
env_map_
280+
.try_emplace(env_path, env_path, dst_env_path, last_modified_time)
281+
.first;
293282
} else {
294283
Environment& env = env_itr->second;
295284

@@ -298,7 +287,7 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path)
298287
if (env.LastModifiedTime() != last_modified_time) {
299288
LOG_MESSAGE(
300289
TRITONSERVER_LOG_VERBOSE,
301-
("Re-extracting Python execution env " + canonical_env_path).c_str());
290+
("Re-extracting Python execution env " + env_path).c_str());
302291
// Environment file has been updated. Need to clear
303292
// the previously extracted environment and extract
304293
// the environment to the same destination directory.
@@ -313,36 +302,33 @@ EnvironmentManager::ExtractIfNotExtracted(const std::string& env_path)
313302

314303
LOG_MESSAGE(
315304
TRITONSERVER_LOG_VERBOSE,
316-
("Successfully extracted Python execution env " + canonical_env_path)
317-
.c_str());
305+
("Successfully extracted Python execution env " + env_path).c_str());
318306

319307
return env.Destination();
320308
}
321309

322310
void
323-
EnvironmentManager::DropEnvironment(const std::string& canonical_env_path)
311+
EnvironmentManager::DropEnvironment(const std::string& env_path)
324312
{
325313
LOG_MESSAGE(
326314
TRITONSERVER_LOG_VERBOSE,
327-
("Trying to drop Python execution env " + canonical_env_path).c_str());
315+
("Trying to drop Python execution env " + env_path).c_str());
328316

329317
std::lock_guard<std::mutex> lk(mutex_);
330318

331-
auto env_itr = env_map_.find(canonical_env_path);
319+
auto env_itr = env_map_.find(env_path);
332320
if (env_itr != env_map_.end()) {
333321
if (env_itr->second.DecrementRefCount() == 0) {
334322
env_map_.erase(env_itr);
335323
LOG_MESSAGE(
336324
TRITONSERVER_LOG_VERBOSE,
337-
("Successfully dropped Python execution env " + canonical_env_path)
338-
.c_str());
325+
("Successfully dropped Python execution env " + env_path).c_str());
339326
}
340327
} else {
341328
LOG_MESSAGE(
342-
TRITONSERVER_LOG_VERBOSE,
343-
("The environment with the key '" + canonical_env_path +
344-
"' is not presented the env_map")
345-
.c_str());
329+
TRITONSERVER_LOG_VERBOSE, ("The environment with the key '" + env_path +
330+
"' is not presented the env_map")
331+
.c_str());
346332
}
347333
}
348334

src/pb_env.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ class EnvironmentManager {
8282
~EnvironmentManager();
8383

8484
// Decrement the refcount for the environment identified by
85-
// canonical_env_path. If the refcount reaches zero, the environment is
85+
// env_path. If the refcount reaches zero, the environment is
8686
// removed from the map.
87-
void DropEnvironment(const std::string& canonical_env_path);
87+
void DropEnvironment(const std::string& env_path);
8888

8989
private:
9090
size_t env_path_counter_ = 0;

src/stub_launcher.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ StubLauncher::Initialize(ModelState* model_state)
106106
// are supported.
107107
if (python_execution_env_ != "") {
108108
#ifndef _WIN32
109+
// Resolve symlinks to avoid duplicate environment entries.
110+
char canonical_env_path[PATH_MAX + 1];
111+
char* err = realpath(python_execution_env_.c_str(), canonical_env_path);
112+
if (err == nullptr) {
113+
return TRITONSERVER_ErrorNew(
114+
TRITONSERVER_ERROR_INTERNAL,
115+
("Failed to get the canonical path for " + python_execution_env_ +
116+
".")
117+
.c_str());
118+
}
119+
120+
python_execution_env_ = canonical_env_path;
121+
109122
RETURN_IF_ERROR(GetPythonEnvironment(model_state));
110123
#else
111124
return TRITONSERVER_ErrorNew(

0 commit comments

Comments
 (0)