mount: ensure atexit gets run on interrupt
When running `rclone mount`, there were 2 signal handlers for `os.Interrupt`. Those handlers would run concurrently and in some cases cause either unmount or `atexit.Run()` being skipped. In addition `atexit.Run()` will get called in `resolveExitCode` to ensure cleanup on errors.
This commit is contained in:
committed by
Nick Craig-Wood
parent
cdde8fa75a
commit
d022c81d99
+17
-3
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
var (
|
||||
fns []func()
|
||||
exitChan chan os.Signal
|
||||
exitOnce sync.Once
|
||||
registerOnce sync.Once
|
||||
)
|
||||
@@ -24,10 +25,13 @@ func Register(fn func()) {
|
||||
// Run AtExit handlers on SIGINT or SIGTERM so everything gets
|
||||
// tidied up properly
|
||||
registerOnce.Do(func() {
|
||||
exitChan = make(chan os.Signal, 1)
|
||||
signal.Notify(exitChan, os.Interrupt) // syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT
|
||||
go func() {
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, os.Interrupt) // syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT
|
||||
sig := <-ch
|
||||
sig, closed := <-exitChan
|
||||
if closed {
|
||||
return
|
||||
}
|
||||
fs.Infof(nil, "Signal received: %s", sig)
|
||||
Run()
|
||||
fs.Infof(nil, "Exiting...")
|
||||
@@ -36,6 +40,16 @@ func Register(fn func()) {
|
||||
})
|
||||
}
|
||||
|
||||
// IgnoreSignals disables the signal handler and prevents Run from beeing executed automatically
|
||||
func IgnoreSignals() {
|
||||
registerOnce.Do(func() {})
|
||||
if exitChan != nil {
|
||||
signal.Stop(exitChan)
|
||||
close(exitChan)
|
||||
exitChan = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Run all the at exit functions if they haven't been run already
|
||||
func Run() {
|
||||
exitOnce.Do(func() {
|
||||
|
||||
Reference in New Issue
Block a user