From 013c86553cc9beb923a3c34b40de2a82bb8ad949 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 423e5ed9c..30eddaecc 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 9b298a80208f4c27aba5a207dabd82129a9421d8 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 1bf127c3b..3928671eb 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 5cf0721be7be721452b0bd6cb286e5d9fab3654c 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 3928671eb..5082cfa3d 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 c41966647f7803b22aa7feffdbcc950dd83ccaea 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 5082cfa3d..c6da56291 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.