Skip to content

slog/log: Concurrent writes to TextHandler and JSONHandler lose log lines.  #61321

@WillAbides

Description

@WillAbides

What version of Go are you using (go version)?

$ go version
go version go1.21rc2 darwin/amd64

Does this issue reproduce with the latest release?

It reproduces on go1.21rc2, and on go1.20 if I use "golang.org/x/exp/slog"
instead of "log/slog"

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/wroden/Library/Caches/go-build'
GOENV='/Users/wroden/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/wroden/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/wroden/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/wroden/go/go1.21rc2'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/wroden/go/go1.21rc2/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.21rc2'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/bm/vj4qgj_n737bj6l4b2wkf08h0000gn/T/go-build3015726571=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I logged a lot of messages from concurrent goroutines to a slog.Logger and children
created by logger.With(...). See the play link for more details.

play: https://go.dev/play/p/Uhzxkd5e-a7?v=gotip

What did you expect to see?

I expected to see all the logged messages in the logger's output.

What did you see instead?

Most of the logged messages, but not all of them.

The only way I could get it to reliably log all messages was to use one mutex for all the loggers. Separate mutexes for each child logger did not help.

What else?

To check whether this was a problem with the handler or the logger, I also tried
calling handler.Handle() directly. That showed the problem is in the handler
and happens on both of the handlers provided by the slog package.

In experimenting with dummy handlers, the only way I found to avoid this is
adding a mutex to the handler. I'm not sure if that's the best solution, but it
does seem to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions