You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by je...@apache.org on 2022/03/09 15:43:06 UTC

[incubator-mxnet] branch v1.9.x updated: Avoid modifying loaded library map while iterating in lib_close() (#20941) (#20944)

This is an automated email from the ASF dual-hosted git repository.

jevans pushed a commit to branch v1.9.x
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/v1.9.x by this push:
     new 32cef8e  Avoid modifying loaded library map while iterating in lib_close() (#20941) (#20944)
32cef8e is described below

commit 32cef8ec18b667a884b512706125b8487021bac6
Author: Joe Evans <jo...@gmail.com>
AuthorDate: Wed Mar 9 07:38:41 2022 -0800

    Avoid modifying loaded library map while iterating in lib_close() (#20941) (#20944)
    
    * Update close libs to not modify map while iterating over opened libraries, rename loaded_libs to loaded_libs_ to signify it is a private member.
    
    * Clean up and simplify library close code.
    
    * Fix clang-format.
---
 src/initialize.cc | 22 +++++++---------------
 src/initialize.h  |  4 ++--
 2 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/src/initialize.cc b/src/initialize.cc
index e06f905..0c094c5 100644
--- a/src/initialize.cc
+++ b/src/initialize.cc
@@ -97,7 +97,7 @@ LibraryInitializer::LibraryInitializer()
 }
 
 bool LibraryInitializer::lib_is_loaded(const std::string& path) const {
-  return loaded_libs.count(path) > 0;
+  return loaded_libs_.count(path) > 0;
 }
 
 /*!
@@ -127,9 +127,9 @@ void* LibraryInitializer::lib_load(const char* path) {
     }
 #endif  // _WIN32 or _WIN64 or __WINDOWS__
     // then store the pointer to the library
-    loaded_libs[path] = handle;
+    loaded_libs_[path] = handle;
   } else {
-    handle = loaded_libs.at(path);
+    handle = loaded_libs_.at(path);
   }
   return handle;
 }
@@ -138,15 +138,7 @@ void* LibraryInitializer::lib_load(const char* path) {
  * \brief Closes the loaded dynamic shared library file
  * \param handle library file handle
  */
-void LibraryInitializer::lib_close(void* handle) {
-  std::string libpath;
-  for (const auto& l : loaded_libs) {
-    if (l.second == handle) {
-      libpath = l.first;
-      break;
-    }
-  }
-  CHECK(!libpath.empty());
+void LibraryInitializer::lib_close(void* handle, const std::string& libpath) {
 #if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
   FreeLibrary((HMODULE)handle);
 #else
@@ -155,7 +147,6 @@ void LibraryInitializer::lib_close(void* handle) {
         << " loaded from: '" << libpath << "': " << dlerror();
   }
 #endif  // _WIN32 or _WIN64 or __WINDOWS__
-  loaded_libs.erase(libpath);
 }
 
 /*!
@@ -230,9 +221,10 @@ void LibraryInitializer::install_signal_handlers() {
 }
 
 void LibraryInitializer::close_open_libs() {
-  for (const auto& l : loaded_libs) {
-    lib_close(l.second);
+  for (const auto& l : loaded_libs_) {
+    lib_close(l.second, l.first);
   }
+  loaded_libs_.clear();
 }
 
 /**
diff --git a/src/initialize.h b/src/initialize.h
index 04e2322..05574fb 100644
--- a/src/initialize.h
+++ b/src/initialize.h
@@ -67,7 +67,7 @@ class LibraryInitializer {
   // Library loading
   bool lib_is_loaded(const std::string& path) const;
   void* lib_load(const char* path);
-  void lib_close(void* handle);
+  void lib_close(void* handle, const std::string& libpath);
   static void get_sym(void* handle, void** func, char* name);
 
   /**
@@ -102,7 +102,7 @@ class LibraryInitializer {
 
   void close_open_libs();
 
-  loaded_libs_t loaded_libs;
+  loaded_libs_t loaded_libs_;
 };
 
 /*!