12

I want to run specific code before quitting when the user hits CTRL-C. The code is in Go and I want to run it on Windows using Git Bash / MINGW64. Using Go, I do

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
// some goroutines get started here
// ...
for {
  select {
  case <-interrupt:
    // code which shall be run on CTRL-C
  }
}

On Windows, this works when I use a Windows command line, but I want it to work on MINGW64/Git Bash as well.

I found on https://stackoverflow.com/a/31974985/1370397 that adding

trap '' SIGINT

to ~/.bashrc traps the SIGINT signal and prevents bash from terminating my program.

This works for me on MINGW32 with bash version

$ bash --version
GNU bash, version 3.1.20(4)-release (i686-pc-msys)
Copyright (C) 2005 Free Software Foundation, Inc.

but it fails to work on MINGW64, bash version

$ bash --version
GNU bash, version 4.3.42(5)-release (x86_64-pc-msys)
Copyright (C) 2013 Free Software Foundation, Inc.
[...]

What's different on MINGW64 or on that new (git) bash version?

For easier testing, here is a minimal example to see the behaviour differences:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    "time"
)

func cleanup(){
    for i:=0; i<3; i++ {
        fmt.Println("Cleaning up...")
        time.Sleep(500*time.Millisecond)
    }
}

func work() {
    for {
        fmt.Println("Working...")
        time.Sleep(300*time.Millisecond)
    }
}

func main() {
    interrupt := make(chan os.Signal, 1)
    signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

    go work()

    for {
        select {
        case <-interrupt:
            fmt.Println("Interrupt received - calling cleanup()...")
            cleanup()
            fmt.Println("Quitting...")
            return
        }
        fmt.Println("Waiting...")
    }
}

Output from MINGW32 (with trap '' SIGINT in ~/.bashrc):

$ ./sigint.exe
Working...
Working...
Working...
Interrupt received - calling cleanup()...
Cleaning up...
Working...
Working...
Cleaning up...
Working...
Cleaning up...
Working...
Working...
Quitting...

The cleanup() code gets executed.

Output from MINGW64 (also with trap '' SIGINT in ~/.bashrc):

$ ./sigint.exe
Working...
Working...
Working...
Working...

cleanup() does not get executed. :-(

Community
  • 1
  • 1
alex
  • 1,870
  • 4
  • 18
  • 29
  • 1
    There seems to be someone with similar setup here https://sourceforge.net/p/mingw-w64/bugs/561/ May be worth following up with Keynan Pratt. There was a mention it may be a GO bug. But from what you've said maybe it is in MINGW64. You should add pointer back to your post here perhaps. – JGFMK Jul 11 '17 at 19:20
  • 1
    Not a Go bug, actually, also happens if you try the very same in C. – Leandros Sep 15 '17 at 23:22

1 Answers1

7

Use winpty to catch signals correctly in Git Bash for Windows. It comes bundled with the installation, so all you need to do is:

$ winpty ./my-program.exe
Phil K
  • 3,745
  • 4
  • 26
  • 48
  • Thanks for this answer, Phil. It works well for me. One thing I have found is that once I use `winpty` at least once, then subsequent invocations of the program will catch signals without me having to explicitly call `winpty`. This is just what I want to happen. – John Jeffery Jun 17 '18 at 01:49
  • So I did "winpty mintty" (mintty is the program that runs git bash window on Windows). It prompts me for which mintty version. It seems still now working for me... – uudaddy Mar 29 '19 at 20:44