Skip to content

Commit a7939fa

Browse files
authored
swarm: add a default timeout to conn.NewStream (#2907)
1 parent e2e0d29 commit a7939fa

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

p2p/net/swarm/swarm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const (
3232
// This includes the time between dialing the raw network connection,
3333
// protocol selection as well the handshake, if applicable.
3434
defaultDialTimeoutLocal = 5 * time.Second
35+
36+
defaultNewStreamTimeout = 15 * time.Second
3537
)
3638

3739
var log = logging.Logger("swarm2")

p2p/net/swarm/swarm_conn.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,18 @@ func (c *Conn) NewStream(ctx context.Context) (network.Stream, error) {
209209
return nil, err
210210
}
211211

212+
if _, ok := ctx.Deadline(); !ok {
213+
var cancel context.CancelFunc
214+
ctx, cancel = context.WithTimeout(ctx, defaultNewStreamTimeout)
215+
defer cancel()
216+
}
217+
212218
s, err := c.openAndAddStream(ctx, scope)
213219
if err != nil {
214220
scope.Done()
221+
if errors.Is(err, context.DeadlineExceeded) {
222+
err = fmt.Errorf("timed out: %w", err)
223+
}
215224
return nil, err
216225
}
217226
return s, nil

0 commit comments

Comments
 (0)