Skip to content
Open
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
3 changes: 2 additions & 1 deletion p2p/peer_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package p2p

import (
"context"
"errors"
"sync"
"time"

Expand Down Expand Up @@ -268,7 +269,7 @@ func (p *peerTracker) dumpPeers(ctx context.Context) {
}
p.peerLk.RUnlock()

ctx, cancel := context.WithTimeout(ctx, time.Second*5)
ctx, cancel := context.WithTimeoutCause(ctx, time.Second*5, errors.New("dumping peers timeout "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get the cause, it needs to be unwrapped via context.Cause. The context is used for the Put operation, so we should unwrap the cause in the log next to it.

defer cancel()

err := p.pidstore.Put(ctx, peers)
Expand Down
2 changes: 1 addition & 1 deletion p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (serv *ExchangeServer[H]) requestHandler(stream network.Stream) {
log.Error(err)
}

ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RequestTimeout)
ctx, cancel := context.WithTimeoutCause(serv.ctx, serv.Params.RequestTimeout, errors.New("server "+string(serv.host.ID())+": request timed out "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, we need to unwrap the cause for this to have any effect.

defer cancel()

var headers []H
Expand Down
3 changes: 2 additions & 1 deletion p2p/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package p2p

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -85,7 +86,7 @@ func TestExchangeServer_Timeout(t *testing.T) {
{
name: "handleHeadRequest",
fn: func() error {
ctx, cancel := context.WithTimeout(context.Background(), testRequestTimeout)
ctx, cancel := context.WithTimeoutCause(context.Background(), testRequestTimeout, errors.New(" test: server request timeout"))
defer cancel()

_, err := server.handleHeadRequest(ctx)
Expand Down
2 changes: 1 addition & 1 deletion p2p/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (s *session[H]) doRequest(
))
defer span.End()

ctx, cancel := context.WithTimeout(ctx, s.requestTimeout)
ctx, cancel := context.WithTimeoutCause(ctx, s.requestTimeout, errors.New("peer "+string(stat.peerID)+": request timed out"))
defer cancel()

start := time.Now()
Expand Down
2 changes: 1 addition & 1 deletion sync/syncer_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *Syncer[H]) networkHead(ctx context.Context) (H, bool, error) {
log.Warnw("attempting to request the most recent network head...")

// cap the max blocking time for the request call
ctx, cancel := context.WithTimeout(ctx, NetworkHeadRequestTimeout)
ctx, cancel := context.WithTimeoutCause(ctx, NetworkHeadRequestTimeout, errors.New("network head request time out "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract error to a global public var

defer cancel()

newHead, err := s.head.Head(ctx, header.WithTrustedHead[H](sbjHead))
Expand Down