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
This commit is contained in:
Andrew Kryczka 2021-08-06 19:05:01 -07:00 committed by Facebook GitHub Bot
parent 87882736ef
commit 23ffed9cb7

View File

@ -200,12 +200,12 @@ void ThreadPoolImpl::Impl::BGThread(size_t thread_id) {
queue_.empty()) { queue_.empty()) {
break; break;
} }
} } else if (IsLastExcessiveThread(thread_id)) {
if (IsLastExcessiveThread(thread_id)) {
// Current thread is the last generated one and is excessive. // Current thread is the last generated one and is excessive.
// We always terminate excessive thread in the reverse order of // 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(); auto& terminating_thread = bgthreads_.back();
terminating_thread.detach(); terminating_thread.detach();
bgthreads_.pop_back(); bgthreads_.pop_back();