Internal change

PiperOrigin-RevId: 448332185
diff --git a/quiche/quic/core/crypto/quic_crypto_server_config.cc b/quiche/quic/core/crypto/quic_crypto_server_config.cc
index 11ccc2f..7a651bf 100644
--- a/quiche/quic/core/crypto/quic_crypto_server_config.cc
+++ b/quiche/quic/core/crypto/quic_crypto_server_config.cc
@@ -706,7 +706,7 @@
         params,
     quiche::QuicheReferenceCountedPointer<QuicSignedServerConfig> signed_config,
     QuicByteCount total_framing_overhead, QuicByteCount chlo_packet_size,
-    std::unique_ptr<ProcessClientHelloResultCallback> done_cb) const {
+    std::shared_ptr<ProcessClientHelloResultCallback> done_cb) const {
   QUICHE_DCHECK(done_cb);
   auto context = std::make_unique<ProcessClientHelloContext>(
       validate_chlo_result, reject_only, connection_id, server_address,
diff --git a/quiche/quic/core/crypto/quic_crypto_server_config.h b/quiche/quic/core/crypto/quic_crypto_server_config.h
index c3febdc..f114546 100644
--- a/quiche/quic/core/crypto/quic_crypto_server_config.h
+++ b/quiche/quic/core/crypto/quic_crypto_server_config.h
@@ -335,7 +335,7 @@
       quiche::QuicheReferenceCountedPointer<QuicSignedServerConfig>
           signed_config,
       QuicByteCount total_framing_overhead, QuicByteCount chlo_packet_size,
-      std::unique_ptr<ProcessClientHelloResultCallback> done_cb) const;
+      std::shared_ptr<ProcessClientHelloResultCallback> done_cb) const;
 
   // BuildServerConfigUpdateMessage invokes |cb| with a SCUP message containing
   // the current primary config, an up to date source-address token, and cert
@@ -587,7 +587,7 @@
         quiche::QuicheReferenceCountedPointer<QuicSignedServerConfig>
             signed_config,
         QuicByteCount total_framing_overhead, QuicByteCount chlo_packet_size,
-        std::unique_ptr<ProcessClientHelloResultCallback> done_cb)
+        std::shared_ptr<ProcessClientHelloResultCallback> done_cb)
         : validate_chlo_result_(validate_chlo_result),
           reject_only_(reject_only),
           connection_id_(connection_id),
@@ -674,7 +674,7 @@
         signed_config_;
     const QuicByteCount total_framing_overhead_;
     const QuicByteCount chlo_packet_size_;
-    std::unique_ptr<ProcessClientHelloResultCallback> done_cb_;
+    std::shared_ptr<ProcessClientHelloResultCallback> done_cb_;
   };
 
   // Callback class for bridging between ProcessClientHello and
diff --git a/quiche/quic/core/quic_crypto_server_stream.cc b/quiche/quic/core/quic_crypto_server_stream.cc
index 5d41e47..2bd8fbd 100644
--- a/quiche/quic/core/quic_crypto_server_stream.cc
+++ b/quiche/quic/core/quic_crypto_server_stream.cc
@@ -69,7 +69,6 @@
       zero_rtt_attempted_(false),
       chlo_packet_size_(0),
       validate_client_hello_cb_(nullptr),
-      process_client_hello_cb_(nullptr),
       encryption_established_(false),
       one_rtt_keys_available_(false),
       one_rtt_packet_decrypted_(false),
@@ -89,9 +88,10 @@
     send_server_config_update_cb_->Cancel();
     send_server_config_update_cb_ = nullptr;
   }
-  if (process_client_hello_cb_ != nullptr) {
-    process_client_hello_cb_->Cancel();
-    process_client_hello_cb_ = nullptr;
+  if (std::shared_ptr<ProcessClientHelloCallback> cb =
+          process_client_hello_cb_.lock()) {
+    cb->Cancel();
+    process_client_hello_cb_.reset();
   }
 }
 
@@ -115,7 +115,7 @@
   }
 
   if (validate_client_hello_cb_ != nullptr ||
-      process_client_hello_cb_ != nullptr) {
+      !process_client_hello_cb_.expired()) {
     // Already processing some other handshake message.  The protocol
     // does not allow for clients to send multiple handshake messages
     // before the server has a chance to respond.
@@ -129,7 +129,7 @@
 
   std::unique_ptr<ValidateCallback> cb(new ValidateCallback(this));
   QUICHE_DCHECK(validate_client_hello_cb_ == nullptr);
-  QUICHE_DCHECK(process_client_hello_cb_ == nullptr);
+  QUICHE_DCHECK(process_client_hello_cb_.expired());
   validate_client_hello_cb_ = cb.get();
   crypto_config_->ValidateClientHello(
       message, GetClientAddress(), session()->connection()->self_address(),
@@ -144,12 +144,11 @@
     std::unique_ptr<ProofSource::Details> details) {
   // Clear the callback that got us here.
   QUICHE_DCHECK(validate_client_hello_cb_ != nullptr);
-  QUICHE_DCHECK(process_client_hello_cb_ == nullptr);
+  QUICHE_DCHECK(process_client_hello_cb_.expired());
   validate_client_hello_cb_ = nullptr;
 
-  std::unique_ptr<ProcessClientHelloCallback> cb(
-      new ProcessClientHelloCallback(this, result));
-  process_client_hello_cb_ = cb.get();
+  auto cb = std::make_shared<ProcessClientHelloCallback>(this, result);
+  process_client_hello_cb_ = cb;
   ProcessClientHello(result, std::move(details), std::move(cb));
 }
 
@@ -161,9 +160,9 @@
         std::unique_ptr<DiversificationNonce> diversification_nonce,
         std::unique_ptr<ProofSource::Details> proof_source_details) {
   // Clear the callback that got us here.
-  QUICHE_DCHECK(process_client_hello_cb_ != nullptr);
+  QUICHE_DCHECK(!process_client_hello_cb_.expired());
   QUICHE_DCHECK(validate_client_hello_cb_ == nullptr);
-  process_client_hello_cb_ = nullptr;
+  process_client_hello_cb_.reset();
   proof_source_details_ = std::move(proof_source_details);
 
   AdjustTestValue("quic::QuicCryptoServerStream::after_process_client_hello",
@@ -457,7 +456,7 @@
         ValidateClientHelloResultCallback::Result>
         result,
     std::unique_ptr<ProofSource::Details> proof_source_details,
-    std::unique_ptr<ProcessClientHelloResultCallback> done_cb) {
+    std::shared_ptr<ProcessClientHelloResultCallback> done_cb) {
   proof_source_details_ = std::move(proof_source_details);
   const CryptoHandshakeMessage& message = result->client_hello;
   std::string error_details;
diff --git a/quiche/quic/core/quic_crypto_server_stream.h b/quiche/quic/core/quic_crypto_server_stream.h
index f91ceba..665d071 100644
--- a/quiche/quic/core/quic_crypto_server_stream.h
+++ b/quiche/quic/core/quic_crypto_server_stream.h
@@ -101,7 +101,7 @@
           ValidateClientHelloResultCallback::Result>
           result,
       std::unique_ptr<ProofSource::Details> proof_source_details,
-      std::unique_ptr<ProcessClientHelloResultCallback> done_cb);
+      std::shared_ptr<ProcessClientHelloResultCallback> done_cb);
 
   // Hook that allows the server to set QuicConfig defaults just
   // before going through the parameter negotiation step.
@@ -254,7 +254,7 @@
   // ProcessClientHello and forward it to
   // FinishProcessingHandshakeMessageAfterProcessClientHello.  Note that this
   // field is mutually exclusive with validate_client_hello_cb_.
-  ProcessClientHelloCallback* process_client_hello_cb_;
+  std::weak_ptr<ProcessClientHelloCallback> process_client_hello_cb_;
 
   // The ProofSource::Details from this connection.
   std::unique_ptr<ProofSource::Details> proof_source_details_;