go-ethereum icon indicating copy to clipboard operation
go-ethereum copied to clipboard

node: fix race condition on httpConfig access

Open stevenegoavil opened this issue 5 months ago • 0 comments

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:

  1. Updated the httpServer struct to use sync.RWMutex instead of sync.Mutex: mu sync.RWMutex
  2. 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
    }
}
  1. Re-ran the tests using: go test ./node -race Output: ok github.com/ethereum/go-ethereum/node 4.044s Let me know if you want a PR for this!

stevenegoavil avatar Jun 14 '25 02:06 stevenegoavil