From 8b25084637e1e149d7ff99da71a0fda157c4836a Mon Sep 17 00:00:00 2001 From: Alexey Zatelepin Date: Thu, 22 Nov 2018 20:29:49 +0300 Subject: [PATCH] fix deadlock in ParallelInputsProcessor in case thread creation fails [#CLICKHOUSE-4155] --- .../src/DataStreams/ParallelInputsProcessor.h | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/dbms/src/DataStreams/ParallelInputsProcessor.h b/dbms/src/DataStreams/ParallelInputsProcessor.h index a1c4e2ac480..509522de29a 100644 --- a/dbms/src/DataStreams/ParallelInputsProcessor.h +++ b/dbms/src/DataStreams/ParallelInputsProcessor.h @@ -107,8 +107,26 @@ public: active_threads = max_threads; threads.reserve(max_threads); auto thread_group = CurrentThread::getGroup(); - for (size_t i = 0; i < max_threads; ++i) - threads.emplace_back([=] () { thread(thread_group, i); } ); + + try + { + for (size_t i = 0; i < max_threads; ++i) + threads.emplace_back([=] () { thread(thread_group, i); } ); + } + catch (...) + { + cancel(false); + wait(); + if (active_threads) + { + active_threads = 0; + /// handler.onFinish() is supposed to be called from one of the threads when the number of + /// finished threads reaches max_threads. But since we weren't able to launch all threads, + /// we have to call onFinish() manually here. + handler.onFinish(); + } + throw; + } } /// Ask all sources to stop earlier than they run out.