Skip to content

Commit 04bf1c8

Browse files
authored
eth/protocols/snap, internal/testlog: fix dataraces (#29301)
1 parent 8f7fbdf commit 04bf1c8

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

eth/protocols/snap/sync_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,8 +1873,9 @@ func verifyTrie(scheme string, db ethdb.KeyValueStore, root common.Hash, t *test
18731873
// TestSyncAccountPerformance tests how efficient the snap algo is at minimizing
18741874
// state healing
18751875
func TestSyncAccountPerformance(t *testing.T) {
1876-
t.Parallel()
1877-
1876+
// These tests must not run in parallel: they modify the
1877+
// global var accountConcurrency
1878+
// t.Parallel()
18781879
testSyncAccountPerformance(t, rawdb.HashScheme)
18791880
testSyncAccountPerformance(t, rawdb.PathScheme)
18801881
}

internal/testlog/testlog.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ type bufHandler struct {
4747
buf []slog.Record
4848
attrs []slog.Attr
4949
level slog.Level
50+
mu sync.Mutex
5051
}
5152

5253
func (h *bufHandler) Handle(_ context.Context, r slog.Record) error {
54+
h.mu.Lock()
55+
defer h.mu.Unlock()
5356
h.buf = append(h.buf, r)
5457
return nil
5558
}
@@ -59,12 +62,14 @@ func (h *bufHandler) Enabled(_ context.Context, lvl slog.Level) bool {
5962
}
6063

6164
func (h *bufHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
65+
h.mu.Lock()
66+
defer h.mu.Unlock()
6267
records := make([]slog.Record, len(h.buf))
6368
copy(records[:], h.buf[:])
6469
return &bufHandler{
65-
records,
66-
append(h.attrs, attrs...),
67-
h.level,
70+
buf: records,
71+
attrs: append(h.attrs, attrs...),
72+
level: h.level,
6873
}
6974
}
7075

@@ -75,9 +80,9 @@ func (h *bufHandler) WithGroup(_ string) slog.Handler {
7580
// Logger returns a logger which logs to the unit test log of t.
7681
func Logger(t *testing.T, level slog.Level) log.Logger {
7782
handler := bufHandler{
78-
[]slog.Record{},
79-
[]slog.Attr{},
80-
level,
83+
buf: []slog.Record{},
84+
attrs: []slog.Attr{},
85+
level: level,
8186
}
8287
return &logger{
8388
t: t,
@@ -200,6 +205,8 @@ func (h *bufHandler) terminalFormat(r slog.Record) string {
200205
// flush writes all buffered messages and clears the buffer.
201206
func (l *logger) flush() {
202207
l.t.Helper()
208+
l.h.mu.Lock()
209+
defer l.h.mu.Unlock()
203210
for _, r := range l.h.buf {
204211
l.t.Logf("%s", l.h.terminalFormat(r))
205212
}

0 commit comments

Comments
 (0)