If listener.Accept returns an error then the server stops responding
stefanba3 opened this issue · 2 comments
For example, here:
func (s *server) run() {
defer s.wg.Done()
for {
client, err := s.netListener.Accept()
if err != nil {
break
}
s.handle(client)
}
}
netListener
is commonly a TLS listener, which can return an error from Accept for many reasons. Breaking out of the loop stops the server from accepting new connections. And, as far as I can tell, there is no way to know from the outside (since run
gets called in a goroutine) that this has happened.
Actually, I am wrong. TLS wont return handshake errors from Accept. So this issue is less serious than I thought. I don't know if we can assume Accept will only return an error if closed, so it might still be a good idea to guard against that case.
I think this code could use errors.Is(err, net.ErrClosed)
to determine if the loop should break. Other errors should probably be logged, but continue to Accept()
.
net.ErrClosed
is newly exported in Go 1.16. golang/go#4373 (comment)