Skip to content

Commit

Permalink
Fix leaking connections on cluster port (#3680)
Browse files Browse the repository at this point in the history
  • Loading branch information
briankassouf authored Dec 13, 2017
1 parent 2931148 commit 2b7c903
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions vault/request_forwarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ func (c *Core) startForwarding() error {
go func() {
defer shutdownWg.Done()

// closeCh is used to shutdown the spawned goroutines once this
// function returns
closeCh := make(chan struct{})
defer func() {
close(closeCh)
}()

if c.logger.IsInfo() {
c.logger.Info("core/startClusterListener: starting listener", "listener_address", laddr)
}
Expand Down Expand Up @@ -134,7 +141,6 @@ func (c *Core) startForwarding() error {
if conn == nil {
continue
}
defer conn.Close()

// Type assert to TLS connection and handshake to populate the
// connection state
Expand All @@ -159,11 +165,28 @@ func (c *Core) startForwarding() error {
c.clusterParamsLock.RLock()
rpcServer := c.rpcServer
c.clusterParamsLock.RUnlock()

shutdownWg.Add(2)
// quitCh is used to close the connection and the second
// goroutine if the server closes before closeCh.
quitCh := make(chan struct{})
go func() {
select {
case <-quitCh:
case <-closeCh:
}
tlsConn.Close()
shutdownWg.Done()
}()

go func() {
fws.ServeConn(tlsConn, &http2.ServeConnOpts{
Handler: rpcServer,
})
tlsConn.Close()
// close the quitCh which will close the connection and
// the other goroutine.
close(quitCh)
shutdownWg.Done()
}()

default:
Expand Down

0 comments on commit 2b7c903

Please sign in to comment.