go-ethereum
go-ethereum copied to clipboard
node: fix race condition on httpConfig access
Hey there, I looked into the issue you flagged in rpcstack.go, there was a race condition on h.httpConfig due to an unguarded read on line 212. Here’s how I resolved it:
- Updated the httpServer struct to use sync.RWMutex instead of sync.Mutex:
mu sync.RWMutex - Wrapped the read access to h.httpConfig.prefix with RLock() and RUnlock():
h.mu.RLock()
rpc := h.httpHandler.Load().(*rpcHandler)
h.mu.RUnlock()
if rpc != nil {
// First try to route in the mux.
muxHandler, pattern := h.mux.Handler(r)
if pattern != "" {
muxHandler.ServeHTTP(w, r)
return
}
h.mu.RLock()
prefix := h.httpConfig.prefix
h.mu.RUnlock()
if checkPath(r, prefix) {
rpc.ServeHTTP(w, r)
return
}
}
- Re-ran the tests using:
go test ./node -raceOutput:ok github.com/ethereum/go-ethereum/node 4.044sLet me know if you want a PR for this!