Skip to content

Commit 32ab088

Browse files
Liu Caofacebook-github-bot
authored andcommitted
add death-test for “create before registrationComplete()” guard (#2429)
Summary: Pull Request resolved: #2429 # Motivation ------------------ * **Real-world failure case** – An HHVM extension recently crashed because its global `folly::Singleton` objects were first instantiated while the vault was still in the *registration* phase. That path calls `singletonWarnCreateBeforeRegistrationCompleteAndAbort()` and raises SIGABRT. The new test is a minimal repro so anyone investigating similar traces can run `singleton_test` and see the exact behavior. * **Document the API contract** – Folly’s design intentionally treats “create-before-registrationComplete” as a hard logic error. A unit-test makes this behavior explicit and discoverable. If maintainers later decide to soften or eliminate the abort, the failing test will prompt a conscious, reviewed decision instead of a silent behavior change. # What the test does ------------------ 1. Creates a *fresh* vault identified by a private `VaultTag`; a new vault starts in the **REGISTRATION** state. 2. Registers a dummy singleton `<Dummy, Tag, VaultTag>` against that vault. 3. Calls `try_get()` **before** `registrationComplete()` is invoked. 4. Uses `EXPECT_DEATH` to assert that the process aborts with `singletonWarnCreateBeforeRegistrationCompleteAndAbort`. Reviewed By: yfeldblum Differential Revision: D73976074 fbshipit-source-id: eaaff85669c39d4cebb9923f0de3431ea6c19f7f
1 parent 55df811 commit 32ab088

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

folly/Singleton.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
/// Singletons won't be recreated after destroyInstances call. If you
130130
/// want to re-enable singleton creation (say after fork was called) you
131131
/// should call reenableInstances.
132+
///
133+
/// NOTE: Calling `try_get()` **before**
134+
/// `SingletonVault::registrationComplete()` has completed (i.e. before
135+
/// `folly::init()` completed) is a logic error. In this case, `try_get()`
136+
/// will abort via `singletonWarnCreateBeforeRegistrationCompleteAndAbort()`.
132137
/// @class folly::Singleton
133138

134139
#pragma once

folly/test/SingletonTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,3 +1232,18 @@ auto cancelOnDestructionSingleton =
12321232
TEST(Singleton, CancelOnDestruction) {
12331233
cancelOnDestructionSingleton.try_get();
12341234
}
1235+
1236+
TEST(Singleton, CreationBeforeRegistrationCompleteAborts) {
1237+
struct Dummy {};
1238+
struct VaultTag {};
1239+
struct Tag {};
1240+
1241+
folly::SingletonVault* vault = folly::SingletonVault::singleton<VaultTag>();
1242+
std::ignore = vault;
1243+
1244+
static folly::Singleton<Dummy, Tag, VaultTag> lateSingleton;
1245+
1246+
EXPECT_DEATH(
1247+
{ std::ignore = lateSingleton.try_get(); },
1248+
"singletonWarnCreateBeforeRegistrationCompleteAndAbort");
1249+
}

0 commit comments

Comments
 (0)