Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coredump when another thread destroy #2002

Open
liujp opened this issue May 24, 2023 · 0 comments
Open

Coredump when another thread destroy #2002

liujp opened this issue May 24, 2023 · 0 comments

Comments

@liujp
Copy link

liujp commented May 24, 2023

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=) at /_build/folly/folly/SingletonThreadLocal.h:128
#5 0x00000000005e8e56 in (anonymous namespace)::run(void*) ()
#6 0x00007fe59ed43ca2 in __nptl_deallocate_tsd () from /lib64/libpthread.so.0
#7 0x00007fe59ed43eb3 in start_thread () from /lib64/libpthread.so.0
#8 0x00007fe59d8da9fd in clone () from /lib64/libc.so.6

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>(
static_cast<folly::Promise*>(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 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;
}

@liujp liujp changed the title Coredump when another thread join Coredump when another thread destroy May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant