Skip to content

fix: write to executor stats map while holding mutex #7731

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

Merged
merged 3 commits into from
Jun 1, 2023
Merged
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
5 changes: 5 additions & 0 deletions coderd/autobuild/executor/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package executor
import (
"context"
"database/sql"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -89,6 +90,8 @@ func (e *Executor) runOnce(t time.Time) Stats {
stats := Stats{
Transitions: make(map[uuid.UUID]database.WorkspaceTransition),
}
// we build the map of transitions concurrently, so need a mutex to serialize writes to the map
statsMu := sync.Mutex{}
defer func() {
stats.Elapsed = time.Since(t)
stats.Error = err
Expand Down Expand Up @@ -188,7 +191,9 @@ func (e *Executor) runOnce(t time.Time) Stats {
)
return nil
}
statsMu.Lock()
stats.Transitions[ws.ID] = validTransition
statsMu.Unlock()

log.Info(e.ctx, "scheduling workspace transition", slog.F("transition", validTransition))

Expand Down