Skip to content

Commit

Permalink
Merge pull request sirupsen#1234 from sirupsen/dbd-cleanup
Browse files Browse the repository at this point in the history
fix race condition AddHook and traces
  • Loading branch information
dgsb authored Feb 18, 2021
2 parents d59e561 + d172886 commit 6cff360
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,15 @@ func (entry *Entry) log(level Level, msg string) {
}

func (entry *Entry) fireHooks() {
err := entry.Logger.Hooks.Fire(entry.Level, entry)
var tmpHooks LevelHooks
entry.Logger.mu.Lock()
tmpHooks = make(LevelHooks, len(entry.Logger.Hooks))
for k, v := range entry.Logger.Hooks {
tmpHooks[k] = v
}
entry.Logger.mu.Unlock()

err := tmpHooks.Fire(entry.Level, entry)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
}
Expand Down
16 changes: 16 additions & 0 deletions hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package logrus_test
import (
"bytes"
"encoding/json"
"fmt"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

. "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
. "github.com/sirupsen/logrus/internal/testutils"
)

Expand Down Expand Up @@ -191,6 +193,20 @@ func TestAddHookRace(t *testing.T) {
})
}

func TestAddHookRace2(t *testing.T) {
t.Parallel()

for i := 0; i < 3; i++ {
testname := fmt.Sprintf("Test %d", i)
t.Run(testname, func(t *testing.T) {
t.Parallel()

_ = test.NewGlobal()
Info(testname)
})
}
}

type HookCallFunc struct {
F func()
}
Expand Down

0 comments on commit 6cff360

Please sign in to comment.