From a17f1e875ce5944b2cdbb2c685f1ab32ed8adff5 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 19 Jan 2017 11:19:57 -0800 Subject: [PATCH] server/mux: Close the connection even if buffer.Flush() returns error. (#3599) It is possible that buf.Flush() might return an error, leading to a potential leak in active sockets. --- cmd/server-mux.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/server-mux.go b/cmd/server-mux.go index c7e9ffd1e..bf79bfc2e 100644 --- a/cmd/server-mux.go +++ b/cmd/server-mux.go @@ -108,10 +108,12 @@ func (c *ConnMux) Read(b []byte) (int, error) { // Close the connection. func (c *ConnMux) Close() (err error) { - if err = c.bufrw.Flush(); err != nil { - return err - } - return c.Conn.Close() + // Make sure that we always close a connection, + // even if the bufioWriter flush sends an error. + defer c.Conn.Close() + + // Flush and write to the connection. + return c.bufrw.Flush() } // ListenerMux wraps the standard net.Listener to inspect