From f5ac34d755eb6975958108413c7802a782ca16c8 Mon Sep 17 00:00:00 2001 From: Tom Thorogood Date: Thu, 11 Jan 2018 00:07:59 +1030 Subject: [PATCH] Fix TCP Shutdown 'use of closed network connection' (#623) The check for srv.started being false is in the wrong place, it should be after Accept not after ReadTCP. If Shutdown is called, serveTCP will currently return a 'use of closed network connection' error, which is undesired. This commit mirrors the behaviour of serveUDP with respect to Shutdown. --- server.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server.go b/server.go index b79abad5..b6ce5b5f 100644 --- a/server.go +++ b/server.go @@ -460,6 +460,12 @@ func (srv *Server) serveTCP(l net.Listener) error { // deadline is not used here for { rw, err := l.Accept() + srv.lock.RLock() + if !srv.started { + srv.lock.RUnlock() + return nil + } + srv.lock.RUnlock() if err != nil { if neterr, ok := err.(net.Error); ok && neterr.Temporary() { continue @@ -467,12 +473,6 @@ func (srv *Server) serveTCP(l net.Listener) error { return err } m, err := reader.ReadTCP(rw, rtimeout) - srv.lock.RLock() - if !srv.started { - srv.lock.RUnlock() - return nil - } - srv.lock.RUnlock() if err != nil { continue }