-
Notifications
You must be signed in to change notification settings - Fork 18k
slog/log: Concurrent writes to TextHandler and JSONHandler lose log lines. #61321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The test is racing. The counts get a little better with |
(attn @jba) |
@AndrewHarrisSPU I agree that there is a race writing to buf, but I think it's the handler's responsibility to guard against that race. The doc says:
commonHandler already has a mutex guarding against concurrent writes to the output. It just doesn't extend that to child handlers. |
Thanks everyone. 20 seconds after reading "it just doesn't extend that to child handlers" I realized there was a serious bug, which I hadn't noticed in many months of development. To elaborate on what @WillAbides said, the handler implementation has two fields, a One fix is to change the |
@jba I've been fiddling with it here and got it working by changing mu to a I would be happy to submit a change. |
Yeah, that's my option 1. Would love to take your change! Thanks! |
Will do. I'll have something pushed shortly. |
This is still going to be a problem for people who make two top-level loggers that use the same io.Writer. I don't think there's a simple fix for that. (The We ultimately want something like /cc @bcmills |
Change https://go.dev/cl/509196 mentions this issue: |
Seems good to have caught this :)
I wonder about an additional |
The documentation for |
This causes instances commonHandler created by withAttrs or withGroup to share a mutex with their parent preventing concurrent writes to their shared writer. Fixes golang#61321 Change-Id: Ieec225e88ad51c84b41bad6c409fac48c90320b2 Reviewed-on: https://go-review.googlesource.com/c/go/+/509196 Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
This causes instances commonHandler created by withAttrs or withGroup to share a mutex with their parent preventing concurrent writes to their shared writer. Fixes golang#61321 Change-Id: Ieec225e88ad51c84b41bad6c409fac48c90320b2 Reviewed-on: https://go-review.googlesource.com/c/go/+/509196 Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
This causes instances commonHandler created by withAttrs or withGroup to share a mutex with their parent preventing concurrent writes to their shared writer. Fixes golang#61321 Change-Id: Ieec225e88ad51c84b41bad6c409fac48c90320b2 Reviewed-on: https://go-review.googlesource.com/c/go/+/509196 Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
What version of Go are you using (
go version
)?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
OutputWhat 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 handlerand 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.
The text was updated successfully, but these errors were encountered: