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>
void SingletonHolder<T>::destroyInstance() {
if (state_.load(std::memory_order_relaxed) ==
SingletonHolderState::LivingInChildAfterFork) {
LOG(DFATAL) << "Attempting to destroy singleton " << type().name()
<< " in child process after fork";
if (vault_.failOnUseAfterFork_) {
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;
instance_.reset();
......@@ -242,8 +247,13 @@ void SingletonHolder<T>::createInstance() {
}
if (state_.load(std::memory_order_relaxed) ==
SingletonHolderState::LivingInChildAfterFork) {
LOG(DFATAL) << "Attempting to use singleton " << type().name()
<< " in child process after fork";
if (vault_.failOnUseAfterFork_) {
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;
state_.compare_exchange_strong(
expected,
......
......@@ -522,6 +522,10 @@ class SingletonVault {
[[noreturn]] void fireShutdownTimer();
void setFailOnUseAfterFork(bool failOnUseAfterFork) {
failOnUseAfterFork_ = failOnUseAfterFork;
}
private:
template <typename T>
friend struct detail::SingletonHolder;
......@@ -567,6 +571,7 @@ class SingletonVault {
std::atomic<bool> shutdownTimerStarted_{false};
std::chrono::milliseconds shutdownTimeout_{std::chrono::minutes{5}};
Synchronized<std::vector<std::string>> shutdownLog_;
bool failOnUseAfterFork_{false};
};
// This is the wrapper class that most users actually interact with.
......
......@@ -1069,6 +1069,7 @@ TEST(Singleton, ForkInChild) {
using SingletonObject = Singleton<ForkObject, PrivateTag, VaultTag>;
auto& vault = *SingletonVault::singleton<VaultTag>();
vault.setFailOnUseAfterFork(true);
SingletonObject object;
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