Skip to content

Commit

Permalink
respect the context when opening a new stream in the base host
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jan 9, 2022
1 parent b3d1be8 commit ccd3f41
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions p2p/host/blank/blank_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,24 @@ func (h *BaseHost) NewStream(ctx context.Context, p peer.ID, protos ...protocol.
return nil, err
}

selected, err := msmux.SelectOneOf(protocol.ConvertToStrings(protos), s)
if err != nil {
// Negotiate the protocol in the background, obeying the context.
var selected string
errCh := make(chan error, 1)
go func() {
selected, err = msmux.SelectOneOf(protocol.ConvertToStrings(protos), s)
errCh <- err
}()
select {
case err = <-errCh:
if err != nil {
s.Reset()
return nil, err
}
case <-ctx.Done():
s.Reset()
return nil, err
// wait for `SelectOneOf` to error out because of resetting the stream.
<-errCh
return nil, ctx.Err()
}

selpid := protocol.ID(selected)
Expand Down

0 comments on commit ccd3f41

Please sign in to comment.