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

Fix race condition in logging #4519

Merged
merged 1 commit into from
Jun 19, 2017
Merged

Conversation

exekias
Copy link
Contributor

@exekias exekias commented Jun 16, 2017

logp.FileRotator had an issue when WriteLines is called concurrently. Added a RWLock to protect both rotator.current and rotator.currentSize

`logp.FileRotator` had an issue when `WriteLines` is called concurrently
@exekias exekias force-pushed the fix-race-logging branch from 05c0746 to cdea7f0 Compare June 16, 2017 13:34
@@ -73,16 +75,26 @@ func (rotator *FileRotator) WriteLine(line []byte) error {
}

line = append(line, '\n')

rotator.currentLock.RLock()
_, err := rotator.current.Write(line)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the Write and the increment of the currentSize occur within the same lock block? Without that it seems that the currentSize may not be accurate when it is checked by other goroutines.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah you are right, I was trying to keep Write under a RLock as it may block for a while, and sacrifice some consistency in currentSize in favor of performance. Putting everything under a write lock would make goroutines logging wait on others, but yeah, this is probably overkill. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say having currentSize slightly out of date is fine, we just use it for the rotator so it's ok if that starts a bit late.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what do you think @andrewkroh? I can also add some comment explaining this

Copy link
Member

Choose a reason for hiding this comment

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

I'm OK with the behavior as is.

@andrewkroh andrewkroh merged commit fdfcaa3 into elastic:master Jun 19, 2017
@andrewkroh
Copy link
Member

@exekias Does this need back-ported to 5.6? #5131 (comment)

@exekias
Copy link
Contributor Author

exekias commented Oct 18, 2017

Didn't think about it but it may make sense?

exekias added a commit to exekias/beats that referenced this pull request Oct 18, 2017
`logp.FileRotator` had an issue when `WriteLines` is called concurrently
(cherry picked from commit fdfcaa3)
ph added a commit that referenced this pull request Oct 23, 2017
Cherry-pick #4519 to 5.6: Fix race condition in logging
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.

3 participants