Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: install sigint handler for run_cli_driver #78

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <sys/types.h>

#include <chrono>
#include <csignal>
#include <iostream>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -592,11 +593,24 @@ void py_unsetup_example(VW::workspace& ws, std::vector<VW::example*>& ex)
for (auto& example : ex) { py_unsetup_example(ws, *example); }
}

// Because of the GIL we can use globals here.
static bool SIGINT_CALLED = false;
static VW::workspace* CLI_DRIVER_WORKSPACE = nullptr;

// return type is an optional error information (nullopt if success), driver output, list of log messages
// stdin is not supported
std::tuple<std::optional<std::string>, std::string, std::vector<std::string>> run_cli_driver(
const std::vector<std::string>& args, bool onethread)
{
SIGINT_CALLED = false;
CLI_DRIVER_WORKSPACE = nullptr;
std::signal(SIGINT,
[](int)
{
if (CLI_DRIVER_WORKSPACE != nullptr) { VW::details::set_done(*CLI_DRIVER_WORKSPACE); }
SIGINT_CALLED = true;
});

auto args_copy = args;
args_copy.push_back("--no_stdin");
auto options = VW::make_unique<VW::config::options_cli>(args_copy);
Expand All @@ -620,18 +634,23 @@ std::tuple<std::optional<std::string>, std::string, std::vector<std::string>> ru
{
auto all = VW::initialize_experimental(std::move(options), nullptr, driver_logger, &driver_log, &logger);
all->vw_is_main = true;
CLI_DRIVER_WORKSPACE = all.get();

if (onethread) { VW::LEARNER::generic_driver_onethread(*all); }
else
// If sigint was called before we got here, we should avoid running the driver.
if (!SIGINT_CALLED)
{
VW::start_parser(*all);
VW::LEARNER::generic_driver(*all);
VW::end_parser(*all);
if (onethread) { VW::LEARNER::generic_driver_onethread(*all); }
else
{
VW::start_parser(*all);
VW::LEARNER::generic_driver(*all);
VW::end_parser(*all);
}

if (all->example_parser->exc_ptr) { std::rethrow_exception(all->example_parser->exc_ptr); }
VW::sync_stats(*all);
all->finish();
}

if (all->example_parser->exc_ptr) { std::rethrow_exception(all->example_parser->exc_ptr); }
VW::sync_stats(*all);
all->finish();
}
catch (const std::exception& ex)
{
Expand All @@ -642,6 +661,8 @@ std::tuple<std::optional<std::string>, std::string, std::vector<std::string>> ru
return std::make_tuple("Unknown exception occurred", driver_log.str(), log_log);
}

SIGINT_CALLED = false;
CLI_DRIVER_WORKSPACE = nullptr;
return std::make_tuple(std::nullopt, driver_log.str(), log_log);
}

Expand Down