Skip to content

Commit 31f583d

Browse files
committed
[BugFix][android][ios] fix crash of that when WorkThreadExecutor exec shutdown.
Thread-safe acquisition of worker ownership issue: #44
1 parent bdb7c0d commit 31f583d

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

debug_router/native/socket/work_thread_executor.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void WorkThreadExecutor::submit(std::function<void()> task) {
3434
}
3535

3636
void WorkThreadExecutor::shutdown() {
37+
std::shared_ptr<std::thread> worker_ptr;
3738
{
3839
std::lock_guard<std::mutex> lock(task_mtx);
3940
if (is_shut_down) {
@@ -42,28 +43,27 @@ void WorkThreadExecutor::shutdown() {
4243
is_shut_down = true;
4344
std::queue<std::function<void()>> empty;
4445
tasks.swap(empty);
46+
worker_ptr = std::move(worker); // take ownership of worker
4547
}
4648
cond.notify_all();
4749

48-
if (worker && worker->joinable()) {
50+
if (worker_ptr && worker_ptr->joinable()) {
4951
#if __cpp_exceptions >= 199711L
5052
try {
5153
#endif
52-
if (worker->get_id() != std::this_thread::get_id()) {
53-
worker->join();
54+
if (worker_ptr->get_id() != std::this_thread::get_id()) {
55+
worker_ptr->join();
5456
LOGI("WorkThreadExecutor::shutdown worker->join() success.");
5557
} else {
56-
worker->detach();
58+
worker_ptr->detach();
5759
LOGI("WorkThreadExecutor::shutdown worker->detach() success.");
5860
}
5961
#if __cpp_exceptions >= 199711L
6062
} catch (const std::exception& e) {
61-
LOGE("WorkThreadExecutor::shutdown worker->detach() failed, "
62-
<< e.what());
63+
LOGE("WorkThreadExecutor::shutdown worker->detach() failed, " << e.what());
6364
}
6465
#endif
6566
}
66-
worker.reset();
6767
LOGI("WorkThreadExecutor::shutdown success.");
6868
}
6969

0 commit comments

Comments
 (0)