Skip to content
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

grpc: server should send RST_STREAM when deadline is exceeded #7892

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions internal/transport/handler_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ func handleStreamCloseBodyTest(t *testing.T, statusCode codes.Code, msg string)
st.ht.HandleStreams(
context.Background(), func(s *ServerStream) { go handleStream(s) },
)

wantHeader := http.Header{
"Date": nil,
"Content-Type": {"application/grpc"},
Expand Down Expand Up @@ -379,6 +380,15 @@ func (s) TestHandlerTransport_HandleStreams_Timeout(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// rst flag setting to verify the noop function: signalDeadlineExceeded
ch := make(chan struct{}, 1)
origSignalDeadlineExceeded := signalDeadlineExceeded
signalDeadlineExceeded = func() {
ch <- struct{}{}
}
defer func() {
signalDeadlineExceeded = origSignalDeadlineExceeded
}()
runStream := func(s *ServerStream) {
defer bodyw.Close()
select {
Expand All @@ -392,7 +402,9 @@ func (s) TestHandlerTransport_HandleStreams_Timeout(t *testing.T) {
t.Errorf("ctx.Err = %v; want %v", err, context.DeadlineExceeded)
return
}

s.WriteStatus(status.New(codes.DeadlineExceeded, "too slow"))

}
ht.HandleStreams(
context.Background(), func(s *ServerStream) { go runStream(s) },
Expand All @@ -407,6 +419,13 @@ func (s) TestHandlerTransport_HandleStreams_Timeout(t *testing.T) {
"Grpc-Message": {encodeGrpcMessage("too slow")},
}
checkHeaderAndTrailer(t, rw, wantHeader, wantTrailer)
select {
case <-ch: // Signal received, continue with the test
case <-time.After(5 * time.Second):
t.Errorf("timeout waiting for signalDeadlineExceeded")
return
}

}

// TestHandlerTransport_HandleStreams_MultiWriteStatus ensures that
Expand Down
5 changes: 5 additions & 0 deletions internal/transport/http2_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,9 @@ func (t *http2Server) writeStatus(s *ServerStream, st *status.Status) error {
}
// Send a RST_STREAM after the trailers if the client has not already half-closed.
rst := s.getState() == streamActive
if rst {
signalDeadlineExceeded()
}
t.finishStream(s, rst, http2.ErrCodeNo, trailingHeader, true)
for _, sh := range t.stats {
// Note: The trailer fields are compressed with hpack after this call returns.
Expand All @@ -1111,6 +1114,8 @@ func (t *http2Server) writeStatus(s *ServerStream, st *status.Status) error {
return nil
}

var signalDeadlineExceeded = func() {}

// Write converts the data into HTTP2 data frame and sends it out. Non-nil error
// is returns if it fails (e.g., framing error, transport error).
func (t *http2Server) write(s *ServerStream, hdr []byte, data mem.BufferSlice, _ *WriteOptions) error {
Expand Down
Loading