From c3474319078c73d5a67b37181d962a86ad1dd781 Mon Sep 17 00:00:00 2001 From: George Armhold Date: Sun, 5 Nov 2017 07:14:27 -0500 Subject: [PATCH 1/2] also handle SIGPIPE in cleanup routines fixes gh-1413: restic fails to cleanup locks when bash pipeline fails --- cmd/restic/cleanup.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cmd/restic/cleanup.go b/cmd/restic/cleanup.go index 09559595b..208effdbc 100644 --- a/cmd/restic/cleanup.go +++ b/cmd/restic/cleanup.go @@ -12,27 +12,29 @@ import ( var cleanupHandlers struct { sync.Mutex - list []func() error - done bool - sigintCh chan os.Signal + list []func() error + done bool + ch chan os.Signal } var stderr = os.Stderr func init() { - cleanupHandlers.sigintCh = make(chan os.Signal) - go CleanupHandler(cleanupHandlers.sigintCh) + cleanupHandlers.ch = make(chan os.Signal) + go CleanupHandler(cleanupHandlers.ch) InstallSignalHandler() } -// InstallSignalHandler listens for SIGINT and triggers the cleanup handlers. +// InstallSignalHandler listens for SIGINT and SIGPIPE, and triggers the cleanup handlers. 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() { signal.Reset(syscall.SIGINT) + signal.Reset(syscall.SIGPIPE) } // AddCleanupHandler adds the function f to the list of cleanup handlers so @@ -67,7 +69,7 @@ func RunCleanupHandlers() { cleanupHandlers.list = nil } -// CleanupHandler handles the SIGINT signal. +// CleanupHandler handles the SIGINT and SIGPIPE signals. func CleanupHandler(c <-chan os.Signal) { for s := range c { debug.Log("signal %v received, cleaning up", s) From 0ed2401711a4dd50fedae13846af09616a440f7e Mon Sep 17 00:00:00 2001 From: George Armhold Date: Thu, 9 Nov 2017 07:16:01 -0500 Subject: [PATCH 2/2] exit 1 if received signal is other than SIGINT send cleanup msg to stderr, not stdout gh-1413 --- cmd/restic/cleanup.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/restic/cleanup.go b/cmd/restic/cleanup.go index 208effdbc..04875fe45 100644 --- a/cmd/restic/cleanup.go +++ b/cmd/restic/cleanup.go @@ -73,8 +73,14 @@ func RunCleanupHandlers() { func CleanupHandler(c <-chan os.Signal) { for s := range c { debug.Log("signal %v received, cleaning up", s) - fmt.Printf("%sInterrupt received, cleaning up\n", ClearLine()) - Exit(0) + fmt.Fprintf(stderr, "%ssignal %v received, cleaning up\n", ClearLine(), s) + + code := 0 + if s != syscall.SIGINT { + code = 1 + } + + Exit(code) } }