Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leaking connections on cluster port #3680

Merged
merged 1 commit into from
Dec 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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