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

Pressing Control+C twice on Windows stops chat #150

Open
ItsPi3141 opened this issue Mar 24, 2023 · 4 comments
Open

Pressing Control+C twice on Windows stops chat #150

ItsPi3141 opened this issue Mar 24, 2023 · 4 comments

Comments

@ItsPi3141
Copy link

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.

@Terristen
Copy link

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
Copy link

Terristen commented Mar 25, 2023

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
Copy link

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

@DrMeo
Copy link

DrMeo commented Mar 26, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants