apple / apple/foundationdb

segment fault under C/C++ API binding

Open
#12,359 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
1.6k
Avg merge
1d 20h
Merged PRs (30d)
126

Description

I am using C API (libfdb_c.so) in my C++ application:

1. I created an Singleton method for the `FDBCppBinding`.

```
static std::shared_ptr GetInstance(FDBConfig config) {
static std::shared_ptr instance(new FDBCppBinding(std::move(config)));
TLOG(INFO, "FDBCppBinding instance returned");
return instance;
}
```

2. Here's how I init the `FDBCppBinding`:
```
fdb_error_t error = fdb_select_api_version(FDB_API_VERSION);
if (error != 0) {
TLOG(ERROR, "Failed to select FDB API version {}: {}", FDB_API_VERSION, error);
return FDBError::kUnknownError;
}
TLOG(INFO, "Selected FDB API version: {}", FDB_API_VERSION);

setNetworkOptions();

error = fdb_setup_network();
if (error != 0) {
TLOG(ERROR, "FDB Network setup failed: {}", error);
return FDBError::kNetworkError;
}
LOG_INFO("FDB Network setup completed");

startNetworkThread();
```

3. Inside the `startNetworkThread`
```
network_future_ = std::async(std::launch::async, [this]() {
network_running_ = true;
TLOG(INFO, "Starting global FDB network thread");
auto error = fdb_run_network();
network_running_.store(false);
TLOG(INFO, "FDB network thread exited with error: {}", error);
});
```

The problem is, when I exit the UT of the application, there's an segment error:

```
Missing separate debuginfos, use: dnf debuginfo-install foundationdb-clients-7.1.38-1.x86_64 glibc-2.38-29.tl4.x86_64 libgcc-12.3.1.5-2.tl4.x86_64 libstdc++-12.3.1.5-2.tl4.x86_64 numactl-devel-2.0.16-5.tl4.x86_64
(gdb) bt
#0 0x00000000047bb6c0 in ?? ()
#1 0x00007ffff69d576d in Transaction::createTrLogInfoProbabilistically(Database const&) () from /lib64/libfdb_c.so
#2 0x00007ffff69fbe86 in Transaction::Transaction(Database const&, Optional > const&) ()
from /lib64/libfdb_c.so
#3 0x00007ffff6d8772c in ReadYourWritesTransaction::ReadYourWritesTransaction(Database const&, Optional >) () from /lib64/libfdb_c.so
#4 0x00007ffff6fcdd78 in internal_thread_helper::DoOnMainThreadVoidActorState >)::{lambda()#1}, internal_thread_helper::DoOnMainThreadVoidActor >)::{lambda()#1}> >::a_body1cont1(Void const&, int) [clone .constprop.0] [clone .isra.0] () from /lib64/libfdb_c.so
#5 0x00007ffff6fcde68 in ActorCallback >)::{lambda()#1}>, 0, Void>::fire(Void const&) () from /lib64/libfdb_c.so
#6 0x00007ffff715c3f8 in N2::Net2::run() () from /lib64/libfdb_c.so
#7 0x00007ffff69a12c2 in runNetwork() () from /lib64/libfdb_c.so
#8 0x00007ffff6fce1bf in ThreadSafeApi::runNetwork() () from /lib64/libfdb_c.so
#9 0x00007ffff6948f91 in MultiVersionApi::runNetwork() () from /lib64/libfdb_c.so
#10 0x00007ffff691e9fa in fdb_run_network () from /lib64/libfdb_c.so
#11 0x0000000001fccef0 in operator() (__closure=0x47a4a68)
at /data00/kuankuan/tcqa-table/src/ms/fdb/fdb_cpp_binding.cc:569
#12 std::__invoke_impl > (__f=...)
at /usr/include/c++/12/bits/invoke.h:61
#13 std::__invoke > (__fn=...)
```

**FDB version: 7.1.38 (And I've tried 7.3.59)**

Contributor guide

Open the contributing guide

Research direction

Start with fdb_cpp_binding.cc at line 569 and trace the fdb_run_network entry point through the reported backtrace. Reproduce the unit-test exit and inspect the network thread's shutdown path; done means the exit no longer produces the reported segmentation fault and the lifecycle behavior is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
api, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.