also handle SIGPIPE in cleanup routines
fixes gh-1413: restic fails to cleanup locks when bash pipeline fails
This commit is contained in:
parent
f39f7c76dd
commit
c347431907
1 changed files with 11 additions and 9 deletions
|
@ -12,27 +12,29 @@ import (
|
||||||
|
|
||||||
var cleanupHandlers struct {
|
var cleanupHandlers struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
list []func() error
|
list []func() error
|
||||||
done bool
|
done bool
|
||||||
sigintCh chan os.Signal
|
ch chan os.Signal
|
||||||
}
|
}
|
||||||
|
|
||||||
var stderr = os.Stderr
|
var stderr = os.Stderr
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cleanupHandlers.sigintCh = make(chan os.Signal)
|
cleanupHandlers.ch = make(chan os.Signal)
|
||||||
go CleanupHandler(cleanupHandlers.sigintCh)
|
go CleanupHandler(cleanupHandlers.ch)
|
||||||
InstallSignalHandler()
|
InstallSignalHandler()
|
||||||
}
|
}
|
||||||
|
|
||||||
// InstallSignalHandler listens for SIGINT and triggers the cleanup handlers.
|
// InstallSignalHandler listens for SIGINT and SIGPIPE, and triggers the cleanup handlers.
|
||||||
func InstallSignalHandler() {
|
func InstallSignalHandler() {
|
||||||
signal.Notify(cleanupHandlers.sigintCh, syscall.SIGINT)
|
signal.Notify(cleanupHandlers.ch, syscall.SIGINT)
|
||||||
|
signal.Notify(cleanupHandlers.ch, syscall.SIGPIPE)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SuspendSignalHandler removes the signal handler for SIGINT.
|
// SuspendSignalHandler removes the signal handler for SIGINT and SIGPIPE.
|
||||||
func SuspendSignalHandler() {
|
func SuspendSignalHandler() {
|
||||||
signal.Reset(syscall.SIGINT)
|
signal.Reset(syscall.SIGINT)
|
||||||
|
signal.Reset(syscall.SIGPIPE)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddCleanupHandler adds the function f to the list of cleanup handlers so
|
// AddCleanupHandler adds the function f to the list of cleanup handlers so
|
||||||
|
@ -67,7 +69,7 @@ func RunCleanupHandlers() {
|
||||||
cleanupHandlers.list = nil
|
cleanupHandlers.list = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CleanupHandler handles the SIGINT signal.
|
// CleanupHandler handles the SIGINT and SIGPIPE signals.
|
||||||
func CleanupHandler(c <-chan os.Signal) {
|
func CleanupHandler(c <-chan os.Signal) {
|
||||||
for s := range c {
|
for s := range c {
|
||||||
debug.Log("signal %v received, cleaning up", s)
|
debug.Log("signal %v received, cleaning up", s)
|
||||||
|
|
Loading…
Reference in a new issue