This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If the proxy fails due to an internal error or if it receives a fatal signal, write a stack trace to the log and exit. If it was started with `--debug` also dump core. Note: `handleVersion()` introduced to keep `gocyclo` happy. Fixes #59. Signed-off-by: James O. D. Hunt <[email protected]>
- Loading branch information
1 parent
baf8b9e
commit 203aac5
Showing
2 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2018 Intel Corporation. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"runtime/pprof" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
// List of fatal signals | ||
var sigFatal = map[syscall.Signal]bool{ | ||
syscall.SIGABRT: true, | ||
syscall.SIGBUS: true, | ||
syscall.SIGILL: true, | ||
syscall.SIGQUIT: true, | ||
syscall.SIGSEGV: true, | ||
syscall.SIGSTKFLT: true, | ||
syscall.SIGSYS: true, | ||
syscall.SIGTRAP: true, | ||
} | ||
|
||
func handlePanic() { | ||
r := recover() | ||
|
||
if r != nil { | ||
msg := fmt.Sprintf("%s", r) | ||
logger().WithField("panic", msg).Error("fatal error") | ||
|
||
die() | ||
} | ||
} | ||
|
||
func backtrace() { | ||
profiles := pprof.Profiles() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
for _, p := range profiles { | ||
// The magic number requests a full stacktrace. See | ||
// https://golang.org/pkg/runtime/pprof/#Profile.WriteTo. | ||
pprof.Lookup(p.Name()).WriteTo(buf, 2) | ||
} | ||
|
||
for _, line := range strings.Split(buf.String(), "\n") { | ||
logger().Error(line) | ||
} | ||
} | ||
|
||
func fatalSignal(sig syscall.Signal) bool { | ||
return sigFatal[sig] | ||
} | ||
|
||
func fatalSignals() []syscall.Signal { | ||
var signals []syscall.Signal | ||
|
||
for sig := range sigFatal { | ||
signals = append(signals, sig) | ||
|
||
} | ||
|
||
return signals | ||
} | ||
|
||
func die() { | ||
backtrace() | ||
|
||
if crashOnError { | ||
signal.Reset(syscall.SIGABRT) | ||
syscall.Kill(0, syscall.SIGABRT) | ||
} | ||
|
||
os.Exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters