[C++][Acero] If `initial_task` in Acero plan throws an exception the plan hangs indefinitely
Describe the bug, including details regarding any error messages, version, and platform.
This can easily be reproduced by creating an ExecPlan with an ExecNode that throws an exception during StartProducing. The easiest for this to appear is a SourceNode with a generator that throws and does not await anything.
I believe that this is the relevant code section:
/Users/ataeppe/.conan2/p/b/arrowacf6fafc6086d/b/src/cpp/src/arrow/util/async_util.cc:462-473:
Future<> AsyncTaskScheduler::Make(FnOnce<Status(AsyncTaskScheduler*)> initial_task,
FnOnce<void(const Status&)> abort_callback,
StopToken stop_token) {
util::tracing::Span span;
auto scope = START_SCOPED_SPAN_SV(span, "AsyncTaskScheduler::InitialTask"sv);
auto scheduler = std::make_unique<AsyncTaskSchedulerImpl>(std::move(stop_token),
std::move(abort_callback));
Status initial_task_st = std::move(initial_task)(scheduler.get());
scheduler->OnTaskFinished(std::move(initial_task_st));
// Keep scheduler alive until finished
return scheduler->OnFinished().Then([scheduler = std::move(scheduler)] {});
}
The problem arises when initial_task throws an exception. In an ExecPlan the lambda initial_task invokes StartProducing on all nodes.
In debug mode a throw triggers this assert:
~AsyncTaskSchedulerImpl() {
DCHECK_EQ(running_tasks_, 0) << " scheduler destroyed while tasks still running";
}
In release mode the arrow plan will never be able to destruct as a StopProducing is not enough to make the future returned by finished return anymore.
This is a minimal reproducable example:
#include <arrow/acero/exec_plan.h>
#include <arrow/acero/options.h>
#include <gtest/gtest.h>
using arrow::acero::SourceNodeOptions;
using arrow::acero::AsyncExecBatchGenerator;
using arrow::acero::Declaration;
TEST(ReproduceHangingProblem, Test){
AsyncExecBatchGenerator generator = []() -> arrow::Future<std::optional<arrow::ExecBatch>>{
throw std::runtime_error("some exception");
};
auto schema = std::make_shared<arrow::Schema>(arrow::FieldVector{});
SourceNodeOptions source_options{schema, generator};
Declaration dec("source", source_options);
auto result = DeclarationToTable(dec);
}
Component(s)
C++
Issue resolved by pull request 49860 https://github.com/apache/arrow/pull/49860
@egolearner Can you post on this issue so that we can assign it to you?
Ah, great that this was resolved in such a simple fashion! I initially thought that this could also be solvable by tweaking the destructor's in a way such that they clean up / decrement running_tasks_ appropriately and the stack unwind of the exception will already be enough to circumvent the "never finishing Acero plan". But an explicit try ... catch is of course also viable.
Note that a bad_alloc can also trigger this hang, which can be thrown by the standard library. So to guard against this in user code as briefly suggested in #49860, would essentially require compiling with -fno-exceptions in any project using acero
/assign