Skip to content

Commit

Permalink
remotedb/grpcdb: fix net.Listener leak in ListenAndServe
Browse files Browse the repository at this point in the history
Uses Go's named return value feature to properly check and
ensure that if any error is encountered, that we properly
invoke .Close for the bound net listener inside of ListenAndServe.

Fixes tendermint#268
  • Loading branch information
odeke-em committed Jun 28, 2022
1 parent b3c748f commit bbd5a76
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion remotedb/grpcdb/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ import (
// ListenAndServe is a blocking function that sets up a gRPC based
// server at the address supplied, with the gRPC options passed in.
// Normally in usage, invoke it in a goroutine like you would for http.ListenAndServe.
func ListenAndServe(addr, cert, key string, opts ...grpc.ServerOption) error {
func ListenAndServe(addr, cert, key string, opts ...grpc.ServerOption) (rerr error) {
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer func() {
if rerr != nil {
_ = ln.Close()
}
}()

srv, err := NewServer(cert, key, opts...)
if err != nil {
return err
Expand Down

0 comments on commit bbd5a76

Please sign in to comment.