server: close only non-closed listeners

The listener can be closed twice: in Close and in Serve. It might lead to
pretty bad things, for example, https://golang.org/src/net/unixsock_posix.go#L340
can delete a file which created by another listener.

Signed-off-by: Alexander Morozov <lk4d4math@gmail.com>
This commit is contained in:
Alexander Morozov 2016-07-12 16:15:25 -07:00
parent 13edeeffde
commit 47de9c3564
1 changed files with 4 additions and 2 deletions

View File

@ -320,9 +320,11 @@ func (s *Server) Serve(lis net.Listener) error {
s.lis[lis] = true
s.mu.Unlock()
defer func() {
lis.Close()
s.mu.Lock()
delete(s.lis, lis)
if s.lis != nil && s.lis[lis] {
lis.Close()
delete(s.lis, lis)
}
s.mu.Unlock()
}()
for {