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
This commit is contained in:
mrambacher 2022-02-11 13:19:34 -08:00 committed by Facebook GitHub Bot
parent 5c53b9008f
commit 81ada95bd7
2 changed files with 12 additions and 7 deletions

View file

@ -278,6 +278,9 @@ class ObjectRegistry {
static std::shared_ptr<ObjectRegistry> Default(); static std::shared_ptr<ObjectRegistry> Default();
explicit ObjectRegistry(const std::shared_ptr<ObjectRegistry>& parent) explicit ObjectRegistry(const std::shared_ptr<ObjectRegistry>& parent)
: parent_(parent) {} : parent_(parent) {}
explicit ObjectRegistry(const std::shared_ptr<ObjectLibrary>& library) {
libraries_.push_back(library);
}
std::shared_ptr<ObjectLibrary> AddLibrary(const std::string& id) { std::shared_ptr<ObjectLibrary> AddLibrary(const std::string& id) {
auto library = std::make_shared<ObjectLibrary>(id); auto library = std::make_shared<ObjectLibrary>(id);
@ -498,9 +501,6 @@ class ObjectRegistry {
void Dump(Logger* logger) const; void Dump(Logger* logger) const;
private: private:
explicit ObjectRegistry(const std::shared_ptr<ObjectLibrary>& library) {
libraries_.push_back(library);
}
static std::string ToManagedObjectKey(const std::string& type, static std::string ToManagedObjectKey(const std::string& type,
const std::string& id) { const std::string& id) {
return type + "://" + id; return type + "://" + id;

View file

@ -8,6 +8,7 @@
#include <ctype.h> #include <ctype.h>
#include "logging/logging.h" #include "logging/logging.h"
#include "port/lang.h"
#include "rocksdb/customizable.h" #include "rocksdb/customizable.h"
#include "rocksdb/env.h" #include "rocksdb/env.h"
#include "util/string_util.h" #include "util/string_util.h"
@ -135,14 +136,18 @@ void ObjectLibrary::Dump(Logger *logger) const {
// Returns the Default singleton instance of the ObjectLibrary // Returns the Default singleton instance of the ObjectLibrary
// This instance will contain most of the "standard" registered objects // This instance will contain most of the "standard" registered objects
std::shared_ptr<ObjectLibrary> &ObjectLibrary::Default() { std::shared_ptr<ObjectLibrary> &ObjectLibrary::Default() {
static std::shared_ptr<ObjectLibrary> instance = // Use avoid destruction here so the default ObjectLibrary will not be
std::make_shared<ObjectLibrary>("default"); // statically destroyed and long-lived.
STATIC_AVOID_DESTRUCTION(std::shared_ptr<ObjectLibrary>, instance)
(std::make_shared<ObjectLibrary>("default"));
return instance; return instance;
} }
std::shared_ptr<ObjectRegistry> ObjectRegistry::Default() { std::shared_ptr<ObjectRegistry> ObjectRegistry::Default() {
static std::shared_ptr<ObjectRegistry> instance( // Use avoid destruction here so the default ObjectRegistry will not be
new ObjectRegistry(ObjectLibrary::Default())); // statically destroyed and long-lived.
STATIC_AVOID_DESTRUCTION(std::shared_ptr<ObjectRegistry>, instance)
(std::make_shared<ObjectRegistry>(ObjectLibrary::Default()));
return instance; return instance;
} }