From c5306116428ca6a7c6494f911544c916a2e2e21a Mon Sep 17 00:00:00 2001 From: b00ris Date: Tue, 28 Nov 2017 16:17:15 +0300 Subject: [PATCH] Fix race #452 (#454) It fixes race conditions in `geth/signal`. --- geth/signal/signals.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/geth/signal/signals.go b/geth/signal/signals.go index aff3c60a5..538d2defb 100644 --- a/geth/signal/signals.go +++ b/geth/signal/signals.go @@ -10,6 +10,7 @@ import ( "encoding/json" "github.com/status-im/status-go/geth/log" + "sync" ) const ( @@ -47,14 +48,21 @@ type NodeNotificationHandler func(jsonEvent string) var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler +// notificationHandlerMutex guards notificationHandler for concurrent calls +var notificationHandlerMutex sync.RWMutex + // SetDefaultNodeNotificationHandler sets notification handler to invoke on Send func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) { + notificationHandlerMutex.Lock() notificationHandler = fn + notificationHandlerMutex.Unlock() } // ResetDefaultNodeNotificationHandler sets notification handler to default one func ResetDefaultNodeNotificationHandler() { + notificationHandlerMutex.Lock() notificationHandler = TriggerDefaultNodeNotificationHandler + notificationHandlerMutex.Unlock() } // TriggerDefaultNodeNotificationHandler triggers default notification handler (helpful in tests) @@ -71,6 +79,8 @@ func Send(signal Envelope) { //export NotifyNode //nolint: golint func NotifyNode(jsonEvent *C.char) { + notificationHandlerMutex.RLock() + defer notificationHandlerMutex.RUnlock() notificationHandler(C.GoString(jsonEvent)) }