Skip to content
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

eth/protocols/snap, internal/testlog: fix dataraces #29301

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions eth/protocols/snap/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1873,8 +1873,9 @@ func verifyTrie(scheme string, db ethdb.KeyValueStore, root common.Hash, t *test
// TestSyncAccountPerformance tests how efficient the snap algo is at minimizing
// state healing
func TestSyncAccountPerformance(t *testing.T) {
t.Parallel()

// These tests must not run in parallel: they modify the
// global var accountConcurrency
// t.Parallel()
testSyncAccountPerformance(t, rawdb.HashScheme)
testSyncAccountPerformance(t, rawdb.PathScheme)
}
Expand Down
19 changes: 13 additions & 6 deletions internal/testlog/testlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ type bufHandler struct {
buf []slog.Record
attrs []slog.Attr
level slog.Level
mu sync.Mutex
}

func (h *bufHandler) Handle(_ context.Context, r slog.Record) error {
h.mu.Lock()
defer h.mu.Unlock()
h.buf = append(h.buf, r)
return nil
}
Expand All @@ -59,12 +62,14 @@ func (h *bufHandler) Enabled(_ context.Context, lvl slog.Level) bool {
}

func (h *bufHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
h.mu.Lock()
defer h.mu.Unlock()
records := make([]slog.Record, len(h.buf))
copy(records[:], h.buf[:])
return &bufHandler{
records,
append(h.attrs, attrs...),
h.level,
buf: records,
attrs: append(h.attrs, attrs...),
level: h.level,
}
}

Expand All @@ -75,9 +80,9 @@ func (h *bufHandler) WithGroup(_ string) slog.Handler {
// Logger returns a logger which logs to the unit test log of t.
func Logger(t *testing.T, level slog.Level) log.Logger {
handler := bufHandler{
[]slog.Record{},
[]slog.Attr{},
level,
buf: []slog.Record{},
attrs: []slog.Attr{},
level: level,
}
return &logger{
t: t,
Expand Down Expand Up @@ -200,6 +205,8 @@ func (h *bufHandler) terminalFormat(r slog.Record) string {
// flush writes all buffered messages and clears the buffer.
func (l *logger) flush() {
l.t.Helper()
l.h.mu.Lock()
defer l.h.mu.Unlock()
for _, r := range l.h.buf {
l.t.Logf("%s", l.h.terminalFormat(r))
}
Expand Down
Loading