diff --git a/infrastructure.go b/infrastructure.go index 985441c9..fe6cbc90 100644 --- a/infrastructure.go +++ b/infrastructure.go @@ -145,8 +145,9 @@ type Client struct { requestList *list.List // Notifications. - ntfnHandlers *NotificationHandlers - ntfnState *notificationState + ntfnHandlers *NotificationHandlers + ntfnStateLock sync.Mutex + ntfnState *notificationState // Networking infrastructure. sendChan chan []byte @@ -233,8 +234,8 @@ func (c *Client) trackRegisteredNtfns(cmd interface{}) { return } - c.ntfnState.Lock() - defer c.ntfnState.Unlock() + c.ntfnStateLock.Lock() + defer c.ntfnStateLock.Unlock() switch bcmd := cmd.(type) { case *btcjson.NotifyBlocksCmd: @@ -498,7 +499,9 @@ func (c *Client) reregisterNtfns() error { // the notification state (while not under the lock of course) which // also register it with the remote RPC server, so this prevents double // registrations. + c.ntfnStateLock.Lock() stateCopy := c.ntfnState.Copy() + c.ntfnStateLock.Unlock() // Reregister notifyblocks if needed. if stateCopy.notifyBlocks { diff --git a/notify.go b/notify.go index 45c0b893..0e4ca144 100644 --- a/notify.go +++ b/notify.go @@ -10,7 +10,6 @@ import ( "encoding/json" "errors" "fmt" - "sync" "time" "github.com/btcsuite/btcd/btcjson" @@ -32,7 +31,6 @@ var ( // registered notification so the state can be automatically re-established on // reconnect. type notificationState struct { - sync.Mutex notifyBlocks bool notifyNewTx bool notifyNewTxVerbose bool @@ -41,12 +39,7 @@ type notificationState struct { } // Copy returns a deep copy of the receiver. -// -// This function is safe for concurrent access. func (s *notificationState) Copy() *notificationState { - s.Lock() - defer s.Unlock() - var stateCopy notificationState stateCopy.notifyBlocks = s.notifyBlocks stateCopy.notifyNewTx = s.notifyNewTx