Skip to content

Commit d5d2687

Browse files
popoaichuiniuRovicnowYoyi
authored andcommitted
[BugFix][android][ios] fix crash of that when WorkThreadExecutor exec shutdown.
Thread-safe acquisition of worker ownership issue: lynx-family#44
1 parent bdb7c0d commit d5d2687

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

debug_router/native/socket/work_thread_executor.cc

Lines changed: 6 additions & 5 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,18 +43,19 @@ 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
@@ -63,7 +65,6 @@ void WorkThreadExecutor::shutdown() {
6365
}
6466
#endif
6567
}
66-
worker.reset();
6768
LOGI("WorkThreadExecutor::shutdown success.");
6869
}
6970

0 commit comments

Comments
 (0)