From 37531b18847774a7f057496bc34f2a48053fa417 Mon Sep 17 00:00:00 2001
From: Boqin Qin <Bobbqqin@gmail.com>
Date: Sun, 16 Feb 2020 02:14:29 +0800
Subject: [PATCH] cmd/faucet: protect f.reqs with Rlock to prevent data race
 (#20669)

* cmd/faucet: add Rlock to protect f.reqs in apiHandler

* cmd/faucet: make a locked copy of f.reqs
---
 cmd/faucet/faucet.go | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/cmd/faucet/faucet.go b/cmd/faucet/faucet.go
index 77938efab..e86f8533d 100644
--- a/cmd/faucet/faucet.go
+++ b/cmd/faucet/faucet.go
@@ -360,11 +360,14 @@ func (f *faucet) apiHandler(w http.ResponseWriter, r *http.Request) {
 		}
 	}
 	// Send over the initial stats and the latest header
+	f.lock.RLock()
+	reqs := f.reqs
+	f.lock.RUnlock()
 	if err = send(conn, map[string]interface{}{
 		"funds":    new(big.Int).Div(balance, ether),
 		"funded":   nonce,
 		"peers":    f.stack.Server().PeerCount(),
-		"requests": f.reqs,
+		"requests": reqs,
 	}, 3*time.Second); err != nil {
 		log.Warn("Failed to send initial stats to client", "err", err)
 		return
-- 
GitLab