Skip to content

Commit

Permalink
[SharedLibrary] Fix the problem that throw cannot be executed normall…
Browse files Browse the repository at this point in the history
…y after unload
  • Loading branch information
zhenzew authored and Zhenze Wang committed Oct 9, 2024
1 parent 9ee9631 commit 2c9bdf9
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions onnxruntime/core/session/provider_bridge_ort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1524,24 +1524,33 @@ struct ProviderLibrary {

Provider& Get() {
std::lock_guard<std::mutex> lock{mutex_};
try {
if (!provider_) {
s_library_shared.Ensure();

auto full_path = Env::Default().GetRuntimePath() + filename_;
ORT_THROW_IF_ERROR(Env::Default().LoadDynamicLibrary(full_path, false, &handle_));

Provider* (*PGetProvider)();
ORT_THROW_IF_ERROR(Env::Default().GetSymbolFromLibrary(handle_, "GetProvider", (void**)&PGetProvider));

provider_ = PGetProvider();
provider_->Initialize();
if (!provider_) {
auto is_exception = 0;
std::runtime_error new_e("unknown"); // for rethrow
{
try {
s_library_shared.Ensure();

auto full_path = Env::Default().GetRuntimePath() + filename_;
ORT_THROW_IF_ERROR(Env::Default().LoadDynamicLibrary(full_path, false, &handle_));

Provider* (*PGetProvider)();
ORT_THROW_IF_ERROR(Env::Default().GetSymbolFromLibrary(handle_, "GetProvider", (void**)&PGetProvider));

provider_ = PGetProvider();
provider_->Initialize();
} catch (const std::exception& e) {
// e is constructed in handle_'s dll and needs to be destroyed before unload
new_e = std::runtime_error(e.what());
is_exception = 1;
}
}
if (is_exception) {
Unload(); // If anything fails we unload the library and rethrow
throw new_e;
}
return *provider_;
} catch (const std::exception&) {
Unload(); // If anything fails we unload the library and rethrow
throw;
}
return *provider_;
}

void Unload() {
Expand Down

0 comments on commit 2c9bdf9

Please sign in to comment.