Skip to content

Commit

Permalink
fix(tm2/pkg/bft/node): make Node.startRPC not leak listeners on any e…
Browse files Browse the repository at this point in the history
…rror

Once Node.startRPC encounters an error, previously it was discarding
all the created listeners and leaking them unclosed. This change fixes
that using Go's named return values.

Fixes gnolang#3639
  • Loading branch information
odeke-em committed Jan 29, 2025
1 parent 15d119f commit a38f1a8
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions tm2/pkg/bft/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,17 @@ func (n *Node) configureRPC() {
rpccore.SetConfig(*n.config.RPC)
}

func (n *Node) startRPC() ([]net.Listener, error) {
func (n *Node) startRPC() (listeners []net.Listener, err error) {
defer func() {
if err != nil {
// Close all the created listeners on any error, instead of
// leaking them: https://github.com/gnolang/gno/issues/3639
for _, ln := range listeners {
ln.Close()
}
}
}()

listenAddrs := splitAndTrimEmpty(n.config.RPC.ListenAddress, ",", " ")

config := rpcserver.DefaultConfig()
Expand All @@ -729,8 +739,8 @@ func (n *Node) startRPC() ([]net.Listener, error) {

// we may expose the rpc over both a unix and tcp socket
var rebuildAddresses bool
listeners := make([]net.Listener, len(listenAddrs))
for i, listenAddr := range listenAddrs {
listeners = make([]net.Listener, 0, len(listenAddrs))
for _, listenAddr := range listenAddrs {
mux := http.NewServeMux()
rpcLogger := n.Logger.With("module", "rpc-server")
wmLogger := rpcLogger.With("protocol", "websocket")
Expand Down Expand Up @@ -782,7 +792,7 @@ func (n *Node) startRPC() ([]net.Listener, error) {
)
}

listeners[i] = listener
listeners = append(listeners, listener)
}
if rebuildAddresses {
n.config.RPC.ListenAddress = joinListenerAddresses(listeners)
Expand Down

0 comments on commit a38f1a8

Please sign in to comment.