Skip to content

Commit

Permalink
Add STATIC_AVOID_DESTRUCTION for ObjectLibrary/Registry (facebook#9464)
Browse files Browse the repository at this point in the history
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: facebook#9464

Reviewed By: pdillinger

Differential Revision: D33849876

Pulled By: mrambacher

fbshipit-source-id: 7a69177d7c58c81be293fc7ef8e600d47ddbc14b
  • Loading branch information
mrambacher authored and facebook-github-bot committed Feb 11, 2022
1 parent 5c53b90 commit 81ada95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 3 additions & 3 deletions include/rocksdb/utilities/object_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ class ObjectRegistry {
static std::shared_ptr<ObjectRegistry> Default();
explicit ObjectRegistry(const std::shared_ptr<ObjectRegistry>& parent)
: parent_(parent) {}
explicit ObjectRegistry(const std::shared_ptr<ObjectLibrary>& library) {
libraries_.push_back(library);
}

std::shared_ptr<ObjectLibrary> AddLibrary(const std::string& id) {
auto library = std::make_shared<ObjectLibrary>(id);
Expand Down Expand Up @@ -498,9 +501,6 @@ class ObjectRegistry {
void Dump(Logger* logger) const;

private:
explicit ObjectRegistry(const std::shared_ptr<ObjectLibrary>& library) {
libraries_.push_back(library);
}
static std::string ToManagedObjectKey(const std::string& type,
const std::string& id) {
return type + "://" + id;
Expand Down
13 changes: 9 additions & 4 deletions utilities/object_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <ctype.h>

#include "logging/logging.h"
#include "port/lang.h"
#include "rocksdb/customizable.h"
#include "rocksdb/env.h"
#include "util/string_util.h"
Expand Down Expand Up @@ -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> &ObjectLibrary::Default() {
static std::shared_ptr<ObjectLibrary> instance =
std::make_shared<ObjectLibrary>("default");
// Use avoid destruction here so the default ObjectLibrary will not be
// statically destroyed and long-lived.
STATIC_AVOID_DESTRUCTION(std::shared_ptr<ObjectLibrary>, instance)
(std::make_shared<ObjectLibrary>("default"));
return instance;
}

std::shared_ptr<ObjectRegistry> ObjectRegistry::Default() {
static std::shared_ptr<ObjectRegistry> 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<ObjectRegistry>, instance)
(std::make_shared<ObjectRegistry>(ObjectLibrary::Default()));
return instance;
}

Expand Down

0 comments on commit 81ada95

Please sign in to comment.