From 2a0d6234cb2ccebb265c035ffd09ecc9a347b4bf Mon Sep 17 00:00:00 2001
From: Yash Tibrewal <yashkt@google.com>
Date: Wed, 9 Mar 2022 17:57:05 -0800
Subject: [PATCH] HTTP2: No need to run cancelling logic on servers when
 receiving GOAWAY (#29067)

---
 .../chttp2/transport/chttp2_transport.cc      | 34 ++++++++++---------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/src/core/ext/transport/chttp2/transport/chttp2_transport.cc b/src/core/ext/transport/chttp2/transport/chttp2_transport.cc
index f8a417c1241..1d6facdd176 100644
--- a/src/core/ext/transport/chttp2/transport/chttp2_transport.cc
+++ b/src/core/ext/transport/chttp2/transport/chttp2_transport.cc
@@ -1100,22 +1100,24 @@ void grpc_chttp2_add_incoming_goaway(grpc_chttp2_transport* t,
     gpr_log(GPR_INFO, "%s: Got goaway [%d] err=%s", t->peer_string.c_str(),
             goaway_error, grpc_error_std_string(t->goaway_error).c_str());
   }
-  cancel_unstarted_streams(t, GRPC_ERROR_REF(t->goaway_error));
-  // Cancel all unseen streams
-  grpc_chttp2_stream_map_for_each(
-      &t->stream_map,
-      [](void* user_data, uint32_t /* key */, void* stream) {
-        uint32_t last_stream_id = *(static_cast<uint32_t*>(user_data));
-        grpc_chttp2_stream* s = static_cast<grpc_chttp2_stream*>(stream);
-        if (s->id > last_stream_id) {
-          s->trailing_metadata_buffer.Set(
-              grpc_core::GrpcStreamNetworkState(),
-              grpc_core::GrpcStreamNetworkState::kNotSeenByServer);
-          grpc_chttp2_cancel_stream(s->t, s,
-                                    GRPC_ERROR_REF(s->t->goaway_error));
-        }
-      },
-      &last_stream_id);
+  if (t->is_client) {
+    cancel_unstarted_streams(t, GRPC_ERROR_REF(t->goaway_error));
+    // Cancel all unseen streams
+    grpc_chttp2_stream_map_for_each(
+        &t->stream_map,
+        [](void* user_data, uint32_t /* key */, void* stream) {
+          uint32_t last_stream_id = *(static_cast<uint32_t*>(user_data));
+          grpc_chttp2_stream* s = static_cast<grpc_chttp2_stream*>(stream);
+          if (s->id > last_stream_id) {
+            s->trailing_metadata_buffer.Set(
+                grpc_core::GrpcStreamNetworkState(),
+                grpc_core::GrpcStreamNetworkState::kNotSeenByServer);
+            grpc_chttp2_cancel_stream(s->t, s,
+                                      GRPC_ERROR_REF(s->t->goaway_error));
+          }
+        },
+        &last_stream_id);
+  }
   absl::Status status = grpc_error_to_absl_status(t->goaway_error);
   // When a client receives a GOAWAY with error code ENHANCE_YOUR_CALM and debug
   // data equal to "too_many_pings", it should log the occurrence at a log level