From eea62427429eee4175f7f276211d0dffa1266218 Mon Sep 17 00:00:00 2001 From: cui Date: Thu, 11 Jun 2026 04:37:39 +0800 Subject: [PATCH] log: share mutex in log handles with same writer and other fields (#35074) In old code, mu is struct not pointer, it caused create new mutex event with same writer. Change to use pointer to sync.Mutex, so that the mutex is shared between handler with same writer. --- log/handler.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/log/handler.go b/log/handler.go index 9c98552e7c..0b51120e51 100644 --- a/log/handler.go +++ b/log/handler.go @@ -37,7 +37,7 @@ func (h *discardHandler) WithAttrs(attrs []slog.Attr) slog.Handler { } type TerminalHandler struct { - mu sync.Mutex + mu *sync.Mutex wr io.Writer lvl slog.Level useColor bool @@ -66,6 +66,7 @@ func NewTerminalHandler(wr io.Writer, useColor bool) *TerminalHandler { // records which are less than or equal to the specified verbosity level. func NewTerminalHandlerWithLevel(wr io.Writer, lvl slog.Level, useColor bool) *TerminalHandler { return &TerminalHandler{ + mu: &sync.Mutex{}, wr: wr, lvl: lvl, useColor: useColor, @@ -92,6 +93,7 @@ func (h *TerminalHandler) WithGroup(name string) slog.Handler { func (h *TerminalHandler) WithAttrs(attrs []slog.Attr) slog.Handler { return &TerminalHandler{ + mu: h.mu, wr: h.wr, lvl: h.lvl, useColor: h.useColor,