Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p
Commits
024f1af9
Commit
024f1af9
authored
Dec 08, 2020
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: obey new stream timeout
parent
fcf69647
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
3 deletions
+61
-3
p2p/host/basic/basic_host.go
p2p/host/basic/basic_host.go
+17
-3
p2p/host/basic/basic_host_test.go
p2p/host/basic/basic_host_test.go
+44
-0
No files found.
p2p/host/basic/basic_host.go
View file @
024f1af9
...
...
@@ -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
)
...
...
p2p/host/basic/basic_host_test.go
View file @
024f1af9
...
...
@@ -3,6 +3,7 @@ package basichost
import
(
"bytes"
"context"
"fmt"
"io"
"reflect"
"sync"
...
...
@@ -777,6 +778,49 @@ func TestHostAddrChangeDetection(t *testing.T) {
}
}
func
TestNegotiationCancel
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
h1
,
h2
:=
getHostPair
(
ctx
,
t
)
defer
h1
.
Close
()
defer
h2
.
Close
()
// pre-negotiation so we can make the negotiation hang.
h1
.
Network
()
.
SetStreamHandler
(
func
(
s
network
.
Stream
)
{
<-
ctx
.
Done
()
// wait till the test is done.
s
.
Reset
()
})
ctx2
,
cancel2
:=
context
.
WithCancel
(
ctx
)
defer
cancel2
()
errCh
:=
make
(
chan
error
,
1
)
go
func
()
{
s
,
err
:=
h2
.
NewStream
(
ctx2
,
h1
.
ID
(),
"/testing"
)
if
s
!=
nil
{
errCh
<-
fmt
.
Errorf
(
"expected to fail negotiation"
)
return
}
errCh
<-
err
}()
select
{
case
err
:=
<-
errCh
:
t
.
Fatal
(
err
)
case
<-
time
.
After
(
10
*
time
.
Millisecond
)
:
// ok, hung.
}
cancel2
()
select
{
case
err
:=
<-
errCh
:
require
.
Equal
(
t
,
err
,
context
.
Canceled
)
case
<-
time
.
After
(
500
*
time
.
Millisecond
)
:
// failed to cancel
t
.
Fatal
(
"expected negotiation to be canceled"
)
}
}
func
waitForAddrChangeEvent
(
ctx
context
.
Context
,
sub
event
.
Subscription
,
t
*
testing
.
T
)
event
.
EvtLocalAddressesUpdated
{
for
{
select
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment