Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow OgaShutdown to be called multiple times #1080

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/generators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,29 @@ OrtGlobals::OrtGlobals()
env_->CreateAndRegisterAllocator(allocator_cpu.GetInfo(), *arena_config);
}

static std::unique_ptr<OrtGlobals> g_globals;
static std::mutex g_globals_mutex;

// Ensure Shutdown() has been called before process exit
struct ValidateShutdown {
~ValidateShutdown() {
if (GetOrtGlobals()) {
if (g_globals) {
std::cerr << "OGA Error: Shutdown must be called before process exit, please check the documentation for the proper API to call to ensure clean shutdown." << std::endl;
std::abort();
}
}
};
} g_shutdown; // This struct should stay immediately after the g_globals and g_globals_mutex

std::unique_ptr<OrtGlobals>&
GetOrtGlobals() {
static auto globals = std::make_unique<OrtGlobals>();
static auto validate = std::make_unique<ValidateShutdown>(); // Must be after the above line so the destructor runs before the above destructor
return globals;
// Initialize g_globals using the g_globals_mutex
if (!g_globals) {
std::lock_guard<std::mutex> lock(g_globals_mutex);
if (!g_globals) // Now that we're in the mutex, double check
g_globals = std::make_unique<OrtGlobals>();
}

return g_globals;
}

// Used by Shutdown() to display the counts and types of any leaked objects
Expand All @@ -81,7 +89,7 @@ void Shutdown() {
std::abort();
}

GetOrtGlobals().reset(); // Delete now because on process exit is too late
g_globals.reset();
}

OrtEnv& GetOrtEnv() {
Expand Down Expand Up @@ -313,6 +321,8 @@ bool Generator::IsSessionTerminated() const {
}

void Generator::GenerateNextToken() {
if (search_->IsDone())
throw std::runtime_error("Search is already done, can't generate next token");
ThrowErrorIfSessionTerminated(state_->session_terminated_);
if (!computed_logits_)
throw std::runtime_error("Must call ComputeLogits before GenerateNextToken");
Expand Down
4 changes: 4 additions & 0 deletions test/c_api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ TEST(CAPITests, SetTerminate) {
#endif
}

TEST(CAPITests, Shutdown) {
OgaShutdown(); // Shutdown in the middle, and see if the next test works properly
}

#if TEST_PHI2

struct Phi2Test {
Expand Down
Loading