Commit 69bbfc4b authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook GitHub Bot

Replace after-fork-use DFATAL with ERROR

Differential Revision: D25683616

fbshipit-source-id: 7f199b0e43e0a5a76c4cf16412b22abdc28833ac
parent e124ab8e
...@@ -174,8 +174,13 @@ template <typename T> ...@@ -174,8 +174,13 @@ template <typename T>
void SingletonHolder<T>::destroyInstance() { void SingletonHolder<T>::destroyInstance() {
if (state_.load(std::memory_order_relaxed) == if (state_.load(std::memory_order_relaxed) ==
SingletonHolderState::LivingInChildAfterFork) { SingletonHolderState::LivingInChildAfterFork) {
LOG(DFATAL) << "Attempting to destroy singleton " << type().name() if (vault_.failOnUseAfterFork_) {
<< " in child process after fork"; LOG(DFATAL) << "Attempting to destroy singleton " << type().name()
<< " in child process after fork";
} else {
LOG(ERROR) << "Attempting to destroy singleton " << type().name()
<< " in child process after fork";
}
} }
state_ = SingletonHolderState::Dead; state_ = SingletonHolderState::Dead;
instance_.reset(); instance_.reset();
...@@ -242,8 +247,13 @@ void SingletonHolder<T>::createInstance() { ...@@ -242,8 +247,13 @@ void SingletonHolder<T>::createInstance() {
} }
if (state_.load(std::memory_order_relaxed) == if (state_.load(std::memory_order_relaxed) ==
SingletonHolderState::LivingInChildAfterFork) { SingletonHolderState::LivingInChildAfterFork) {
LOG(DFATAL) << "Attempting to use singleton " << type().name() if (vault_.failOnUseAfterFork_) {
<< " in child process after fork"; LOG(DFATAL) << "Attempting to use singleton " << type().name()
<< " in child process after fork";
} else {
LOG(ERROR) << "Attempting to use singleton " << type().name()
<< " in child process after fork";
}
auto expected = SingletonHolderState::LivingInChildAfterFork; auto expected = SingletonHolderState::LivingInChildAfterFork;
state_.compare_exchange_strong( state_.compare_exchange_strong(
expected, expected,
......
...@@ -522,6 +522,10 @@ class SingletonVault { ...@@ -522,6 +522,10 @@ class SingletonVault {
[[noreturn]] void fireShutdownTimer(); [[noreturn]] void fireShutdownTimer();
void setFailOnUseAfterFork(bool failOnUseAfterFork) {
failOnUseAfterFork_ = failOnUseAfterFork;
}
private: private:
template <typename T> template <typename T>
friend struct detail::SingletonHolder; friend struct detail::SingletonHolder;
...@@ -567,6 +571,7 @@ class SingletonVault { ...@@ -567,6 +571,7 @@ class SingletonVault {
std::atomic<bool> shutdownTimerStarted_{false}; std::atomic<bool> shutdownTimerStarted_{false};
std::chrono::milliseconds shutdownTimeout_{std::chrono::minutes{5}}; std::chrono::milliseconds shutdownTimeout_{std::chrono::minutes{5}};
Synchronized<std::vector<std::string>> shutdownLog_; Synchronized<std::vector<std::string>> shutdownLog_;
bool failOnUseAfterFork_{false};
}; };
// This is the wrapper class that most users actually interact with. // This is the wrapper class that most users actually interact with.
......
...@@ -1069,6 +1069,7 @@ TEST(Singleton, ForkInChild) { ...@@ -1069,6 +1069,7 @@ TEST(Singleton, ForkInChild) {
using SingletonObject = Singleton<ForkObject, PrivateTag, VaultTag>; using SingletonObject = Singleton<ForkObject, PrivateTag, VaultTag>;
auto& vault = *SingletonVault::singleton<VaultTag>(); auto& vault = *SingletonVault::singleton<VaultTag>();
vault.setFailOnUseAfterFork(true);
SingletonObject object; SingletonObject object;
vault.registrationComplete(); vault.registrationComplete();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment