1
0
mirror of https://github.com/golang/go synced 2024-11-17 07:54:41 -07:00

log/slog: fix issue with concurrent writes

This causes instances commonHandler created by withAttrs or withGroup to
share a mutex with their parent preventing concurrent writes to their
shared writer.

Fixes #61321

Change-Id: Ieec225e88ad51c84b41bad6c409fac48c90320b2
Reviewed-on: https://go-review.googlesource.com/c/go/+/509196
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Will Roden 2023-07-12 17:23:09 -05:00 committed by Jonathan Amsterdam
parent c30faf9c54
commit 847d40d699
4 changed files with 53 additions and 1 deletions

View File

@ -193,7 +193,7 @@ type commonHandler struct {
groupPrefix string groupPrefix string
groups []string // all groups started from WithGroup groups []string // all groups started from WithGroup
nOpenGroups int // the number of groups opened in preformattedAttrs nOpenGroups int // the number of groups opened in preformattedAttrs
mu sync.Mutex mu *sync.Mutex
w io.Writer w io.Writer
} }
@ -207,6 +207,7 @@ func (h *commonHandler) clone() *commonHandler {
groups: slices.Clip(h.groups), groups: slices.Clip(h.groups),
nOpenGroups: h.nOpenGroups, nOpenGroups: h.nOpenGroups,
w: h.w, w: h.w,
mu: h.mu, // mutex shared among all clones of this handler
} }
} }

View File

@ -16,6 +16,7 @@ import (
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
) )
@ -106,6 +107,52 @@ func TestDefaultHandle(t *testing.T) {
} }
} }
func TestConcurrentWrites(t *testing.T) {
ctx := context.Background()
count := 1000
for _, handlerType := range []string{"text", "json"} {
t.Run(handlerType, func(t *testing.T) {
var buf bytes.Buffer
var h Handler
switch handlerType {
case "text":
h = NewTextHandler(&buf, nil)
case "json":
h = NewJSONHandler(&buf, nil)
default:
t.Fatalf("unexpected handlerType %q", handlerType)
}
sub1 := h.WithAttrs([]Attr{Bool("sub1", true)})
sub2 := h.WithAttrs([]Attr{Bool("sub2", true)})
var wg sync.WaitGroup
for i := 0; i < count; i++ {
sub1Record := NewRecord(time.Time{}, LevelInfo, "hello from sub1", 0)
sub1Record.AddAttrs(Int("i", i))
sub2Record := NewRecord(time.Time{}, LevelInfo, "hello from sub2", 0)
sub2Record.AddAttrs(Int("i", i))
wg.Add(1)
go func() {
defer wg.Done()
if err := sub1.Handle(ctx, sub1Record); err != nil {
t.Error(err)
}
if err := sub2.Handle(ctx, sub2Record); err != nil {
t.Error(err)
}
}()
}
wg.Wait()
for i := 1; i <= 2; i++ {
want := "hello from sub" + strconv.Itoa(i)
n := strings.Count(buf.String(), want)
if n != count {
t.Fatalf("want %d occurrences of %q, got %d", count, want, n)
}
}
})
}
}
// Verify the common parts of TextHandler and JSONHandler. // Verify the common parts of TextHandler and JSONHandler.
func TestJSONAndTextHandlers(t *testing.T) { func TestJSONAndTextHandlers(t *testing.T) {
// remove all Attrs // remove all Attrs

View File

@ -13,6 +13,7 @@ import (
"io" "io"
"log/slog/internal/buffer" "log/slog/internal/buffer"
"strconv" "strconv"
"sync"
"time" "time"
"unicode/utf8" "unicode/utf8"
) )
@ -35,6 +36,7 @@ func NewJSONHandler(w io.Writer, opts *HandlerOptions) *JSONHandler {
json: true, json: true,
w: w, w: w,
opts: *opts, opts: *opts,
mu: &sync.Mutex{},
}, },
} }
} }

View File

@ -11,6 +11,7 @@ import (
"io" "io"
"reflect" "reflect"
"strconv" "strconv"
"sync"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
) )
@ -33,6 +34,7 @@ func NewTextHandler(w io.Writer, opts *HandlerOptions) *TextHandler {
json: false, json: false,
w: w, w: w,
opts: *opts, opts: *opts,
mu: &sync.Mutex{},
}, },
} }
} }