diff options
| author | Chris Lu <chrislusf@users.noreply.github.com> | 2018-10-09 00:53:30 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-10-09 00:53:30 -0700 |
| commit | 88ad8d3a896e1ed339b3d811ca921ccca8c9c68a (patch) | |
| tree | 9b96d204547beb42304c3d83a6a37d6b307f63f3 | |
| parent | 1d3f045e850595e84981a009c1bf1b8c384bdaab (diff) | |
| parent | 4db68669b6a27971650fe09516319f87a8cf4b22 (diff) | |
| download | seaweedfs-88ad8d3a896e1ed339b3d811ca921ccca8c9c68a.tar.xz seaweedfs-88ad8d3a896e1ed339b3d811ca921ccca8c9c68a.zip | |
Merge pull request #747 from a1exwang/master
[bugfix] Fix interrupt hook overwritten bug
| -rw-r--r-- | weed/util/pprof.go | 1 | ||||
| -rw-r--r-- | weed/util/signal_handling.go | 25 |
2 files changed, 20 insertions, 6 deletions
diff --git a/weed/util/pprof.go b/weed/util/pprof.go index 94bcdb8b3..363017555 100644 --- a/weed/util/pprof.go +++ b/weed/util/pprof.go @@ -14,7 +14,6 @@ func SetupProfiling(cpuProfile, memProfile string) { glog.Fatal(err) } pprof.StartCPUProfile(f) - defer pprof.StopCPUProfile() OnInterrupt(func() { pprof.StopCPUProfile() }) diff --git a/weed/util/signal_handling.go b/weed/util/signal_handling.go index 7da898738..99447e8be 100644 --- a/weed/util/signal_handling.go +++ b/weed/util/signal_handling.go @@ -5,13 +5,16 @@ package util import ( "os" "os/signal" + "sync" "syscall" ) -func OnInterrupt(fn func()) { - // deal with control+c,etc - signalChan := make(chan os.Signal, 1) - // controlling terminal close, daemon not exit +var signalChan chan os.Signal +var hooks = make([]func(), 0) +var hookLock sync.Mutex + +func init() { + signalChan = make(chan os.Signal, 1) signal.Ignore(syscall.SIGHUP) signal.Notify(signalChan, os.Interrupt, @@ -24,8 +27,20 @@ func OnInterrupt(fn func()) { ) go func() { for _ = range signalChan { - fn() + for _, hook := range hooks { + hook() + } os.Exit(0) } }() } + +func OnInterrupt(fn func()) { + // prevent reentry + hookLock.Lock() + defer hookLock.Unlock() + + // deal with control+c,etc + // controlling terminal close, daemon not exit + hooks = append(hooks, fn) +} |
