Skip to content

Commit 4131c15

Browse files
committed
Don't dlclose if blas_shutdown not found
1 parent 508efa3 commit 4131c15

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

highs/util/HighsDynamicLibrary.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "util/HighsDynamicLibrary.h"
1313

14+
#include "HConfig.h"
15+
1416
#if defined(_WIN32) || defined(_WIN64)
1517
#ifndef WIN32_LEAN_AND_MEAN
1618
#define WIN32_LEAN_AND_MEAN
@@ -65,23 +67,35 @@ bool HighsDynamicLibrary::load(const std::string& filename,
6567
void HighsDynamicLibrary::unload() {
6668
if (!handle_) return;
6769

68-
#if defined(_WIN32) || defined(_WIN64)
69-
FreeLibrary(static_cast<HMODULE>(handle_));
70-
#else
71-
// check if blas_shutdown exists and potentially use it before dlclose-ing
70+
#ifdef HIPO_USES_OPENBLAS
71+
// Openblas creates a thread pool that may still exist after dlclose-ing the
72+
// library, leading to seg fault. blas_shutdown should prevent this, but its
73+
// symbol is not always exposed. If the symbol exists, use it and then call
74+
// dlclose. If the symbol does not exist, avoid calling dlclose.
7275

7376
using shutdown_t = void (*)();
74-
7577
shutdown_t shutdown_fn = nullptr;
76-
7778
if (auto p = dlsym(handle_, "blas_shutdown"))
7879
shutdown_fn = reinterpret_cast<shutdown_t>(p);
7980
else if (auto p = dlsym(handle_, "openblas_shutdown"))
8081
shutdown_fn = reinterpret_cast<shutdown_t>(p);
8182

82-
if (shutdown_fn) shutdown_fn();
83+
if (shutdown_fn) {
84+
shutdown_fn();
85+
#if defined(_WIN32) || defined(_WIN64)
86+
FreeLibrary(static_cast<HMODULE>(handle_));
87+
#else
88+
dlclose(handle_);
89+
#endif
90+
}
91+
92+
#else
8393

94+
#if defined(_WIN32) || defined(_WIN64)
95+
FreeLibrary(static_cast<HMODULE>(handle_));
96+
#else
8497
dlclose(handle_);
98+
#endif
8599
#endif
86100

87101
handle_ = nullptr;

0 commit comments

Comments
 (0)