Skip to content

Commit 275b9c1

Browse files
whoanBillyONeal
authored andcommitted
Fix thread not joined (#1080)
* Add locks to guarantee asio io_service thread is joined * Use lock_guard instead of unique_lock and add unlock comments.
1 parent 42a6e76 commit 275b9c1

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

Release/src/websockets/client/ws_client_wspp.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -400,23 +400,26 @@ class wspp_callback_client : public websocket_client_callback_impl,
400400

401401
m_state = CONNECTING;
402402
client.connect(con);
403-
m_thread = std::thread([&client]() {
403+
{
404+
std::lock_guard<std::mutex> lock(m_wspp_client_lock);
405+
m_thread = std::thread([&client]() {
404406
#if defined(__ANDROID__)
405-
crossplat::get_jvm_env();
407+
crossplat::get_jvm_env();
406408
#endif
407-
client.run();
409+
client.run();
408410
#if defined(__ANDROID__)
409-
crossplat::JVM.load()->DetachCurrentThread();
411+
crossplat::JVM.load()->DetachCurrentThread();
410412
#endif
411413

412414
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
413-
// OpenSSL stores some per thread state that never will be cleaned up until
414-
// the dll is unloaded. If static linking, like we do, the state isn't cleaned up
415-
// at all and will be reported as leaks.
416-
// See http://www.openssl.org/support/faq.html#PROG13
417-
ERR_remove_thread_state(nullptr);
415+
// OpenSSL stores some per thread state that never will be cleaned up until
416+
// the dll is unloaded. If static linking, like we do, the state isn't cleaned up
417+
// at all and will be reported as leaks.
418+
// See http://www.openssl.org/support/faq.html#PROG13
419+
ERR_remove_thread_state(nullptr);
418420
#endif
419-
});
421+
});
422+
} // unlock
420423
return pplx::create_task(m_connect_tce);
421424
}
422425

@@ -648,10 +651,13 @@ class wspp_callback_client : public websocket_client_callback_impl,
648651

649652
// Can't join thread directly since it is the current thread.
650653
pplx::create_task([this, connecting, ec, closeCode, reason] {
651-
if (m_thread.joinable())
652654
{
653-
m_thread.join();
654-
}
655+
std::lock_guard<std::mutex> lock(m_wspp_client_lock);
656+
if (m_thread.joinable())
657+
{
658+
m_thread.join();
659+
}
660+
} // unlock
655661

656662
// Delete client to make sure Websocketpp cleans up all Boost.Asio portions.
657663
m_client.reset();

0 commit comments

Comments
 (0)