fix: concurrent writes to executor stats (#7731)

Signed-off-by: Spike Curtis <spike@coder.com>
This commit is contained in:
Spike Curtis
2023-06-01 09:23:51 +04:00
committed by GitHub
parent 9810339fd6
commit a46e8f2e49

View File

@ -3,6 +3,7 @@ package executor
import (
"context"
"database/sql"
"sync"
"sync/atomic"
"time"
@ -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
@ -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))