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:
parent
cdde8fa75a
commit
d022c81d99
3 changed files with 20 additions and 3 deletions
|
@ -471,6 +471,7 @@ func initConfig() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveExitCode(err error) {
|
func resolveExitCode(err error) {
|
||||||
|
atexit.Run()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
os.Exit(exitCodeSuccess)
|
os.Exit(exitCodeSuccess)
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
fusefs "bazil.org/fuse/fs"
|
fusefs "bazil.org/fuse/fs"
|
||||||
"github.com/ncw/rclone/cmd/mountlib"
|
"github.com/ncw/rclone/cmd/mountlib"
|
||||||
"github.com/ncw/rclone/fs"
|
"github.com/ncw/rclone/fs"
|
||||||
|
"github.com/ncw/rclone/lib/atexit"
|
||||||
"github.com/ncw/rclone/vfs"
|
"github.com/ncw/rclone/vfs"
|
||||||
"github.com/ncw/rclone/vfs/vfsflags"
|
"github.com/ncw/rclone/vfs/vfsflags"
|
||||||
"github.com/okzk/sdnotify"
|
"github.com/okzk/sdnotify"
|
||||||
|
@ -133,6 +134,7 @@ func Mount(f fs.Fs, mountpoint string) error {
|
||||||
signal.Notify(sigInt, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigInt, syscall.SIGINT, syscall.SIGTERM)
|
||||||
sigHup := make(chan os.Signal, 1)
|
sigHup := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigHup, syscall.SIGHUP)
|
signal.Notify(sigHup, syscall.SIGHUP)
|
||||||
|
atexit.IgnoreSignals()
|
||||||
|
|
||||||
if err := sdnotify.SdNotifyReady(); err != nil && err != sdnotify.SdNotifyNoSocket {
|
if err := sdnotify.SdNotifyReady(); err != nil && err != sdnotify.SdNotifyNoSocket {
|
||||||
return errors.Wrap(err, "failed to notify systemd")
|
return errors.Wrap(err, "failed to notify systemd")
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
fns []func()
|
fns []func()
|
||||||
|
exitChan chan os.Signal
|
||||||
exitOnce sync.Once
|
exitOnce sync.Once
|
||||||
registerOnce sync.Once
|
registerOnce sync.Once
|
||||||
)
|
)
|
||||||
|
@ -24,10 +25,13 @@ func Register(fn func()) {
|
||||||
// Run AtExit handlers on SIGINT or SIGTERM so everything gets
|
// Run AtExit handlers on SIGINT or SIGTERM so everything gets
|
||||||
// tidied up properly
|
// tidied up properly
|
||||||
registerOnce.Do(func() {
|
registerOnce.Do(func() {
|
||||||
|
exitChan = make(chan os.Signal, 1)
|
||||||
|
signal.Notify(exitChan, os.Interrupt) // syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT
|
||||||
go func() {
|
go func() {
|
||||||
ch := make(chan os.Signal, 1)
|
sig, closed := <-exitChan
|
||||||
signal.Notify(ch, os.Interrupt) // syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT
|
if closed {
|
||||||
sig := <-ch
|
return
|
||||||
|
}
|
||||||
fs.Infof(nil, "Signal received: %s", sig)
|
fs.Infof(nil, "Signal received: %s", sig)
|
||||||
Run()
|
Run()
|
||||||
fs.Infof(nil, "Exiting...")
|
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
|
// Run all the at exit functions if they haven't been run already
|
||||||
func Run() {
|
func Run() {
|
||||||
exitOnce.Do(func() {
|
exitOnce.Do(func() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue