-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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 for entry data field race condition #1229
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,8 +78,20 @@ func NewEntry(logger *Logger) *Entry { | |
} | ||
} | ||
|
||
func (entry *Entry) Dup() *Entry { | ||
data := make(Fields, len(entry.Data)) | ||
for k, v := range entry.Data { | ||
data[k] = v | ||
} | ||
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, Context: entry.Context, err: entry.err} | ||
} | ||
|
||
// Returns the bytes representation of this entry from the formatter. | ||
func (entry *Entry) Bytes() ([]byte, error) { | ||
return entry.bytes_nolock() | ||
} | ||
|
||
func (entry *Entry) bytes_nolock() ([]byte, error) { | ||
return entry.Logger.Formatter.Format(entry) | ||
} | ||
|
||
|
@@ -212,68 +224,68 @@ func (entry Entry) HasCaller() (has bool) { | |
|
||
// This function is not declared with a pointer value because otherwise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment should be updated since it's a pointer method now. Is it safe to use via pointer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it is safe, the comment should indeed be updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wanted to point out since I was checking back through this that it's safe because of the Hooks are now safe to modify entry because any log call on entry duplicates the entire entry. |
||
// race conditions will occur when using multiple goroutines | ||
func (entry Entry) log(level Level, msg string) { | ||
func (entry *Entry) log(level Level, msg string) { | ||
var buffer *bytes.Buffer | ||
|
||
// Default to now, but allow users to override if they want. | ||
// | ||
// We don't have to worry about polluting future calls to Entry#log() | ||
// with this assignment because this function is declared with a | ||
// non-pointer receiver. | ||
if entry.Time.IsZero() { | ||
entry.Time = time.Now() | ||
newEntry := entry.Dup() | ||
|
||
if newEntry.Time.IsZero() { | ||
newEntry.Time = time.Now() | ||
} | ||
|
||
entry.Level = level | ||
entry.Message = msg | ||
entry.Logger.mu.Lock() | ||
if entry.Logger.ReportCaller { | ||
entry.Caller = getCaller() | ||
newEntry.Level = level | ||
newEntry.Message = msg | ||
|
||
newEntry.Logger.mu.Lock() | ||
reportCaller := newEntry.Logger.ReportCaller | ||
newEntry.Logger.mu.Unlock() | ||
|
||
if reportCaller { | ||
newEntry.Caller = getCaller() | ||
} | ||
entry.Logger.mu.Unlock() | ||
|
||
entry.fireHooks() | ||
newEntry.fireHooks() | ||
|
||
buffer = getBuffer() | ||
defer func() { | ||
entry.Buffer = nil | ||
newEntry.Buffer = nil | ||
putBuffer(buffer) | ||
}() | ||
buffer.Reset() | ||
entry.Buffer = buffer | ||
newEntry.Buffer = buffer | ||
|
||
entry.write() | ||
newEntry.write() | ||
|
||
entry.Buffer = nil | ||
newEntry.Buffer = nil | ||
|
||
// To avoid Entry#log() returning a value that only would make sense for | ||
// panic() to use in Entry#Panic(), we avoid the allocation by checking | ||
// directly here. | ||
if level <= PanicLevel { | ||
panic(&entry) | ||
panic(newEntry) | ||
} | ||
} | ||
|
||
func (entry *Entry) fireHooks() { | ||
entry.Logger.mu.Lock() | ||
defer entry.Logger.mu.Unlock() | ||
err := entry.Logger.Hooks.Fire(entry.Level, entry) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err) | ||
} | ||
} | ||
|
||
func (entry *Entry) write() { | ||
entry.Logger.mu.Lock() | ||
defer entry.Logger.mu.Unlock() | ||
serialized, err := entry.Logger.Formatter.Format(entry) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err) | ||
return | ||
} | ||
if _, err = entry.Logger.Out.Write(serialized); err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err) | ||
} | ||
func() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This extra function wrapper isn't required here since the write does nothing else after writing. ie the lock doesn't require it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it's a leftover from temporary step, I will remove it |
||
entry.Logger.mu.Lock() | ||
defer entry.Logger.mu.Unlock() | ||
if _, err := entry.Logger.Out.Write(serialized); err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err) | ||
} | ||
}() | ||
} | ||
|
||
func (entry *Entry) Log(level Level, args ...interface{}) { | ||
|
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.
Why not copy
caller
into new entry? There are some scenarios where I want to pass the caller by myself, for example, many libraries accept a logger interface and use it to output internal log(such as SQL exec log forGorm
), so many users wrap logrus to the needed interface, but the caller got bylogrus
is inaccuracy, and I get the caller and pass it intoEntry
, and this change breaks my wrapped logger for these libraries.Can I send a PR for copying caller in
Entry.Dup
util? What do you think of it? Thanks! @dgsb @epelc