Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,6 @@ var _jsonPool = sync.Pool{New: func() interface{} {
return &jsonEncoder{}
}}

func getJSONEncoder() *jsonEncoder {
return _jsonPool.Get().(*jsonEncoder)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this means then that the whole pool is now useless?

Ill need to verify if still used, otherwise we maybe better removing it entierly.

}

func putJSONEncoder(enc *jsonEncoder) {
if enc.reflectBuf != nil {
enc.reflectBuf.Free()
Expand Down Expand Up @@ -546,7 +542,7 @@ func (enc *jsonEncoder) Clone() zapcore.Encoder {
}

func (enc *jsonEncoder) clone() *jsonEncoder {
clone := getJSONEncoder()
clone := &jsonEncoder{}
clone.EncoderConfig = enc.EncoderConfig
clone.spaced = enc.spaced
clone.openNamespaces = enc.openNamespaces
Expand Down
32 changes: 32 additions & 0 deletions encoder_concurrency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package logging

import (
"io"
"sync"
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// Regression test for a data race in jsonEncoder.clone(): getJSONEncoder()
// hands out a pooled *jsonEncoder that a concurrent clone() call is still
// mutating. Triggered by concurrent Logger.With + log emission on the same
// base logger (the pattern loopagent's Thread.log exercised). Run with -race.
func TestEncoderConcurrentWithAndLog(t *testing.T) {
core := zapcore.NewCore(NewEncoder(1, false), zapcore.AddSync(io.Discard), zap.DebugLevel)
base := zap.New(core).Named("base").With(zap.String("thread_id", "t-1"), zap.String("agent_id", "a-1"))

var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for j := 0; j < 2000; j++ {
derived := base.With(zap.Int("n", n))
derived.Info("hello", zap.String("j", string(rune(j))))
}
}(i)
}
wg.Wait()
}
Loading