From 80b65cc88c0db385422d9657a6c4062bbecd4d8b Mon Sep 17 00:00:00 2001 From: Karthic Rao Date: Wed, 18 May 2016 09:55:51 +0530 Subject: [PATCH] Race fix during interrupt, and avoid receiving multiple interrupts --- profile.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/profile.go b/profile.go index 38c3e47..e2fe536 100644 --- a/profile.go +++ b/profile.go @@ -178,6 +178,10 @@ func Start(options ...func(*profile)) interface { }) } + prof.closers = append(prof.closers, func() { + atomic.SwapUint32(&started, 0) + }) + if !prof.noShutdownHook { go func() { c := make(chan os.Signal, 1) @@ -185,15 +189,15 @@ func Start(options ...func(*profile)) interface { <-c log.Println("profile: caught interrupt, stopping profiles") + // Stop receiving any more interrupts, while exiting. + signal.Stop(c) + // Stop profiling calling all closers. prof.Stop() + // Exit peacefully. os.Exit(0) }() } - prof.closers = append(prof.closers, func() { - atomic.SwapUint32(&started, 0) - }) - return &prof }