Prevent joining detached thread in ThreadPoolImpl (#8635)

Summary:
This draining mechanism should not be run during `JoinThreads()` because it can detach threads that will be joined. Joining detached threads would throw an exception.

With this PR, we skip draining when `JoinThreads()` has already decided what threads to `join()`, so the threads will exit naturally once the work queue empties.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/8635

Test Plan: verified it unblocked using `WaitForJobsAndJoinAllThreads()` in https://github.com/facebook/rocksdb/issues/8611.

Reviewed By: riversand963

Differential Revision: D30174587

Pulled By: ajkr

fbshipit-source-id: 144966398a607987e0763c7152a0f653fdbf3c8b
main
Andrew Kryczka 3 years ago committed by Facebook GitHub Bot
parent 87882736ef
commit 23ffed9cb7
  1. 8
      util/threadpool_imp.cc

@ -200,12 +200,12 @@ void ThreadPoolImpl::Impl::BGThread(size_t thread_id) {
queue_.empty()) {
break;
}
}
if (IsLastExcessiveThread(thread_id)) {
} else if (IsLastExcessiveThread(thread_id)) {
// Current thread is the last generated one and is excessive.
// We always terminate excessive thread in the reverse order of
// generation time.
// generation time. But not when `exit_all_threads_ == true`,
// otherwise `JoinThreads()` could try to `join()` a `detach()`ed
// thread.
auto& terminating_thread = bgthreads_.back();
terminating_thread.detach();
bgthreads_.pop_back();

Loading…
Cancel
Save