make sure the server is stopped before closing all server sessions

This commit is contained in:
Marten Seemann 2021-01-23 12:45:14 +08:00
parent f1500db062
commit aea8f7345b
2 changed files with 10 additions and 8 deletions

View file

@ -250,7 +250,7 @@ func (h *packetHandlerMap) CloseServer() {
wg.Wait() wg.Wait()
} }
// Destroy the underlying connection and wait until listen() has returned. // Destroy closes the underlying connection and waits until listen() has returned.
// It does not close active sessions. // It does not close active sessions.
func (h *packetHandlerMap) Destroy() error { func (h *packetHandlerMap) Destroy() error {
if err := h.conn.Close(); err != nil { if err := h.conn.Close(); err != nil {

View file

@ -274,25 +274,27 @@ func (s *baseServer) accept(ctx context.Context) (quicSession, error) {
// Close the server // Close the server
func (s *baseServer) Close() error { func (s *baseServer) Close() error {
s.sessionHandler.CloseServer()
s.mutex.Lock() s.mutex.Lock()
defer s.mutex.Unlock()
if s.closed { if s.closed {
s.mutex.Unlock()
return nil return nil
} }
if s.serverError == nil { if s.serverError == nil {
s.serverError = errors.New("server closed") s.serverError = errors.New("server closed")
} }
var err error
// If the server was started with ListenAddr, we created the packet conn. // If the server was started with ListenAddr, we created the packet conn.
// We need to close it in order to make the go routine reading from that conn return. // We need to close it in order to make the go routine reading from that conn return.
if s.createdPacketConn { createdPacketConn := s.createdPacketConn
err = s.sessionHandler.Destroy()
}
s.closed = true s.closed = true
close(s.errorChan) close(s.errorChan)
s.mutex.Unlock()
<-s.running <-s.running
return err s.sessionHandler.CloseServer()
if createdPacketConn {
return s.sessionHandler.Destroy()
}
return nil
} }
func (s *baseServer) setCloseError(e error) { func (s *baseServer) setCloseError(e error) {