Skip to content
Closed
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
37 changes: 21 additions & 16 deletions tools/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
#include <array>
#include <atomic>
#include <algorithm>
#include <csignal>
#include <filesystem>
#include <fstream>
#include <thread>
#include <signal.h>

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
# define NOMINMAX
#endif
#include <io.h>
#include <stdio.h>
#include <windows.h>
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#endif

const char * LLAMA_ASCII_LOGO = R"(
Expand All @@ -37,19 +41,26 @@ const char * LLAMA_ASCII_LOGO = R"(

static std::atomic<bool> g_is_interrupted = false;
static bool should_stop() {
return g_is_interrupted.load();
return g_is_interrupted.load(std::memory_order_acquire);
}
static void reset_stop() {
g_is_interrupted.store(false, std::memory_order_release);
}

#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
static void signal_handler(int) {
if (g_is_interrupted.load()) {
const bool already_interrupted = g_is_interrupted.exchange(true, std::memory_order_acq_rel);
if (already_interrupted) {
// second Ctrl+C - exit immediately
// make sure to clear colors before exiting (not using LOG or console.cpp here to avoid deadlock)
fprintf(stdout, "\033[0m\n");
fflush(stdout);
std::exit(130);
static constexpr char color_reset[] = "\033[0m\n";
#if defined(_WIN32)
_write(_fileno(stdout), color_reset, sizeof(color_reset) - 1);
#else
[[maybe_unused]] ssize_t ret = write(STDOUT_FILENO, color_reset, sizeof(color_reset) - 1);
#endif
_exit(128 + SIGINT);
}
g_is_interrupted.store(true);
}
#endif

Expand All @@ -60,9 +71,6 @@ struct cli_context {
task_params defaults;
bool verbose_prompt;

// thread for showing "loading" animation
std::atomic<bool> loading_show;

cli_context(const common_params & params) {
defaults.sampling = params.sampling;
defaults.speculative = params.speculative;
Expand Down Expand Up @@ -150,10 +158,7 @@ struct cli_context {
std::string curr_content;
bool is_thinking = false;

while (result) {
if (should_stop()) {
break;
}
while (result && !should_stop()) {
if (result->is_error()) {
json err_data = result->to_json();
if (err_data.contains("message")) {
Expand Down Expand Up @@ -195,7 +200,7 @@ struct cli_context {
}
result = rd.next(should_stop);
}
g_is_interrupted.store(false);
reset_stop();
// server_response_reader automatically cancels pending tasks upon destruction
return curr_content;
}
Expand Down Expand Up @@ -527,7 +532,7 @@ int llama_cli(int argc, char ** argv) {
console::log("\n");

if (should_stop()) {
g_is_interrupted.store(false);
reset_stop();
break;
}

Expand Down