Skip to content

Commit a0806b2

Browse files
authored
Merge pull request #2735 from headlamp-k8s/races
backend: Fix two races
2 parents ba4555e + 192faac commit a0806b2

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

backend/cmd/multiplexer_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ func TestUpdateStatus(t *testing.T) {
200200
conn := &Connection{
201201
Status: ConnectionStatus{},
202202
Done: make(chan struct{}),
203+
mu: sync.RWMutex{},
203204
}
204205

205206
// Test different state transitions
@@ -211,7 +212,9 @@ func TestUpdateStatus(t *testing.T) {
211212
}
212213

213214
for _, state := range states {
215+
conn.mu.Lock()
214216
conn.Status.State = state
217+
conn.mu.Unlock()
215218
assert.Equal(t, state, conn.Status.State)
216219
}
217220

@@ -222,15 +225,18 @@ func TestUpdateStatus(t *testing.T) {
222225

223226
go func(i int) {
224227
defer wg.Done()
225-
228+
conn.mu.Lock()
226229
state := states[i%len(states)]
227230
conn.Status.State = state
231+
conn.mu.Unlock()
228232
}(i)
229233
}
230234
wg.Wait()
231235

232236
// Verify final state is valid
237+
conn.mu.RLock()
233238
assert.Contains(t, states, conn.Status.State)
239+
conn.mu.RUnlock()
234240
}
235241

236242
func TestMonitorConnection_Reconnect(t *testing.T) {

backend/pkg/cache/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ func (c *cache[T]) cleanUp() {
126126
for {
127127
<-ticker.C
128128

129+
c.lock.Lock()
129130
for key, value := range c.store {
130131
if !value.expiresAt.IsZero() && value.expiresAt.Before(time.Now()) {
131-
c.lock.Lock()
132132
delete(c.store, key)
133-
c.lock.Unlock()
134133
}
135134
}
135+
c.lock.Unlock()
136136
}
137137
}
138138

0 commit comments

Comments
 (0)