From 1fb99552b75e9b3fc1e391bc66049d1091128c5b Mon Sep 17 00:00:00 2001
From: Craig Tiller <ctiller@google.com>
Date: Mon, 27 Apr 2015 08:55:08 -0700
Subject: [PATCH] Fix early shutdown: await client context deletion before
 channel deletion

---
 include/grpc++/client_context.h | 5 ++++-
 src/cpp/client/channel.cc       | 2 +-
 src/cpp/client/channel.h        | 1 +
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h
index 19630c9b54..a58e9872e6 100644
--- a/include/grpc++/client_context.h
+++ b/include/grpc++/client_context.h
@@ -35,6 +35,7 @@
 #define GRPCXX_CLIENT_CONTEXT_H
 
 #include <map>
+#include <memory>
 #include <string>
 
 #include <grpc/support/log.h>
@@ -126,9 +127,10 @@ class ClientContext {
   friend class ::grpc::ClientAsyncResponseReader;
 
   grpc_call* call() { return call_; }
-  void set_call(grpc_call* call) {
+  void set_call(grpc_call* call, const std::shared_ptr<ChannelInterface>& channel) {
     GPR_ASSERT(call_ == nullptr);
     call_ = call;
+    channel_ = channel;
   }
 
   grpc_completion_queue* cq() { return cq_; }
@@ -137,6 +139,7 @@ class ClientContext {
   grpc::string authority() { return authority_; }
 
   bool initial_metadata_received_;
+  std::shared_ptr<ChannelInterface> channel_;
   grpc_call* call_;
   grpc_completion_queue* cq_;
   gpr_timespec deadline_;
diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc
index ba8882278f..c541ddfb48 100644
--- a/src/cpp/client/channel.cc
+++ b/src/cpp/client/channel.cc
@@ -71,7 +71,7 @@ Call Channel::CreateCall(const RpcMethod& method, ClientContext* context,
                                          : context->authority().c_str(),
                                      context->raw_deadline());
   GRPC_TIMER_MARK(CALL_CREATED, c_call);
-  context->set_call(c_call);
+  context->set_call(c_call, shared_from_this());
   return Call(c_call, this, cq);
 }
 
diff --git a/src/cpp/client/channel.h b/src/cpp/client/channel.h
index cd239247c8..46009d20ba 100644
--- a/src/cpp/client/channel.h
+++ b/src/cpp/client/channel.h
@@ -51,6 +51,7 @@ class Credentials;
 class StreamContextInterface;
 
 class Channel GRPC_FINAL : public GrpcLibrary,
+                           public std::enable_shared_from_this<Channel>,
                            public ChannelInterface {
  public:
   Channel(const grpc::string& target, grpc_channel* c_channel);
-- 
GitLab