feat(sync): fix data race

Signed-off-by: Vishwas Rajashekar <dev@vrajashkr.com>
This commit is contained in:
Vishwas Rajashekar
2026-05-20 23:32:08 +05:30
parent 38f0e498ab
commit f8ef0ae706
+12 -4
View File
@@ -100,11 +100,15 @@ func (cbr *ChunkedBlobReader) Read(buff []byte) (int, error) {
cbr.bytesMu.Unlock()
cbr.clientMu.RLock()
clients := cbr.clients
clientIDs := make([]int, 0, len(cbr.clients))
for id := range cbr.clients {
clientIDs = append(clientIDs, id)
}
cbr.clientMu.RUnlock()
// drain all clients and close their channels
for clientId := range clients {
for _, clientId := range clientIDs {
cbr.Unsubscribe(clientId)
}
@@ -155,10 +159,14 @@ func (cbr *ChunkedBlobReader) Read(buff []byte) (int, error) {
// If the reader has finished reading the blob, close all clients.
if err == io.EOF {
cbr.clientMu.RLock()
clients := cbr.clients
clientIDs := make([]int, 0, len(cbr.clients))
for id := range cbr.clients {
clientIDs = append(clientIDs, id)
}
cbr.clientMu.RUnlock()
for clientId := range clients {
for _, clientId := range clientIDs {
cbr.Unsubscribe(clientId)
}
}