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

p2p: fix data race in test #2875

Merged
merged 1 commit into from
Feb 12, 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
13 changes: 7 additions & 6 deletions p2p/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package p2p
import (
"context"
"sync"
"sync/atomic"
"time"

"github.com/libp2p/go-libp2p/core/host"
Expand Down Expand Up @@ -42,7 +43,7 @@ type SendReceiveFunc func(ctx context.Context, tcpNode host.Host, peerID peer.ID

var (
_ SendFunc = Send
_ SendFunc = new(Sender).SendAsync
_ SendFunc = (&Sender{}).SendAsync
)

// errorBuffer holds a slice of errors, and mutexes access to it with a sync.RWMutex.
Expand Down Expand Up @@ -82,7 +83,7 @@ func (eb *errorBuffer) trim(by int) {
}

type peerState struct {
failing bool
failing atomic.Bool
buffer errorBuffer
}

Expand All @@ -108,7 +109,7 @@ func (s *Sender) addResult(ctx context.Context, peerID peer.ID, err error) {
failure := err != nil
success := !failure

if success && state.failing {
if success && state.failing.Load() {
// See if we have senderHysteresis successes i.o.t. change state to success.
full := state.buffer.len() == senderBuffer
oldestFailure := state.buffer.get(0) != nil
Expand All @@ -121,17 +122,17 @@ func (s *Sender) addResult(ctx context.Context, peerID peer.ID, err error) {
}

if full && oldestFailure && othersSuccess {
state.failing = false
state.failing.Store(false)
log.Info(ctx, "P2P sending recovered", z.Str("peer", PeerName(peerID)))
}
} else if failure && (state.buffer.len() == 1 || !state.failing) {
} else if failure && (state.buffer.len() == 1 || !state.failing.Load()) {
// First attempt failed or state changed to failing

if _, ok := dialErrMsgs(err); !ok { // Only log non-dial errors
log.Warn(ctx, "P2P sending failing", err, z.Str("peer", PeerName(peerID)))
}

state.failing = true
state.failing.Store(true)
}

s.states.Store(peerID, state)
Expand Down
2 changes: 1 addition & 1 deletion p2p/sender_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestSenderAddResult(t *testing.T) {
if val, ok := sender.states.Load(peerID); ok {
state = val.(*peerState)
}
require.Equal(t, expect, state.failing)
require.Equal(t, expect, state.failing.Load())
}

add := func(result error) {
Expand Down
Loading