From 81ada95bd72e8b5d8c73c4405afd20680f73b0e1 Mon Sep 17 00:00:00 2001 From: mrambacher Date: Fri, 11 Feb 2022 13:19:34 -0800 Subject: [PATCH] Add STATIC_AVOID_DESTRUCTION for ObjectLibrary/Registry (#9464) Summary: This change should guarantee that the default ObjectLibrary/Registry are long-lived and not destroyed while the process is running. This will prevent some issues of them being referenced after they were destroyed via the static destruction. Pull Request resolved: https://github.com/facebook/rocksdb/pull/9464 Reviewed By: pdillinger Differential Revision: D33849876 Pulled By: mrambacher fbshipit-source-id: 7a69177d7c58c81be293fc7ef8e600d47ddbc14b --- include/rocksdb/utilities/object_registry.h | 6 +++--- utilities/object_registry.cc | 13 +++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/rocksdb/utilities/object_registry.h b/include/rocksdb/utilities/object_registry.h index 9730ed1b1..b6751cae8 100644 --- a/include/rocksdb/utilities/object_registry.h +++ b/include/rocksdb/utilities/object_registry.h @@ -278,6 +278,9 @@ class ObjectRegistry { static std::shared_ptr Default(); explicit ObjectRegistry(const std::shared_ptr& parent) : parent_(parent) {} + explicit ObjectRegistry(const std::shared_ptr& library) { + libraries_.push_back(library); + } std::shared_ptr AddLibrary(const std::string& id) { auto library = std::make_shared(id); @@ -498,9 +501,6 @@ class ObjectRegistry { void Dump(Logger* logger) const; private: - explicit ObjectRegistry(const std::shared_ptr& library) { - libraries_.push_back(library); - } static std::string ToManagedObjectKey(const std::string& type, const std::string& id) { return type + "://" + id; diff --git a/utilities/object_registry.cc b/utilities/object_registry.cc index 7b0a6bb5a..61820620f 100644 --- a/utilities/object_registry.cc +++ b/utilities/object_registry.cc @@ -8,6 +8,7 @@ #include #include "logging/logging.h" +#include "port/lang.h" #include "rocksdb/customizable.h" #include "rocksdb/env.h" #include "util/string_util.h" @@ -135,14 +136,18 @@ void ObjectLibrary::Dump(Logger *logger) const { // Returns the Default singleton instance of the ObjectLibrary // This instance will contain most of the "standard" registered objects std::shared_ptr &ObjectLibrary::Default() { - static std::shared_ptr instance = - std::make_shared("default"); + // Use avoid destruction here so the default ObjectLibrary will not be + // statically destroyed and long-lived. + STATIC_AVOID_DESTRUCTION(std::shared_ptr, instance) + (std::make_shared("default")); return instance; } std::shared_ptr ObjectRegistry::Default() { - static std::shared_ptr instance( - new ObjectRegistry(ObjectLibrary::Default())); + // Use avoid destruction here so the default ObjectRegistry will not be + // statically destroyed and long-lived. + STATIC_AVOID_DESTRUCTION(std::shared_ptr, instance) + (std::make_shared(ObjectLibrary::Default())); return instance; }