From e76a5c4450e47f607abd9161feb7700ec944a750 Mon Sep 17 00:00:00 2001 From: Alisdair MacLeod Date: Thu, 19 Mar 2020 09:29:19 +0000 Subject: [PATCH 1/4] create test to prove issue sirupsen/logrus#954 --- entry_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/entry_test.go b/entry_test.go index 423e5ed..30eddae 100644 --- a/entry_test.go +++ b/entry_test.go @@ -243,3 +243,14 @@ func TestEntryLogfLevel(t *testing.T) { entry.Logf(WarnLevel, "%s", "warn") assert.Contains(t, buffer.String(), "warn") } + +func TestEntryReportCallerRace(t *testing.T) { + logger := New() + entry := NewEntry(logger) + go func() { + logger.SetReportCaller(true) + }() + go func() { + entry.Info("should not race") + }() +} From b28acda22db49a5e5ff39f3c23968b02a3585dda Mon Sep 17 00:00:00 2001 From: Alisdair MacLeod Date: Thu, 19 Mar 2020 09:32:08 +0000 Subject: [PATCH 2/4] fix race condition in entry --- entry.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/entry.go b/entry.go index 1bf127c..3928671 100644 --- a/entry.go +++ b/entry.go @@ -219,6 +219,8 @@ func (entry Entry) HasCaller() (has bool) { // This function is not declared with a pointer value because otherwise // race conditions will occur when using multiple goroutines func (entry Entry) log(level Level, msg string) { + entry.Logger.mu.Lock() + defer entry.Logger.mu.Unlock() var buffer *bytes.Buffer // Default to now, but allow users to override if they want. From ba670baee189f68debc2d23e69b848d46436916a Mon Sep 17 00:00:00 2001 From: Alisdair MacLeod Date: Thu, 19 Mar 2020 10:01:29 +0000 Subject: [PATCH 3/4] fix deadlock in previous entry race condition fix --- entry.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/entry.go b/entry.go index 3928671..5082cfa 100644 --- a/entry.go +++ b/entry.go @@ -219,8 +219,7 @@ func (entry Entry) HasCaller() (has bool) { // This function is not declared with a pointer value because otherwise // race conditions will occur when using multiple goroutines func (entry Entry) log(level Level, msg string) { - entry.Logger.mu.Lock() - defer entry.Logger.mu.Unlock() + var buffer *bytes.Buffer // Default to now, but allow users to override if they want. @@ -234,9 +233,11 @@ func (entry Entry) log(level Level, msg string) { entry.Level = level entry.Message = msg + entry.Logger.mu.Lock() if entry.Logger.ReportCaller { entry.Caller = getCaller() } + entry.Logger.mu.Unlock() entry.fireHooks() From e3e40605a2a706827801190746fd3ae99f6f4872 Mon Sep 17 00:00:00 2001 From: Alisdair MacLeod Date: Thu, 19 Mar 2020 10:02:20 +0000 Subject: [PATCH 4/4] remove errant whitespace --- entry.go | 1 - 1 file changed, 1 deletion(-) diff --git a/entry.go b/entry.go index 5082cfa..c6da562 100644 --- a/entry.go +++ b/entry.go @@ -219,7 +219,6 @@ func (entry Entry) HasCaller() (has bool) { // This function is not declared with a pointer value because otherwise // race conditions will occur when using multiple goroutines func (entry Entry) log(level Level, msg string) { - var buffer *bytes.Buffer // Default to now, but allow users to override if they want.