Skip to content

Commit

Permalink
Use the daemon context when handling a stream so that it properly han…
Browse files Browse the repository at this point in the history
…dles the shutdown and does not wait for a deadline
  • Loading branch information
alexsporn committed Feb 21, 2024
1 parent 09347be commit 1f6f25f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/network/p2p/autopeering/autopeering.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ func (m *Manager) discoverAndDialPeers() {
}

for peerAddrInfo := range peerChan {
if m.ctx.Err() != nil {
m.logger.LogDebug("Context is done, stopping dialing new peers")
return
}

if peersToFind <= 0 {
m.logger.LogDebug("Enough new autopeering peers connected")
return
Expand Down
12 changes: 11 additions & 1 deletion pkg/network/p2p/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Manager struct {
libp2pHost host.Host
peerDB *network.DB

ctx context.Context

logger log.Logger

shutdownMutex syncutils.RWMutex
Expand Down Expand Up @@ -158,6 +160,8 @@ func (m *Manager) DialPeer(ctx context.Context, peer *network.Peer) error {

// Start starts the manager and initiates manual- and autopeering.
func (m *Manager) Start(ctx context.Context, networkID string) error {
m.ctx = ctx

m.manualPeering.Start()

if m.autoPeering.MaxNeighbors() > 0 {
Expand Down Expand Up @@ -283,6 +287,12 @@ func (m *Manager) handleStream(stream p2pnetwork.Stream) {

m.allowPeerMutex.Lock()
defer m.allowPeerMutex.Unlock()
if m.ctx.Err() != nil {
m.logger.LogDebugf("aborting handling stream, context is done")
m.closeStream(stream)

return
}

peerID := stream.Conn().RemotePeer()

Expand Down Expand Up @@ -314,7 +324,7 @@ func (m *Manager) handleStream(stream p2pnetwork.Stream) {
return
}

if err := m.addNeighbor(context.Background(), networkPeer, ps); err != nil {
if err := m.addNeighbor(m.ctx, networkPeer, ps); err != nil {
m.logger.LogErrorf("failed to add neighbor, peerID: %s, error: %s", peerID, err)
m.closeStream(stream)

Expand Down

0 comments on commit 1f6f25f

Please sign in to comment.