Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: Fix two races #2735

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading