Skip to content

Commit 92159e9

Browse files
Moved FfiClient instance to .cpp to ensure only one copy exists owned by the lib, added unit tests
1 parent 9159f3e commit 92159e9

4 files changed

Lines changed: 366 additions & 8 deletions

File tree

src/ffi_client.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ std::optional<FfiClient::AsyncId> ExtractAsyncId(const proto::FfiEvent& event) {
146146

147147
} // namespace
148148

149+
FfiClient& FfiClient::instance() noexcept {
150+
static FfiClient instance;
151+
return instance;
152+
}
153+
149154
// clang-tidy flags this as a trivial destructor in release mode
150155
// due to the assert being pre-processed out
151156
// NOLINTNEXTLINE(modernize-use-equals-default)

src/ffi_client.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ class LIVEKIT_INTERNAL_API FfiClient {
7474
FfiClient(FfiClient&&) = delete;
7575
FfiClient& operator=(FfiClient&&) = delete;
7676

77-
static FfiClient& instance() noexcept {
78-
static FfiClient instance;
79-
return instance;
80-
}
77+
// Defined out-of-line in ffi_client.cpp so the process has exactly one
78+
// FfiClient. An inline definition would, under the SDK's hidden inline
79+
// visibility, produce a separate function-local static in every TU that
80+
// includes this header (notably: in-tree test executables), giving each
81+
// binary its own "singleton" and silently desynchronizing the dylib-side
82+
// and test-exe-side initialization flags.
83+
static FfiClient& instance() noexcept;
8184

8285
// Must be called before any other FFI usage
8386
bool initialize(bool capture_logs);

src/livekit.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@
2222
namespace livekit {
2323

2424
bool initialize(const LogLevel& level, const LogSink& log_sink) {
25+
// Initializes logger if singleton instance is not already initialized
2526
setLogLevel(level);
2627
auto& ffi_client = FfiClient::instance();
2728
return ffi_client.initialize(log_sink == LogSink::kCallback);
2829
}
2930

3031
bool isInitialized() {
31-
auto& ffi_client = FfiClient::instance();
32-
return ffi_client.isInitialized();
32+
return FfiClient::instance().isInitialized();
3333
}
3434

3535
void shutdown() {
36-
auto& ffi_client = FfiClient::instance();
37-
ffi_client.shutdown();
36+
FfiClient::instance().shutdown();
3837
detail::shutdownLogger();
3938
}
4039

0 commit comments

Comments
 (0)