Skip to content

Commit

Permalink
Fix potential data race in DynamicThreadPool (#4648)
Browse files Browse the repository at this point in the history
close #4595
  • Loading branch information
fuzhe1989 authored Apr 14, 2022
1 parent e192ce5 commit e41c545
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions dbms/src/Common/DynamicThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,30 @@ void DynamicThreadPool::fixedWork(size_t index)

void DynamicThreadPool::dynamicWork(TaskPtr initial_task)
{
UPDATE_CUR_AND_MAX_METRIC(tiflash_thread_count, type_total_threads_of_thdpool, type_max_threads_of_thdpool);
executeTask(initial_task);

DynamicNode node;
while (true)
{
UPDATE_CUR_AND_MAX_METRIC(tiflash_thread_count, type_total_threads_of_thdpool, type_max_threads_of_thdpool);
executeTask(initial_task);

DynamicNode node;
while (true)
{
std::unique_lock lock(dynamic_mutex);
if (in_destructing)
{
std::unique_lock lock(dynamic_mutex);
if (in_destructing)
break;
// attach to just after head to reuse hot threads so that cold threads have chance to exit
node.appendTo(&dynamic_idle_head);
node.cv.wait_for(lock, dynamic_auto_shrink_cooldown);
node.detach();
}

if (!node.task) // may be timeout or cancelled
break;
// attach to just after head to reuse hot threads so that cold threads have chance to exit
node.appendTo(&dynamic_idle_head);
node.cv.wait_for(lock, dynamic_auto_shrink_cooldown);
node.detach();
executeTask(node.task);
}

if (!node.task) // may be timeout or cancelled
break;
executeTask(node.task);
}
// must decrease counter after scope of `UPDATE_CUR_AND_MAX_METRIC`
// to avoid potential data race (#4595)
alive_dynamic_threads.fetch_sub(1);
}

Expand Down

0 comments on commit e41c545

Please sign in to comment.