Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(chat): avoid disconnect delivery deadlock
Gabe Kangasc2c11905c38c
diff --git a/services/chat/chatclient.go b/services/chat/chatclient.goindex 32f3e327d4..849b10296d 100644--- a/services/chat/chatclient.go+++ b/services/chat/chatclient.go@@ -199,14 +199,21 @@ func (c *Client) close() {log.Traceln("client closed:", c.User.DisplayName, c.Id, c.IPAddress)c.mu.Lock()- defer c.mu.Unlock()-- if c.send != nil {+ if c.send == nil {+ c.mu.Unlock()+ return+ }+ if c.conn != nil {_ = c.conn.Close()- c.server.unregister <- c.Id- close(c.send)- c.send = nil}+ close(c.send)+ c.send = nil+ c.mu.Unlock()++ // Do not hold c.mu while waiting for the server loop. The loop can be+ // delivering to this client and needs the same mutex before it can receive+ // the unregister request.+ c.server.unregister <- c.Id}func (c *Client) passesRateLimit() bool {diff --git a/services/chat/server_test.go b/services/chat/server_test.goindex 3fa8109fbc..90c6649e98 100644--- a/services/chat/server_test.go+++ b/services/chat/server_test.go@@ -110,6 +110,60 @@ func TestSendUserJoinedMessage_WebhookFiresRegardlessOfJoinPartSetting(t *testin}}+func TestClientCloseDoesNotDeadlockDelivery(t *testing.T) {+ unregister := make(chan uint)+ service := &Service{unregister: unregister}+ client := &Client{+ Id: 1,+ User: &models.User{DisplayName: "Tester"},+ server: service,+ send: make(chan []byte, 1),+ }++ closed := make(chan struct{})+ go func() {+ client.close()+ close(closed)+ }()++ deadline := time.After(time.Second)+ for {+ if client.mu.TryRLock() {+ sendClosed := client.send == nil+ client.mu.RUnlock()+ if sendClosed {+ break+ }+ }+ select {+ case <-deadline:+ t.Fatal("client close kept the delivery lock while unregistering")+ default:+ }+ }++ delivered := make(chan struct{})+ go func() {+ client.trySend([]byte("message"))+ close(delivered)+ }()++ select {+ case <-delivered:+ case <-time.After(time.Second):+ t.Fatal("delivery deadlocked with client close")+ }++ if id := <-unregister; id != client.Id {+ t.Fatalf("unregistered client %d, want %d", id, client.Id)+ }+ select {+ case <-closed:+ case <-time.After(time.Second):+ t.Fatal("client close did not complete")+ }+}+func TestUserPartedTimersHandleConcurrentDisconnectsAndReconnects(t *testing.T) {chatSvc := &Service{clients: map[uint]*Client{},