From ad11e1392c35006d72febca4ffea14b197a2aae7 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Sat, 6 Dec 2014 22:42:32 +0800 Subject: [PATCH] Removed race condition If two interrupts were fired really quickly interruptCount could have been incremented twice before the LoadUint32 making cleanup not being called at all. Signed-off-by: Erik Dubbelboer --- signal/trap.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/signal/trap.go b/signal/trap.go index 9be8267..78a709b 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -34,9 +34,8 @@ func Trap(cleanup func()) { case os.Interrupt, syscall.SIGTERM: // If the user really wants to interrupt, let him do so. if atomic.LoadUint32(&interruptCount) < 3 { - atomic.AddUint32(&interruptCount, 1) // Initiate the cleanup only once - if atomic.LoadUint32(&interruptCount) == 1 { + if atomic.AddUint32(&interruptCount, 1) == 1 { // Call cleanup handler cleanup() os.Exit(0)