Skip to content

Commit

Permalink
transport: do not create a Stream on a canceled context
Browse files Browse the repository at this point in the history
Occasionally Invoke() would let a message slip through when the context
is already canceled.
  • Loading branch information
Anthony Romano committed Apr 13, 2016
1 parent 326d663 commit 025674f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (
expectedResponse = "pong"
weirdError = "format verbs: %v%s"
sizeLargeErr = 1024 * 1024
canceled = 0
)

type testCodec struct {
Expand Down Expand Up @@ -100,6 +101,11 @@ func (h *testStreamHandler) handleStream(t *testing.T, s *transport.Stream) {
h.t.WriteStatus(s, codes.Internal, weirdError)
return
}
if v == "canceled" {
canceled++
h.t.WriteStatus(s, codes.Internal, "")
return
}
if v != expectedRequest {
h.t.WriteStatus(s, codes.Internal, strings.Repeat("A", sizeLargeErr))
return
Expand Down Expand Up @@ -244,3 +250,20 @@ func TestInvokeErrorSpecialChars(t *testing.T) {
cc.Close()
server.stop()
}

// TestInvokeCancel checks that an Invoke with a canceled context is not sent.
func TestInvokeCancel(t *testing.T) {
server, cc := setUp(t, 0, math.MaxUint32)
var reply string
req := "canceled"
for i := 0; i < 100; i++ {
ctx, cancel := context.WithCancel(context.Background())
cancel()
Invoke(ctx, "/foo/bar", &req, &reply, cc)
}
if canceled != 0 {
t.Fatalf("received %d of 100 canceled requests", canceled)
}
cc.Close()
server.stop()
}
6 changes: 3 additions & 3 deletions transport/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ func (t *http2Client) NewStream(ctx context.Context, callHdr *CallHdr) (_ *Strea
var timeout time.Duration
if dl, ok := ctx.Deadline(); ok {
timeout = dl.Sub(time.Now())
if timeout <= 0 {
return nil, ContextErr(context.DeadlineExceeded)
}
}
if err := ctx.Err(); err != nil {
return nil, ContextErr(err)
}
pr := &peer.Peer{
Addr: t.conn.RemoteAddr(),
Expand Down

0 comments on commit 025674f

Please sign in to comment.