Skip to content

Commit

Permalink
fix: use a sync.Map to prevent "concurrent map read and map write" fa…
Browse files Browse the repository at this point in the history
…tal error when using "output: prefixed"
  • Loading branch information
tinmrn committed Nov 18, 2024
1 parent c28eb20 commit 65977a9
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/output/prefixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import (
"fmt"
"io"
"strings"
"sync"

"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/templater"
)

type Prefixed struct {
logger *logger.Logger
seen map[string]uint
seen *sync.Map
counter *uint
}

func NewPrefixed(logger *logger.Logger) Prefixed {
var counter uint

return Prefixed{
seen: make(map[string]uint),
seen: &sync.Map{},
counter: &counter,
logger: logger,
}
Expand Down Expand Up @@ -85,11 +86,11 @@ func (pw *prefixWriter) writeLine(line string) error {
line += "\n"
}

idx, ok := pw.prefixed.seen[pw.prefix]
idx, ok := pw.prefixed.seen.Load(pw.prefix)

if !ok {
idx = *pw.prefixed.counter
pw.prefixed.seen[pw.prefix] = idx
pw.prefixed.seen.Store(pw.prefix, idx)

*pw.prefixed.counter++
}
Expand All @@ -98,7 +99,7 @@ func (pw *prefixWriter) writeLine(line string) error {
return nil
}

color := PrefixColorSequence[idx%uint(len(PrefixColorSequence))]
color := PrefixColorSequence[idx.(uint)%uint(len(PrefixColorSequence))]
pw.prefixed.logger.FOutf(pw.writer, color, pw.prefix)

if _, err := fmt.Fprint(pw.writer, "] "); err != nil {
Expand Down

0 comments on commit 65977a9

Please sign in to comment.