fix: race condition in initialisation of server

This commit is contained in:
Tom Arrell 2023-01-20 15:12:37 +01:00
parent 8b671291b8
commit c746dbc05b
No known key found for this signature in database
GPG Key ID: A5E3D9D260A58474
1 changed files with 8 additions and 0 deletions

View File

@ -235,12 +235,14 @@ func Serve(ln net.Listener,
closed func(conn Conn, err error), closed func(conn Conn, err error),
) error { ) error {
s := newServer() s := newServer()
s.mu.Lock()
s.net = ln.Addr().Network() s.net = ln.Addr().Network()
s.laddr = ln.Addr().String() s.laddr = ln.Addr().String()
s.ln = ln s.ln = ln
s.handler = handler s.handler = handler
s.accept = accept s.accept = accept
s.closed = closed s.closed = closed
s.mu.Unlock()
return serve(s) return serve(s)
} }
@ -296,7 +298,9 @@ func (s *Server) ListenServeAndSignal(signal chan error) error {
} }
return err return err
} }
s.mu.Lock()
s.ln = ln s.ln = ln
s.mu.Unlock()
if signal != nil { if signal != nil {
signal <- nil signal <- nil
} }
@ -305,9 +309,11 @@ func (s *Server) ListenServeAndSignal(signal chan error) error {
// Serve serves incoming connections with the given net.Listener. // Serve serves incoming connections with the given net.Listener.
func (s *Server) Serve(ln net.Listener) error { func (s *Server) Serve(ln net.Listener) error {
s.mu.Lock()
s.ln = ln s.ln = ln
s.net = ln.Addr().Network() s.net = ln.Addr().Network()
s.laddr = ln.Addr().String() s.laddr = ln.Addr().String()
s.mu.Unlock()
return serve(s) return serve(s)
} }
@ -321,7 +327,9 @@ func (s *TLSServer) ListenServeAndSignal(signal chan error) error {
} }
return err return err
} }
s.mu.Lock()
s.ln = ln s.ln = ln
s.mu.Unlock()
if signal != nil { if signal != nil {
signal <- nil signal <- nil
} }