-
Notifications
You must be signed in to change notification settings - Fork 584
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 conditions in logging package functions and enable race detection in tests #1101
Fix race conditions in logging package functions and enable race detection in tests #1101
Conversation
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.
just a minor nit.
pkg/logging/logging.go
Outdated
} | ||
|
||
func init() { | ||
loggingStderr = true | ||
loggingW = nil | ||
loggingLevel = PanicLevel | ||
logger = &lumberjack.Logger{} | ||
|
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.
No need for this, right ?
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.
my bad missed it :) i removed it
92fd0d0
to
24a2911
Compare
pkg/logging/logging.go
Outdated
var updatedLogger lumberjack.Logger = lumberjack.Logger{Filename: logger.Filename, MaxAge: logger.MaxAge, MaxBackups: logger.MaxBackups, | ||
Compress: logger.Compress, MaxSize: logger.MaxSize, LocalTime: logger.LocalTime} |
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.
sorry for not raising it before, but do you really need to initialize these ? You're over-writing the defaults just below.
I mean, couldn't you just:
var updatedLogger lumberjack.Logger = lumberjack.Logger{
Filename: logger.Filename,
MaxAge: 5,
MaxBackups: 5,
Compress: true,
MaxSize: 100,
LocalTime: logger.LocalTime
}
if options != nil {
...
}
...
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.
glad you raised it, changed as you suggested :)
pkg/logging/logging.go
Outdated
var updatedLogger lumberjack.Logger = lumberjack.Logger{Filename: filename, MaxAge: logger.MaxAge, MaxBackups: logger.MaxBackups, | ||
Compress: logger.Compress, MaxSize: logger.MaxSize, LocalTime: logger.LocalTime} |
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.
please use one attribute per line
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.
Done
24a2911
to
9d64562
Compare
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.
@dougbtv can you approve running the tests ?...
I don't know why, but it seems I can't trigger those manually.
Oh and thanks for the PR! @AlinaSecret Good catch :) |
The logging package contains two functions, SetLogOptions and SetLogFile, that could experience race conditions when multiple goroutines access and modify the logger struct concurrently. To address these issues, a copy of the logger struct is now created in each function to eliminate data races. In addition, the test-go.sh script is updated to include the '-race' flag, enabling race detection during testing. This change helps prevent future race conditions by activating the Go race detector. Signed-off-by: Alina Sudakov <[email protected]>
9d64562
to
272b3ca
Compare
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.
LGTM! Thanks for the contribution.
This PR addresses the race conditions discovered during local testing using the Go race command. The race command detected potential data race conditions in the logging package's SetLogOptions and SetLogFile functions when multiple goroutines accessed and modified the shared logger struct concurrently.
Before applying the fix, the race detector logs indicated conflicting accesses as can be seen here:
To resolve these race conditions, a copy of the logger struct is now created within each function.
This PR enables race detection in unit tests by modifying the the test-go.sh script . The test configuration is updated to include the '-race' flag, allowing thorough race detection during testing.
for further reference about race detection view: https://go.dev/doc/articles/race_detector)