my application uses a logging pipeline, so requirement is to handle runtime panic gracefully like sednding a message on logging pipeline to declare the app going down, so that rule engine can work on it.
having said the goal, i followed go doc for signal (https://golang.org/src/os/signal/doc.go) it mentions about SIGBUS, SIGFPE, and SIGSEGV but not clearly mention about how to catch them in code. So i tried with go test and signal handler in app but it does not get called. Is there a way to achieve the goal.
func sigHandler1(exitChannel, syncChannel chan bool) {
c := make(chan os.Signal, 1)
signal.Notify(c)
go func() {
for {
// Block until a signal is received.
s := <-c
mlog.Info(" got signal1: %+v", s)
switch s {
// kill -SIGHUP XXXX
case syscall.SIGHUP, syscall.SIGQUIT,syscall.SIGINT, syscall.SIGTERM:
exitChannel <- true
case syscall.SIGSEGV:
syncChannel <-true
default:
log.Println("Unknown signal.")
}
}
}()
}
func Test() {
var msgBuf []byte
log.Println("Test going to generate panic")
msgBuf[0] = 1
}
panic result are as follows.
panic: runtime error: index out of range [recovered]
panic: runtime error: index out of range
goroutine 10 [running]:
panic(0x515de0, 0xc420010150)
/usr/local/Cellar/go/1.7.5/libexec/src/runtime/panic.go:500 +0x1a1
testing.tRunner.func1(0xc420092300)
/usr/local/Cellar/go/1.7.5/libexec/src/testing/testing.go:579 +0x25d
panic(0x515de0, 0xc420010150)
/usr/local/Cellar/go/1.7.5/libexec/src/runtime/panic.go:458 +0x243
app.Test()
app/main.go:142 +0x56
app.TestMHProcessDlAck_22(0xc420092300)
app.mh_test.go:946 +0x68
testing.tRunner(0xc420092300, 0x60bc90)
/usr/local/Cellar/go/1.7.5/libexec/src/testing/testing.go:610 +0x81
created by testing.(*T).Run
/usr/local/Cellar/go/1.7.5/libexec/src/testing/testing.go:646 +0x2ec
exit status 2
FAIL app 0.085s