From 031ad1f22dbb3b393850c9534ac082ab71ab1128 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 31 May 2023 18:15:34 +0200 Subject: prevent deadlocks when connections are closed Currently the unbounded Events channel is never closed and Connection are not removed from WRMS therefore any call to Broadcast deadlocks because the wsEndpoint consumer is no longer reading Events. Prevent this deadlock with a combination of announcing the closing of connections and sender counting for each connection. Before a sender is trying to write to the Events channel it checks if it is already closed. If it observes that the connection is open it registers itself as sender send the Event and unregisters itself. The wsEndpoint announces that the connection is closed and consumes Events until no registered senders are left. Then it closed the Events channel and terminates. --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 55a789a..f962a76 100644 --- a/main.go +++ b/main.go @@ -220,8 +220,9 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { cancel() }() - conn := &Connection{id, make(chan Event), c} - wrms.Connections[id] = conn + conn := newConnection(id, c) + wrms.addConn(conn) + defer conn.Close() llog.Info("New websocket connection with id %v", id) @@ -339,7 +340,6 @@ func main() { config.HasUpload = slices.Contains(config.Backends, "upload") wrms = NewWrms(config) - defer wrms.Close() setupRoutes() -- cgit v1.2.3