fix deadlock when closing the server and the connection at the same time

This commit is contained in:
Marten Seemann 2020-10-29 17:56:14 +07:00
parent b7652887d2
commit 4c25cea9ec
3 changed files with 6 additions and 1 deletions

View file

@ -238,6 +238,10 @@ func (h *packetHandlerMap) SetServer(s unknownPacketHandler) {
func (h *packetHandlerMap) CloseServer() { func (h *packetHandlerMap) CloseServer() {
h.mutex.Lock() h.mutex.Lock()
if h.server == nil {
h.mutex.Unlock()
return
}
h.server = nil h.server = nil
var wg sync.WaitGroup var wg sync.WaitGroup
for _, handler := range h.handlers { for _, handler := range h.handlers {

View file

@ -251,6 +251,7 @@ var _ = Describe("Packet Handler Map", func() {
}) })
It("closes all server sessions", func() { It("closes all server sessions", func() {
handler.SetServer(NewMockUnknownPacketHandler(mockCtrl))
clientSess := NewMockPacketHandler(mockCtrl) clientSess := NewMockPacketHandler(mockCtrl)
clientSess.EXPECT().getPerspective().Return(protocol.PerspectiveClient) clientSess.EXPECT().getPerspective().Return(protocol.PerspectiveClient)
serverSess := NewMockPacketHandler(mockCtrl) serverSess := NewMockPacketHandler(mockCtrl)

View file

@ -274,12 +274,12 @@ 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() defer s.mutex.Unlock()
if s.closed { if s.closed {
return nil return nil
} }
s.sessionHandler.CloseServer()
if s.serverError == nil { if s.serverError == nil {
s.serverError = errors.New("server closed") s.serverError = errors.New("server closed")
} }