Clear queued undecryptable packets on handshake complete rather than check encryption_level.

Protected by FLAGS_quic_reloadable_flag_quic_clear_undecryptable_packets_on_handshake_complete.

PiperOrigin-RevId: 395252247
diff --git a/quic/core/quic_connection.cc b/quic/core/quic_connection.cc
index 170a206..9e0c95e 100644
--- a/quic/core/quic_connection.cc
+++ b/quic/core/quic_connection.cc
@@ -4467,10 +4467,17 @@
     iter = undecryptable_packets_.erase(iter);
   }
 
-  // Once forward secure encryption is in use, there will be no
-  // new keys installed and hence any undecryptable packets will
-  // never be able to be decrypted.
-  if (encryption_level_ == ENCRYPTION_FORWARD_SECURE) {
+  // Once handshake is complete, there will be no new keys installed and hence
+  // any undecryptable packets will never be able to be decrypted.
+  bool clear_undecryptable_packets =
+      encryption_level_ == ENCRYPTION_FORWARD_SECURE;
+  if (GetQuicReloadableFlag(
+          quic_clear_undecryptable_packets_on_handshake_complete)) {
+    QUIC_RELOADABLE_FLAG_COUNT(
+        quic_clear_undecryptable_packets_on_handshake_complete);
+    clear_undecryptable_packets = IsHandshakeComplete();
+  }
+  if (clear_undecryptable_packets) {
     if (debug_visitor_ != nullptr) {
       for (const auto& undecryptable_packet : undecryptable_packets_) {
         debug_visitor_->OnUndecryptablePacket(
diff --git a/quic/core/quic_flags_list.h b/quic/core/quic_flags_list.h
index 7e85017..423401a 100644
--- a/quic/core/quic_flags_list.h
+++ b/quic/core/quic_flags_list.h
@@ -39,6 +39,8 @@
 QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_allow_client_enabled_bbr_v2, false)
 // If true, avoid calling reloadable flags in QuicVersionManager constructor by lazily initializing internal state.
 QUIC_FLAG(FLAGS_quic_restart_flag_quic_lazy_quic_version_manager, false)
+// If true, clear undecryptable packets on handshake complete.
+QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_clear_undecryptable_packets_on_handshake_complete, true)
 // If true, close read side but not write side in QuicSpdyStream::OnStreamReset().
 QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_fix_on_stream_reset, true)
 // If true, default on PTO which unifies TLP + RTO loss recovery.