fix: obey new stream timeout

This commit is contained in:
Steven Allen
2020-12-08 13:57:39 -08:00
parent fcf69647e9
commit 024f1af9ae
2 changed files with 61 additions and 3 deletions
+17 -3
View File
@@ -631,10 +631,24 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I
}, nil
}
selected, err := msmux.SelectOneOf(pidStrings, 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(pidStrings, 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 the negotiation to cancel.
<-errCh
return nil, ctx.Err()
}
selpid := protocol.ID(selected)