Skip to content
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

Add comment about ctxzap.AddFields not being concurrent-safe #732

Conversation

hexdigest
Copy link

@hexdigest hexdigest commented Nov 25, 2024

Changes

Added comment about ctxzap.AddFields not being concurrent-safe

Verification

This assignment is not concurrent-safe:

l.fields = append(l.fields, fields...)

This might result in an AddTo call on an empty zapcore.Field and cause panic here:
https://github.com/uber-go/zap/blob/master/zapcore/field.go#L180

panic: unknown field type: { 0 0 <nil>}

Let me know if you guys want me to actually fix the issue because I think we might not want to introduce a lock here.

@hexdigest
Copy link
Author

hexdigest commented Nov 25, 2024

This 👇 test is flaky.

@johanbrandhorst
Copy link
Collaborator

  1. Thanks for the PR! I don't know this code that well, does it look like it should be protected? I was sort of assuming that loggers are request scoped anyway so it shouldn't matter here, but I could be wrong.
  2. Yes, that test is flaky, that is troubling. If you can figure out what's wrong I'll happily review a PR :).

@hexdigest
Copy link
Author

  1. I was sort of assuming that loggers are request scoped anyway so it shouldn't matter here, but I could be wrong.

They're usually request scoped you're right. However, it doesn't mean that code that handles the request is completely linear. You can query let's say third parties and/or databases concurrently in order to handle request (and this is also something very typical). So each goroutine that participates in request handling technically can call AddFields func and cause panic. And this is something that happened to me many times already and was hard to reproduce locally, hence the PR.

The panic itself is a different topic, I believe that it shouldn't be there in the first place.

@johanbrandhorst
Copy link
Collaborator

I think the docs change is good for now, I don't think we want to make this concurrency safe by default. Hope that makes sense!

@johanbrandhorst johanbrandhorst merged commit 33d2050 into grpc-ecosystem:v1 Nov 26, 2024
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants