Skip to content

Commit

Permalink
Merge pull request #2735 from headlamp-k8s/races
Browse files Browse the repository at this point in the history
backend: Fix two races
  • Loading branch information
joaquimrocha authored Jan 8, 2025
2 parents ba4555e + 192faac commit a0806b2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion backend/cmd/multiplexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func TestUpdateStatus(t *testing.T) {
conn := &Connection{
Status: ConnectionStatus{},
Done: make(chan struct{}),
mu: sync.RWMutex{},
}

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

for _, state := range states {
conn.mu.Lock()
conn.Status.State = state
conn.mu.Unlock()
assert.Equal(t, state, conn.Status.State)
}

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

go func(i int) {
defer wg.Done()

conn.mu.Lock()
state := states[i%len(states)]
conn.Status.State = state
conn.mu.Unlock()
}(i)
}
wg.Wait()

// Verify final state is valid
conn.mu.RLock()
assert.Contains(t, states, conn.Status.State)
conn.mu.RUnlock()
}

func TestMonitorConnection_Reconnect(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions backend/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ func (c *cache[T]) cleanUp() {
for {
<-ticker.C

c.lock.Lock()
for key, value := range c.store {
if !value.expiresAt.IsZero() && value.expiresAt.Before(time.Now()) {
c.lock.Lock()
delete(c.store, key)
c.lock.Unlock()
}
}
c.lock.Unlock()
}
}

Expand Down

0 comments on commit a0806b2

Please sign in to comment.