Skip to content

Commit 44fbaa4

Browse files
committed
Fix concurrent map access panic in webSocketServer.Close()
Close() iterated s.connections without holding the mutex, while connection goroutines (listen/sender) concurrently called connectionClosed() which deletes from the same map under the lock. This caused a "concurrent map iteration and map write" fatal error observed as a flaky failure in TestBroadcastStartWithoutConnections. Snapshot the connections slice under the lock before closing them. Signed-off-by: Enrique Lacal <enrique.lacal@kaleido.io> Made-with: Cursor
1 parent 075e365 commit 44fbaa4

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

pkg/wsserver/wsserver.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,13 @@ func (s *webSocketServer) connectionClosed(c *webSocketConnection) {
127127
}
128128

129129
func (s *webSocketServer) Close() {
130+
s.mux.Lock()
131+
conns := make([]*webSocketConnection, 0, len(s.connections))
130132
for _, c := range s.connections {
133+
conns = append(conns, c)
134+
}
135+
s.mux.Unlock()
136+
for _, c := range conns {
131137
c.close()
132138
}
133139
}

0 commit comments

Comments
 (0)