From 963f23ec8581d2644b7d6c4e87c89aa84563105c Mon Sep 17 00:00:00 2001
From: capossele <angelocapossele@gmail.com>
Date: Thu, 19 Dec 2019 09:22:37 +0000
Subject: [PATCH] :bug: fix data races on ui

---
 plugins/ui/logger.go    | 2 +-
 plugins/ui/nodeInfo.go  | 6 +++++-
 plugins/ui/txLog.go     | 6 ++++++
 plugins/ui/ui.go        | 6 ++++++
 plugins/ui/websocket.go | 4 ++++
 5 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/plugins/ui/logger.go b/plugins/ui/logger.go
index a4653aac..08658c43 100644
--- a/plugins/ui/logger.go
+++ b/plugins/ui/logger.go
@@ -7,7 +7,7 @@ import (
 	"github.com/iotaledger/hive.go/logger"
 )
 
-var logMutex = sync.Mutex{}
+var logMutex = sync.RWMutex{}
 var logHistory = make([]*statusMessage, 0)
 
 type statusMessage struct {
diff --git a/plugins/ui/nodeInfo.go b/plugins/ui/nodeInfo.go
index c23b2544..3c3f09c0 100644
--- a/plugins/ui/nodeInfo.go
+++ b/plugins/ui/nodeInfo.go
@@ -1,6 +1,7 @@
 package ui
 
 import (
+	"sync"
 	"sync/atomic"
 	"time"
 
@@ -14,6 +15,7 @@ var receivedTpsCounter uint64
 var solidTpsCounter uint64
 
 var tpsQueue []uint32
+var tpsQueueMutex sync.RWMutex
 
 const maxQueueSize int = 3600
 
@@ -31,10 +33,12 @@ type nodeInfo struct {
 func gatherInfo() nodeInfo {
 
 	// update tps queue
-	tpsQueue = append(tpsQueue, uint32(receivedTpsCounter))
+	tpsQueueMutex.Lock()
+	tpsQueue = append(tpsQueue, uint32(atomic.LoadUint64(&receivedTpsCounter)))
 	if len(tpsQueue) > maxQueueSize {
 		tpsQueue = tpsQueue[1:]
 	}
+	tpsQueueMutex.Unlock()
 
 	// update neighbors
 	chosenNeighbors := []string{}
diff --git a/plugins/ui/txLog.go b/plugins/ui/txLog.go
index 3f64ac61..d8d57932 100644
--- a/plugins/ui/txLog.go
+++ b/plugins/ui/txLog.go
@@ -2,6 +2,7 @@ package ui
 
 import (
 	"strings"
+	"sync"
 
 	"github.com/iotaledger/goshimmer/packages/model/value_transaction"
 	"github.com/iotaledger/iota.go/transaction"
@@ -9,16 +10,21 @@ import (
 
 // cleared every second
 var transactions []transaction.Transaction
+var tMutex sync.Mutex
 
 var emptyTag = strings.Repeat("9", 27)
 
 func logTransactions() interface{} {
+	tMutex.Lock()
+	defer tMutex.Unlock()
 	a := transactions
 	transactions = make([]transaction.Transaction, 0)
 	return a
 }
 
 func saveTx(tx *value_transaction.ValueTransaction) {
+	tMutex.Lock()
+	defer tMutex.Unlock()
 	transactions = append(transactions, transaction.Transaction{
 		Hash:              tx.MetaTransaction.GetHash(),
 		Address:           tx.GetAddress(),
diff --git a/plugins/ui/ui.go b/plugins/ui/ui.go
index 3061a900..86add4df 100644
--- a/plugins/ui/ui.go
+++ b/plugins/ui/ui.go
@@ -27,9 +27,13 @@ func configure(plugin *node.Plugin) {
 
 	webapi.AddEndpoint("ws", upgrader)
 	webapi.AddEndpoint("loghistory", func(c echo.Context) error {
+		logMutex.RLock()
+		defer logMutex.RUnlock()
 		return c.JSON(http.StatusOK, logHistory)
 	})
 	webapi.AddEndpoint("tpsqueue", func(c echo.Context) error {
+		tpsQueueMutex.RLock()
+		defer tpsQueueMutex.RUnlock()
 		return c.JSON(http.StatusOK, tpsQueue)
 	})
 
@@ -74,10 +78,12 @@ func run(plugin *node.Plugin) {
 			case <-daemon.ShutdownSignal:
 				return
 			case <-time.After(1 * time.Second):
+				wsMutex.Lock()
 				ws.send(resp{
 					"info": gatherInfo(),
 					"txs":  logTransactions(),
 				})
+				wsMutex.Unlock()
 			}
 		}
 	})
diff --git a/plugins/ui/websocket.go b/plugins/ui/websocket.go
index 84cacf53..8507f59a 100644
--- a/plugins/ui/websocket.go
+++ b/plugins/ui/websocket.go
@@ -3,6 +3,7 @@ package ui
 import (
 	"encoding/json"
 	"fmt"
+	"sync"
 
 	"github.com/labstack/echo"
 	"golang.org/x/net/websocket"
@@ -13,6 +14,7 @@ type socket struct {
 }
 
 var ws socket
+var wsMutex sync.Mutex
 
 func (sock socket) send(msg interface{}) {
 	payload, err := json.Marshal(msg)
@@ -24,7 +26,9 @@ func (sock socket) send(msg interface{}) {
 func upgrader(c echo.Context) error {
 
 	websocket.Handler(func(conn *websocket.Conn) {
+		wsMutex.Lock()
 		ws.conn = conn
+		wsMutex.Unlock()
 		defer conn.Close()
 		for {
 			msg := ""
-- 
GitLab