From: Andrew Kryczka Date: Sat, 7 Aug 2021 02:05:01 +0000 (-0700) Subject: Prevent joining detached thread in ThreadPoolImpl (#8635) X-Git-Tag: v6.24.2~44 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=23ffed9cb7433fb86c094069de8886cf39b10505;p=rocksdb.git 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 --- diff --git a/util/threadpool_imp.cc b/util/threadpool_imp.cc index b6a521714..2e42004a0 100644 --- a/util/threadpool_imp.cc +++ b/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();