The following C++ program that uses the C client API seg faults
due to a stack overflow:
#include <cstring>
#include <iostream>
#include <mutex>
#include <zookeeper/zookeeper.h>
zhandle_t *h = nullptr;
clientid_t id;
std::mutex m;
void watcher(zhandle_t *zh, int type, int state, const char *path,
void *watcherCtx) {
if ((type == ZOO_SESSION_EVENT) && (state == ZOO_CONNECTED_STATE)) {
m.unlock(); // notify on successful connect
}
}
int main(int argc, char *argv[]) {
zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
std::memset(&id, 0, sizeof(id));
m.lock(); // initialize to locked state
h = zookeeper_init("127.0.0.1:2181", watcher, 30000, &id, nullptr, 0);
if (h == nullptr) {
std::cerr << "got null handle" << std::endl;
return 1;
}
m.lock(); // wait for watcher
std::cout << "connected" << std::endl;
int ret = zookeeper_close(h);
std::cout << "close returned " << ret << std::endl;
return 0;
}
Running it in gdb, I see that it gets into an infinite recursion
between zookeeper_close() and api_epilog(), as shown below:
(gdb) bt
#0 zookeeper_close (zh=zh@entry=0x603e80) at src/zookeeper.c:2477
#1 0x00007ffff7dcdde8 in api_epilog (zh=0x603e80, rc=0)
at src/zookeeper.c:1779
#2 0x00007ffff7dcdc54 in zookeeper_close (zh=zh@entry=0x603e80)
at src/zookeeper.c:2496
#3 0x00007ffff7dcdde8 in api_epilog (zh=0x603e80, rc=0)
at src/zookeeper.c:1779
#4 0x00007ffff7dcdc4a in zookeeper_close (zh=0x603e80) at
src/zookeeper.c:2493
#5 0x0000000000400e15 in main ()
(gdb)
I am using Fedora 19 x86_64, and have Zookeeper 3.4.6 running
locally. Also I have version 3.4.5-6 of the Zookeeper client
library installed. Does anyone have ideas about what may be
causing this problem?
Thanks,
Dave