p2p: fix conn leak. part of #2046

This commit is contained in:
Ethan Buchman 2018-07-24 21:32:30 -04:00
parent 5768b67162
commit 359898dcac
3 changed files with 10 additions and 2 deletions

View File

@ -10,7 +10,11 @@ BUG FIXES
- (#2049) Fix OOM attack by returning error on negative input - (#2049) Fix OOM attack by returning error on negative input
- Fix result length to have max 20 (instead of 21) block metas - Fix result length to have max 20 (instead of 21) block metas
- [rpc] Validate height is non-negative in `/abci_query` - [rpc] Validate height is non-negative in `/abci_query`
- [Gopkg] Fix versions in the toml - [consensus] (#2050) Include evidence in proposal block parts (previously evidence was
not being included in blocks!)
- [p2p] (#2046) Close rejected inbound connections so file descriptor doesn't
leak
- [Gopkg] (#2053) Fix versions in the toml
## 0.22.5 ## 0.22.5

View File

@ -151,7 +151,7 @@ func (l *DefaultListener) OnStop() {
l.listener.Close() // nolint: errcheck l.listener.Close() // nolint: errcheck
} }
// Accept connections and pass on the channel // Accept connections and pass on the channel.
func (l *DefaultListener) listenRoutine() { func (l *DefaultListener) listenRoutine() {
for { for {
conn, err := l.listener.Accept() conn, err := l.listener.Accept()
@ -178,6 +178,8 @@ func (l *DefaultListener) listenRoutine() {
// Connections returns a channel of inbound connections. // Connections returns a channel of inbound connections.
// It gets closed when the listener closes. // It gets closed when the listener closes.
// It is the callers responsibility to close any connections received
// over this channel.
func (l *DefaultListener) Connections() <-chan net.Conn { func (l *DefaultListener) Connections() <-chan net.Conn {
return l.connections return l.connections
} }

View File

@ -496,6 +496,7 @@ func (sw *Switch) listenerRoutine(l Listener) {
maxPeers := sw.config.MaxNumPeers - DefaultMinNumOutboundPeers maxPeers := sw.config.MaxNumPeers - DefaultMinNumOutboundPeers
if maxPeers <= sw.peers.Size() { if maxPeers <= sw.peers.Size() {
sw.Logger.Info("Ignoring inbound connection: already have enough peers", "address", inConn.RemoteAddr().String(), "numPeers", sw.peers.Size(), "max", maxPeers) sw.Logger.Info("Ignoring inbound connection: already have enough peers", "address", inConn.RemoteAddr().String(), "numPeers", sw.peers.Size(), "max", maxPeers)
inConn.Close()
continue continue
} }
@ -510,6 +511,7 @@ func (sw *Switch) listenerRoutine(l Listener) {
// cleanup // cleanup
} }
// closes conn if err is returned
func (sw *Switch) addInboundPeerWithConfig( func (sw *Switch) addInboundPeerWithConfig(
conn net.Conn, conn net.Conn,
config *config.P2PConfig, config *config.P2PConfig,