alpaca.cpp icon indicating copy to clipboard operation
alpaca.cpp copied to clipboard

Pressing Control+C twice on Windows stops chat

Open ItsPi3141 opened this issue 2 years ago • 4 comments

In alpaca.cpp, ctrl+c is used interrupt text generation and return control to the user. On Windows, it works as expected when ctrl+c is pressed the first time. However, if you were to stop a text generation (press ctrl+c) for the second time, it will exit out of the script.

Is there anyway to fix this? Maybe change ctrl+c to something else like ctrl+d.

ItsPi3141 avatar Mar 24 '23 22:03 ItsPi3141

I second this. I was going to look into it this weekend, but I wasn't planning on doing a pull request since my experience with c++ isn't great. Would prefer if someone more experienced made the change.

Terristen avatar Mar 24 '23 23:03 Terristen

Update. I looked at the code and wrote a fix that should have worked, but it doesn't stop the second CTRL+C from existing the program. My approach assumed that the second CTRL+C was somehow happening due to asynchronous status of the is_interacting bool. I added another variable to check for multiple interrupts but it's not stopping the second one from ending the program. It does, however work, as it takes prevents the first time from exiting if you're in is_interactive == true.

Here was my attempt so maybe someone else can figure it out.

static bool is_interacting = false;
static int sigint_count = 0;

#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
void sigint_handler(int signo) {
    printf(ANSI_COLOR_RESET);
    if (signo == SIGINT) {
        if (is_interacting) {
            sigint_count++;
        } else if (!is_interacting) {
            is_interacting=true;
            sigint_count = 0;
            return;
        }

        
        if(sigint_count > 2)
            _exit(130);

    }
}
#endif

Maybe there's an async issue. Maybe some other code in another thread is getting the interrupt before the handler in chat.cpp. At this point my understanding of C++ runtimes has hit its limit.

Edit: Just also wanted to note I tried SIGBREAK but that didn't work at all. Since the application is piggy-backing off of SIGINT there may not be a graceful way to make this work. Someone would need to insert some other escape handler, like the escape key, to interrupt the generation. I will look into that, but have not done that before in C++.

Terristen avatar Mar 25 '23 02:03 Terristen

I've submitted PR #155 which will change the interrupt key to ESC.

Terristen avatar Mar 25 '23 03:03 Terristen

Ctrl+c is a default terminal interrupt for stopping the current running process, so unless your program is running in a loop that catches interrupts and handles them internally, your shell / terminal will handle it and end the currently focused process.

Happens on Linux, Mac, and Windows as part of the ANSI standards. And ideally it shouldn't be used to interrupt the main loop either, as in cases like this you may double hit the key, accidentally killing the process.

So requesting a different key combo trap like you did would be the best practices.

DrMeo avatar Mar 26 '23 22:03 DrMeo