Skip to content

Commit

Permalink
Make GlobalState ref counted
Browse files Browse the repository at this point in the history
Theoretically, this should prevent Context destructors causing segfaults
if they're destructed after the GlobalState instance.
  • Loading branch information
davisp committed Mar 11, 2025
1 parent 1c2f97a commit fc99837
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
6 changes: 4 additions & 2 deletions tiledb/sm/global_state/global_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/

#include "tiledb/sm/global_state/global_state.h"
#include "tiledb/common/dynamic_memory/dynamic_memory.h"
#include "tiledb/sm/global_state/signal_handlers.h"
#include "tiledb/sm/global_state/watchdog.h"
#include "tiledb/sm/misc/constants.h"
Expand All @@ -47,9 +48,10 @@ using namespace tiledb::common;

namespace tiledb::sm::global_state {

GlobalState& GlobalState::GetGlobalState() {
shared_ptr<GlobalState> GlobalState::GetGlobalState() {
// This is thread-safe in C++11.
static GlobalState globalState;
static shared_ptr<GlobalState> globalState =
tdb::make_shared<GlobalState>(HERE());
return globalState;
}

Expand Down
9 changes: 5 additions & 4 deletions tiledb/sm/global_state/global_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <set>
#include <string>

#include "tiledb/common/common.h"
#include "tiledb/sm/config/config.h"
#include "tiledb/sm/storage_manager/storage_manager_declaration.h"

Expand All @@ -59,8 +60,11 @@ class GlobalState {
GlobalState& operator=(const GlobalState&) = delete;
GlobalState& operator=(const GlobalState&&) = delete;

/** Constructor. */
GlobalState();

/** Returns a reference to the singleton GlobalState instance. */
static GlobalState& GetGlobalState();
static shared_ptr<GlobalState> GetGlobalState();

/**
* Initializes all TileDB global state in an idempotent and threadsafe way.
Expand Down Expand Up @@ -101,9 +105,6 @@ class GlobalState {

/** Mutex protecting list of StorageManagers. */
std::mutex storage_managers_mtx_;

/** Constructor. */
GlobalState();
};

} // namespace global_state
Expand Down
4 changes: 3 additions & 1 deletion tiledb/sm/global_state/watchdog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ void Watchdog::watchdog_thread(Watchdog* watchdog) {
return;
}

auto global_state = GlobalState::GetGlobalState();

while (true) {
std::unique_lock<std::mutex> lck(watchdog->mtx_);
watchdog->cv_.wait_for(
lck, std::chrono::milliseconds(constants::watchdog_thread_sleep_ms));

if (SignalHandlers::signal_received()) {
for (auto* sm : GlobalState::GetGlobalState().storage_managers()) {
for (auto* sm : global_state->storage_managers()) {
throw_if_not_ok(sm->cancel_all_tasks());
}
}
Expand Down
2 changes: 2 additions & 0 deletions tiledb/sm/global_state/watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <mutex>
#include <thread>

#include "tiledb/sm/global_state/global_state.h"

namespace tiledb {
namespace sm {
namespace global_state {
Expand Down
11 changes: 5 additions & 6 deletions tiledb/sm/storage_manager/storage_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ StorageManagerCanonical::StorageManagerCanonical(
ContextResources& resources,
const shared_ptr<Logger>&, // unused
const Config& config)
: vfs_(resources.vfs())
: global_state_(global_state::GlobalState::GetGlobalState())
, vfs_(resources.vfs())
, cancellation_in_progress_(false)
, config_(config)
, queries_in_progress_(0) {
auto& global_state = global_state::GlobalState::GetGlobalState();
global_state.init(config_);

global_state.register_storage_manager(this);
global_state_->init(config_);
global_state_->register_storage_manager(this);
}

StorageManagerCanonical::~StorageManagerCanonical() {
global_state::GlobalState::GetGlobalState().unregister_storage_manager(this);
global_state_->unregister_storage_manager(this);

throw_if_not_ok(cancel_all_tasks());

Expand Down
4 changes: 4 additions & 0 deletions tiledb/sm/storage_manager/storage_manager_canonical.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "tiledb/sm/array/array_directory.h"
#include "tiledb/sm/enums/walk_order.h"
#include "tiledb/sm/filesystem/uri.h"
#include "tiledb/sm/global_state/global_state.h"
#include "tiledb/sm/group/group.h"
#include "tiledb/sm/misc/cancelable_tasks.h"
#include "tiledb/sm/misc/types.h"
Expand Down Expand Up @@ -146,6 +147,9 @@ class StorageManagerCanonical {
/* PRIVATE ATTRIBUTES */
/* ********************************* */

/** The GlobalState to use for this StorageManager. */
shared_ptr<global_state::GlobalState> global_state_;

/**
* VFS instance used in `cancel_all_tasks`.
*/
Expand Down

0 comments on commit fc99837

Please sign in to comment.