Skip to content

Commit 86f1deb

Browse files
eqymalfet
authored andcommitted
Don't call getenv in side threads (#984)
Summary: Calling `getenv` on side threads is dangerous as it can potentially segfault if the main thread is in the middle of setting environment variables: pytorch/pytorch#134596 This PR only calls `getenv` only once during the first call of `isDaemonEnvVarSet()`, which is called from `init`. Pull Request resolved: #984 Reviewed By: sraikund16 Differential Revision: D62152169 Pulled By: malfet fbshipit-source-id: 28dff07cb9775b004580749805b6437dba978eeb Co-authored-by: Nikita Shulga <[email protected]>
1 parent b5c85da commit 86f1deb

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

libkineto/include/Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,4 +502,8 @@ class Config : public AbstractConfig {
502502

503503
constexpr char kUseDaemonEnvVar[] = "KINETO_USE_DAEMON";
504504

505+
#if __linux__
506+
bool isDaemonEnvVarSet();
507+
#endif
508+
505509
} // namespace libkineto

libkineto/src/Config.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,21 @@ Config::Config()
243243
factories->addFeatureConfigs(*this);
244244
}
245245
#if __linux__
246-
enableIpcFabric_ = (getenv(kUseDaemonEnvVar) != nullptr);
246+
enableIpcFabric_ = libkineto::isDaemonEnvVarSet();
247247
#endif
248248
}
249249

250+
#if __linux__
251+
bool isDaemonEnvVarSet() {
252+
static bool rc = [] {
253+
void *ptr = getenv(kUseDaemonEnvVar);
254+
return ptr != nullptr;
255+
}();
256+
return rc;
257+
}
258+
#endif
259+
260+
250261
std::shared_ptr<void> Config::getStaticObjectsLifetimeHandle() {
251262
return configFactories();
252263
}

libkineto/src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void libkineto_init(bool cpuOnly, bool logOnError) {
131131

132132
// Factory to connect to open source daemon if present
133133
#if __linux__
134-
if (getenv(kUseDaemonEnvVar) != nullptr) {
134+
if (libkineto::isDaemonEnvVarSet()) {
135135
LOG(INFO) << "Registering daemon config loader, cpuOnly = "
136136
<< cpuOnly;
137137
DaemonConfigLoader::registerFactory();

0 commit comments

Comments
 (0)