Remove processed packets from undecryptable_packets_.

Protected by FLAGS_quic_reloadable_flag_quic_fix_undecryptable_packets2.

PiperOrigin-RevId: 331861686
Change-Id: I4f09e6961393980814d84c743f45356d3c648edb
diff --git a/quic/core/quic_connection.cc b/quic/core/quic_connection.cc
index 6b694a0..fc72c59 100644
--- a/quic/core/quic_connection.cc
+++ b/quic/core/quic_connection.cc
@@ -3416,6 +3416,11 @@
       encryption_level_ == ENCRYPTION_INITIAL) {
     return;
   }
+  const bool fix_undecryptable_packets =
+      GetQuicReloadableFlag(quic_fix_undecryptable_packets2);
+  if (fix_undecryptable_packets) {
+    QUIC_RELOADABLE_FLAG_COUNT(quic_fix_undecryptable_packets2);
+  }
 
   auto iter = undecryptable_packets_.begin();
   while (connected_ && iter != undecryptable_packets_.end()) {
@@ -3426,9 +3431,11 @@
       return;
     }
     UndecryptablePacket* undecryptable_packet = &*iter;
-    ++iter;
-    if (undecryptable_packet->processed) {
-      continue;
+    if (!fix_undecryptable_packets) {
+      ++iter;
+      if (undecryptable_packet->processed) {
+        continue;
+      }
     }
     QUIC_DVLOG(1) << ENDPOINT << "Attempting to process undecryptable packet";
     if (debug_visitor_ != nullptr) {
@@ -3437,7 +3444,11 @@
     }
     if (framer_.ProcessPacket(*undecryptable_packet->packet)) {
       QUIC_DVLOG(1) << ENDPOINT << "Processed undecryptable packet!";
-      undecryptable_packet->processed = true;
+      if (fix_undecryptable_packets) {
+        iter = undecryptable_packets_.erase(iter);
+      } else {
+        undecryptable_packet->processed = true;
+      }
       ++stats_.packets_processed;
       continue;
     }
@@ -3450,14 +3461,21 @@
       QUIC_DVLOG(1)
           << ENDPOINT
           << "Need to attempt to process this undecryptable packet later";
+      if (fix_undecryptable_packets) {
+        ++iter;
+      }
       continue;
     }
-    undecryptable_packet->processed = true;
+    if (fix_undecryptable_packets) {
+      iter = undecryptable_packets_.erase(iter);
+    } else {
+      undecryptable_packet->processed = true;
+    }
   }
   // Remove processed packets. We cannot remove elements in the while loop
   // above because currently QuicCircularDeque does not support removing
   // mid elements.
-  while (!undecryptable_packets_.empty()) {
+  while (!fix_undecryptable_packets && !undecryptable_packets_.empty()) {
     if (!undecryptable_packets_.front().processed) {
       break;
     }
diff --git a/quic/core/quic_connection.h b/quic/core/quic_connection.h
index f748e9c..dcd655b 100644
--- a/quic/core/quic_connection.h
+++ b/quic/core/quic_connection.h
@@ -1149,6 +1149,8 @@
     // This gets set to true if 1) connection sucessfully processed the packet
     // or 2) connection failed to process the packet and will not try to process
     // it later.
+    // TODO(fayang): Remove this when deprecating
+    // quic_fix_undecryptable_packets2.
     bool processed;
   };
 
diff --git a/quic/core/quic_connection_test.cc b/quic/core/quic_connection_test.cc
index 7b9ece2..fdc4d7a 100644
--- a/quic/core/quic_connection_test.cc
+++ b/quic/core/quic_connection_test.cc
@@ -10934,7 +10934,11 @@
   // Verify all ENCRYPTION_HANDSHAKE packets get processed.
   EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(6);
   connection_.GetProcessUndecryptablePacketsAlarm()->Fire();
-  EXPECT_EQ(4u, QuicConnectionPeer::NumUndecryptablePackets(&connection_));
+  if (GetQuicReloadableFlag(quic_fix_undecryptable_packets2)) {
+    EXPECT_EQ(1u, QuicConnectionPeer::NumUndecryptablePackets(&connection_));
+  } else {
+    EXPECT_EQ(4u, QuicConnectionPeer::NumUndecryptablePackets(&connection_));
+  }
 
   SetDecrypter(ENCRYPTION_FORWARD_SECURE,
                std::make_unique<StrictTaggingDecrypter>(0x02));