Handle closing pending streams.

Make QuicSession handle if a pending stream is closed before it gets converted
into the appropriate incoming stream subclass.

gfe-relnote: n/a, no functional change outside QUIC v99-only code.  Protected by existing disabled gfe2_reloadable_flag_quic_enable_version_99.
PiperOrigin-RevId: 262183753
Change-Id: I4147320860b867df0d80ba8fd55106bf779b15b3
diff --git a/quic/core/quic_session.cc b/quic/core/quic_session.cc
index a5a35f7..875ec22 100644
--- a/quic/core/quic_session.cc
+++ b/quic/core/quic_session.cc
@@ -157,6 +157,10 @@
   if (!connection()->connected()) {
     return;
   }
+  if (pending->sequencer()->IsClosed()) {
+    ClosePendingStream(stream_id);
+    return;
+  }
   if (ProcessPendingStream(pending)) {
     // The pending stream should now be in the scope of normal streams.
     DCHECK(IsClosedStream(stream_id) || IsOpenStream(stream_id))
@@ -318,6 +322,7 @@
   }
 
   pending->OnRstStreamFrame(frame);
+  SendRstStream(stream_id, QUIC_RST_ACKNOWLEDGEMENT, 0);
   ClosePendingStream(stream_id);
 }
 
@@ -884,19 +889,7 @@
 void QuicSession::ClosePendingStream(QuicStreamId stream_id) {
   QUIC_DVLOG(1) << ENDPOINT << "Closing stream " << stream_id;
 
-  if (pending_stream_map_.find(stream_id) == pending_stream_map_.end()) {
-    QUIC_BUG << ENDPOINT << "Stream is already closed: " << stream_id;
-    return;
-  }
-
-  SendRstStream(stream_id, QUIC_RST_ACKNOWLEDGEMENT, 0);
-
-  // The pending stream may have been deleted and removed during SendRstStream.
-  // Remove the stream from pending stream map iff it is still in the map.
-  if (pending_stream_map_.find(stream_id) != pending_stream_map_.end()) {
-    pending_stream_map_.erase(stream_id);
-  }
-
+  pending_stream_map_.erase(stream_id);
   if (VersionHasIetfQuicFrames(connection_->transport_version())) {
     v99_streamid_manager_.OnStreamClosed(stream_id);
   }