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

log: fix data race in RotatingFileHandler #17665

Closed
wants to merge 4 commits into from

Conversation

simplyzhao
Copy link

The StreamHandler(which contains SyncHandler) can only protect the actual Log operation. In RotatingFileHandler we need to protect the count and w in countingWriter.

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)
Copy link
Contributor

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!

Copy link
Author

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.

Copy link
Contributor

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?

Copy link
Author

Choose a reason for hiding this comment

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

Sure!

@kurkomisi
Copy link
Contributor

LGTM, thanks!

log/handler.go Outdated

return FuncHandler(func(r *Record) error {
// lazy evaluate, this needs no lock.
lazyEvaluateRecord(r)
Copy link
Contributor

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?

Copy link
Author

@simplyzhao simplyzhao Sep 20, 2018

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.

Copy link
Author

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

@karalabe
Copy link
Member

karalabe commented Jun 6, 2019

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 return h.Log(r) call. That way we don't need any lazy evaluation optimizations (which subtly changes the behavior of the logger), and this PR reduces to maybe 3 lines of code :)

@holiman
Copy link
Contributor

holiman commented Jun 13, 2019

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

@karalabe
Copy link
Member

I'm unsure this PR fixes the issue now. We protect reading the count, but nowhere do we protect updating it.

fjl
fjl previously approved these changes Jul 17, 2019
@fjl fjl dismissed their stale review July 17, 2019 09:03

Didn't see @karalabe's comment. We need to rework this change.

@karalabe karalabe modified the milestones: 1.9.1, 1.9.2 Jul 23, 2019
@karalabe karalabe modified the milestones: 1.9.2, 1.9.3 Aug 13, 2019
@karalabe karalabe modified the milestones: 1.9.3, 1.9.4 Sep 4, 2019
@karalabe karalabe modified the milestones: 1.9.4, 1.9.5 Sep 19, 2019
@fjl fjl modified the milestones: 1.9.5, 1.9.6 Sep 20, 2019
@fjl fjl modified the milestones: 1.9.6, 1.9.7 Oct 2, 2019
@karalabe karalabe modified the milestones: 1.9.7, 1.9.8 Nov 8, 2019
@karalabe karalabe modified the milestones: 1.9.8, 1.9.9 Nov 27, 2019
@karalabe karalabe modified the milestones: 1.9.9, 1.9.10 Dec 6, 2019
@adamschmideg adamschmideg assigned fjl and unassigned karalabe Jan 21, 2020
@fjl
Copy link
Contributor

fjl commented Jan 21, 2020

We have decided to remove RotatingFileHandler instead of fixing it because the handler has other issues and is not used by go-ethereum. It is removed in PR #20586.

@fjl fjl closed this Jan 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants