Skip to content

Commit 0420029

Browse files
winklemadbaptmont
andauthored
fix(memory): hold read lock while scanning sessions in SearchMemory (#1139)
InMemoryService.SearchMemory read the per-user session map under the read lock but released it before iterating, while AddSessionToMemory writes into that same map under the write lock. Concurrent Search and Add on the same app/user therefore race and can panic with "concurrent map iteration and map write", even though the service is documented as thread-safe. Hold the read lock across the whole scan. AddSessionToMemory replaces session slices wholesale rather than mutating them in place, so a read lock over the iteration is sufficient and still lets searches run concurrently. Co-authored-by: João Westerberg <[email protected]>
1 parent 218ed6f commit 0420029

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎memory/inmemory.go‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,20 @@ func (s *inMemoryService) SearchMemory(ctx context.Context, req *SearchRequest)
114114
userID: req.UserID,
115115
}
116116

117+
res := &SearchResponse{}
118+
119+
// Hold the read lock for the whole scan. AddSessionToMemory writes into the
120+
// per-user session map under the write lock, so releasing the lock before
121+
// iterating would race with a concurrent write and can panic with
122+
// "concurrent map iteration and map write".
117123
s.mu.RLock()
124+
defer s.mu.RUnlock()
125+
118126
values, ok := s.store[k]
119-
s.mu.RUnlock()
120127
if !ok {
121-
return &SearchResponse{}, nil
128+
return res, nil
122129
}
123130

124-
res := &SearchResponse{}
125-
126131
for _, events := range values {
127132
for _, e := range events {
128133
if checkMapsIntersect(e.words, queryWords) {

‎memory/inmemory_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package memory_test
1717
import (
1818
"iter"
1919
"slices"
20+
"strconv"
21+
"sync"
2022
"testing"
2123
"time"
2224

@@ -226,3 +228,43 @@ func must[V any](v V, err error) V {
226228
}
227229
return v
228230
}
231+
232+
// Test_inMemoryService_SearchMemory_Concurrent runs SearchMemory and
233+
// AddSessionToMemory on the same app/user in parallel. The service is
234+
// documented as thread-safe, so under -race (as CI runs it) the two must not
235+
// touch the per-user session map without synchronization.
236+
func Test_inMemoryService_SearchMemory_Concurrent(t *testing.T) {
237+
s := memory.InMemoryService()
238+
ctx := t.Context()
239+
240+
// Seed one session so the per-user map is non-empty while searchers iterate.
241+
if err := s.AddSessionToMemory(ctx, makeSession(t, "app1", "user1", "seed", nil)); err != nil {
242+
t.Fatalf("AddSessionToMemory() error = %v", err)
243+
}
244+
245+
const workers = 8
246+
var wg sync.WaitGroup
247+
for i := 0; i < workers; i++ {
248+
wg.Add(2)
249+
go func(i int) {
250+
defer wg.Done()
251+
for j := 0; j < 50; j++ {
252+
id := "s" + strconv.Itoa(i) + "-" + strconv.Itoa(j)
253+
if err := s.AddSessionToMemory(ctx, makeSession(t, "app1", "user1", id, nil)); err != nil {
254+
t.Errorf("AddSessionToMemory() error = %v", err)
255+
return
256+
}
257+
}
258+
}(i)
259+
go func() {
260+
defer wg.Done()
261+
for j := 0; j < 50; j++ {
262+
if _, err := s.SearchMemory(ctx, &memory.SearchRequest{AppName: "app1", UserID: "user1", Query: "x"}); err != nil {
263+
t.Errorf("SearchMemory() error = %v", err)
264+
return
265+
}
266+
}
267+
}()
268+
}
269+
wg.Wait()
270+
}

0 commit comments

Comments
 (0)