Skip to content

transport: Prevent sending negative timeouts #8312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/transport/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,9 @@ func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr)
// Send out timeout regardless its value. The server can detect timeout context by itself.
// TODO(mmukhi): Perhaps this field should be updated when actually writing out to the wire.
timeout := time.Until(dl)
if timeout <= 0 {
return nil, status.Error(codes.DeadlineExceeded, context.DeadlineExceeded.Error())
}
headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-timeout", Value: grpcutil.EncodeDuration(timeout)})
}
for k, v := range authData {
Expand Down
25 changes: 25 additions & 0 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5351,6 +5351,31 @@ func testRPCTimeout(t *testing.T, e env) {
}
}

// Tests that the client doesn't send a negative timeout to the server. If the
// server receives a negative timeout, it would return an internal status. The
// client checks the context error before starting a stream, however the context
// may expire after this check and before the timeout is calculated.
func (s) TestNegativeRPCTimeout(t *testing.T) {
server := stubserver.StartTestService(t, nil)
defer server.Stop()

if err := server.StartClient(); err != nil {
t.Fatalf("Failed to create client: %v", err)
}

// Try increasingly larger timeout values to trigger the condition when the
// context has expired while creating the grpc-timeout header.
for i := range 10 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(i*100)*time.Nanosecond)
defer cancel()

client := server.Client
if _, err := client.EmptyCall(ctx, &testpb.Empty{}); status.Code(err) != codes.DeadlineExceeded {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v; want <nil>, error code: %s", err, codes.DeadlineExceeded)
}
}
}

func (s) TestDisabledIOBuffers(t *testing.T) {
payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(60000))
if err != nil {
Expand Down
Loading