folly
folly copied to clipboard
Coredump when another thread destroy
Thread 2 "async_client_te" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fe59836c700 (LWP 182469)]
0x00000000005441a0 in folly::ThreadLocalPtr<folly::SingletonThreadLocal<folly::RequestContext::StaticContext, folly::RequestContext, folly::detail::DefaultMakefolly::RequestContext::StaticContext, folly::RequestContext>::Wrapper, folly::RequestContext, void>::get (this=0x7fe5988e4230) at /_build/folly/folly/detail/ThreadLocalDetail.h:322
322 /_build/folly/folly/detail/ThreadLocalDetail.h: No such file or directory.
(gdb) bt
#0 0x00000000005441a0 in folly::ThreadLocalPtr<folly::SingletonThreadLocal<folly::RequestContext::StaticContext, folly::RequestContext, folly::detail::DefaultMakefolly::RequestContext::StaticContext, folly::RequestContext>::Wrapper, folly::RequestContext, void>::get (this=0x7fe5988e4230) at /_build/folly/folly/detail/ThreadLocalDetail.h:322
#1 folly::ThreadLocal<folly::SingletonThreadLocal<folly::RequestContext::StaticContext, folly::RequestContext, folly::detail::DefaultMakefolly::RequestContext::StaticContext, folly::RequestContext>::Wrapper, folly::RequestContext, void>::get (this=0x7fe5988e4230) at /_build/folly/folly/ThreadLocal.h:69
#2 folly::ThreadLocal<folly::SingletonThreadLocal<folly::RequestContext::StaticContext, folly::RequestContext, folly::detail::DefaultMakefolly::RequestContext::StaticContext, folly::RequestContext>::Wrapper, folly::RequestContext, void>::operator* (this=0x7fe5988e4230) at /_build/folly/folly/ThreadLocal.h:78
#3 folly::SingletonThreadLocal<folly::RequestContext::StaticContext, folly::RequestContext, folly::detail::DefaultMakefolly::RequestContext::StaticContext, folly::RequestContext>::getWrapper ()
at /_build/folly/folly/SingletonThreadLocal.h:138
#4 0x00000000005442c1 in folly::SingletonThreadLocal<folly::RequestContext::StaticContext, folly::RequestContext, folly::detail::DefaultMakefolly::RequestContext::StaticContext, folly::RequestContext>::LocalLifetime::~LocalLifetime (this=0x7fe598364fa0, __in_chrg=
Soruce Code:
AsyncClient::AsyncClient(const std::string& ip, int port, int64_t t_out_ms) { redisOptions options = {0}; REDIS_OPTIONS_SET_TCP(&options, "127.0.0.1", 6379); struct timeval tv = {0}; tv.tv_sec = t_out_ms / 1000; tv.tv_usec = (t_out_ms % 1000) * 1000; options.connect_timeout = &tv; async_ch_ = redisAsyncConnectWithOptions(&options);
if (async_ch_ == nullptr || async_ch_->err) { std::cout << "connection error! \n"; redisAsyncFree(async_ch_); }
base_ = event_base_new(); redisLibeventAttach(async_ch_, base_); poll_ = std::thread([&]() { std::cout << "[Thread] libevent: " << std::this_thread::get_id() << std::endl; auto ret = event_base_dispatch(this->base_); std::cout << "[State] libvent stop \n"; }); }
AsyncClient::~AsyncClient() { redisAsyncDisconnect(async_ch_); if (poll_.joinable()) poll_.join(); }
void AsyncClient::GetCallBack(redisAsyncContext* c, void* r, void* data) { redisReply* resp = static_cast<redisReply*>(r); auto p = std::unique_ptr<folly::Promise<GetResponse>>( static_cast<folly::Promise<GetResponse>*>(data)); std::cout << "[Thread] GetCallBak: " << std::this_thread::get_id() << std::endl; if (resp == nullptr) { p->setValue(GetResponse(Status::NotFound(), std::string(""))); return; } p->setValue(GetResponse(Status::OK(), std::string(resp->str))); }
folly::Future<GetResponse> AsyncClient::Get(const std::string& key) { auto p = std::make_unique<folly::Promise<GetResponse>>(); folly::Future<GetResponse> f = p->getFuture(); std::cout << "[Thread] FutureGet: " << std::this_thread::get_id() << std::endl; std::unique_lockstd::mutex l(wlk_); redisAsyncCommand(async_ch_, GetCallBack, p.release(), "GET %b", key.c_str(), key.size()); return f; }
Test code: int main() { AsyncClient apool("127.0.0.7", 6379); auto f = apool.Set("folly_a", "async_test_value"); std::cout << "[Result] set : " << std::move(f).get().IsOK() << std::endl; auto r = apool.Get("folly_a"); std::cout << "[Result] get: " << std::move(r).get().data << std::endl;
return 0; }