c++windowssignalsmingw-w64mintty

Why does mingw-w64 mintty not signal() my program when I CTRL+C?


MinTTY does not seem to raise a signal to my mingw-w64 program when I hit CTRL+C. In CMD with the same identical program the signal is correctly raised. Why is this?

The program is compiled under msys2 mingw-w64 with g++ -static -static-libstdc++ -std=c++14 -Wall -Wextra -pedantic testan.cpp. In both cases, signal() does not return SIG_ERR so the handler seems to be correctly installed.


code:

#include <chrono>
#include <thread>
#include <iostream>
#include <csignal>
using namespace std;

void signalHandler( int x ) {
    cout << "Interrupt: " << x << endl;
    exit( 123 );
}

int main () {
    if( signal(SIGINT, signalHandler) == SIG_ERR )
        cout << "received SIG_ERR" << endl;

    while( true ) {
        cout << "waiting for CTRL+C" << endl;
        this_thread::sleep_for( 1s );
    }

    return 0;
}

mintty output:

$ ./a.exe
waiting for CTRL+C
waiting for CTRL+C
waiting for CTRL+C

$

CMD output:

C:\Users\Xunie\Desktop\project>a.exe
waiting for CTRL+C
waiting for CTRL+C
Interrupt: 2

C:\Users\Xunie\Desktop\project>

Solution

  • MinTTY is a POSIX-oriented terminal emulator, it's using Cygwin/MSYS2 PTYs which don't interface well with native (non-Cygwin non-MSYS2) programs. This includes signals, detection of interactive input etc. MinTTY doesn't attempt to fix this, but Cygwin has recently (since v3.1.0) improved its support of this use case by using the new ConPTY API. As of May 2020, MSYS2 hasn't yet integrated these changes to its runtime, so you can't see the benefits there yet. In the meantime (and on older Windows versions), you can use the winpty wrapper, installable using pacman.