-
Notifications
You must be signed in to change notification settings - Fork 20.5k
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
log: fix data race in RotatingFileHandler #17665
Conversation
log/handler.go
Outdated
// the values of any lazy fn to the result of its execution | ||
hadErr := false | ||
for i := 1; i < len(r.Ctx); i += 2 { | ||
lz, ok := r.Ctx[i].(Lazy) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, maybe merge the two, like if lz, ok := r.Ctx[i].(Lazy); ok {
Otherwise LGTM, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I didn't change code here, just extract code from the original LazyHandler
so that it can be re-used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, it just caught my eye :) Could you please do this change in order to keep the code more compact?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
LGTM, thanks! |
log/handler.go
Outdated
|
||
return FuncHandler(func(r *Record) error { | ||
// lazy evaluate, this needs no lock. | ||
lazyEvaluateRecord(r) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new addition, right? It wasn't mentioned in the PR description. What was the result before versus after applying this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a new addition. Try to fix a data race in the original code which used StreamHandler here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, I'm trying to achieve the same effect of StreamHandler
except move the lock out so that it can protect the count
and w
of the RotatingFileHandler
I don't think all this complexity is needed/warranted. The data race can (I think) be solved by declaring the mutex as you did, but instead of lock/defer-unlock, unlock it before the |
I pushed some changes on top. @karalabe PTAL if it's in line with what you thought, and @simplyzhao PTAL if you think it there's any error in it. I pushed on top of your commits, to not steal the credit from you |
I'm unsure this PR fixes the issue now. We protect reading the count, but nowhere do we protect updating it. |
Didn't see @karalabe's comment. We need to rework this change.
We have decided to remove |
The
StreamHandler
(which containsSyncHandler
) can only protect the actualLog
operation. In RotatingFileHandler we need to protect thecount
andw
incountingWriter
.