Search code examples
c++visual-studiosignalswindows-subsystem-for-linux

WSL + Visual Studio - can't debug signals properly


I'm debugging this code under Visual Studio (not visual studio code):

#include <cstdio>
#include <signal.h>
#include <unistd.h>

int n = 0;

void handleSignal(int sig) {
//it works from terminal but not from IDE
    printf("Signal received: %d - %d times\n", sig ,++n);
}


int main()
{
    printf("Hi From WSL! - PID: %d\n", getpid());
    auto s = signal(SIGUSR1, handleSignal);

    printf("signal was %d\n", s); // prints 0

    while (n<10) {
        printf("sleeping - %d\n", n);
        sleep(1);    
    }
    printf("Received signals %d timeskill , exiting\n", n);
    return 0;
}

I run

kill -10 <pid of process>

If I run the code from bash everithing rns fine

# output if run from bash:
$ ./LinuxConsoleApplicationWsl.out
Hi From WSL! - PID: 4953
signal was 0
sleeping - 0
sleeping - 0
sleeping - 0
sleeping - 0
sleeping - 0
sleeping - 0
sleeping - 0
sleeping - 0
sleeping - 0
Signal received: 10 - 1 times
sleeping - 1
sleeping - 1
Signal received: 10 - 2 times
sleeping - 2
Signal received: 10 - 3 times
sleeping - 3
Signal received: 10 - 4 times
sleeping - 4
Signal received: 10 - 5 times
sleeping - 5
Signal received: 10 - 6 times
sleeping - 6
Signal received: 10 - 7 times
sleeping - 7
sleeping - 7
sleeping - 7
Signal received: 10 - 8 times
sleeping - 8
Signal received: 10 - 9 times
sleeping - 9
Signal received: 10 - 10 times
Received signals 10 timeskill , exiting

(do printfs and at 10th signal sent the app exits) But if I run it from visual studio i got this unhandled exception:

unhandled exception

What am I missing?


Solution

  • My own answer: updating VS to a version 17.6.5 fixed the issue, now the signal is debuggable.

    As a note I report that the "exception" is still notified as such but stepping with de debugger works.