From c49331bd7d42b68d93b99e04b209d31e389d680e Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Tue, 3 Mar 2015 21:26:24 +0100 Subject: [PATCH] Avoid beeing stuck in hub lock when a client gets replaced but the old connection does not hang on close. --- src/app/spreed-webrtc-server/hub.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/app/spreed-webrtc-server/hub.go b/src/app/spreed-webrtc-server/hub.go index a5aefc12..0535670f 100644 --- a/src/app/spreed-webrtc-server/hub.go +++ b/src/app/spreed-webrtc-server/hub.go @@ -154,8 +154,12 @@ func (h *hub) OnConnect(client Client, session *Session) { log.Printf("Created client %d with id %s\n", client.Index(), session.Id) // Register connection or replace existing one. if ec, ok := h.clients[session.Id]; ok { - log.Printf("Closing obsolete client %d with id %s\n", ec.Index(), session.Id) - ec.ReplaceAndClose() + // Clean up old client at the end and make sure to run this in another go routine, + // to avoid blocking the new client if the old one hangs or whatever. + defer func() { + log.Printf("Closing obsolete client %d with id %s\n", ec.Index(), session.Id) + go ec.ReplaceAndClose() + }() } h.clients[session.Id] = client h.mutex.Unlock()