From 447569490d05f95b8caa79a1e9f35f2ac1f7a2bd Mon Sep 17 00:00:00 2001
From: "Mark D. Roth" <roth@google.com>
Date: Tue, 29 Nov 2016 12:43:55 -0800
Subject: [PATCH] Eliminate the user_data overloading hack in
 handshake_manager.

---
 src/core/lib/channel/handshaker.c | 25 ++++++++-----------------
 1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/src/core/lib/channel/handshaker.c b/src/core/lib/channel/handshaker.c
index 905db118be..3c125a22f3 100644
--- a/src/core/lib/channel/handshaker.c
+++ b/src/core/lib/channel/handshaker.c
@@ -154,7 +154,6 @@ static void call_next_handshaker(grpc_exec_ctx* exec_ctx, void* arg,
 // on_handshake_done callback.
 static void call_next_handshaker_locked(grpc_exec_ctx* exec_ctx,
                                         grpc_handshake_manager* mgr,
-                                        grpc_handshaker_args* args,
                                         grpc_error* error) {
   GPR_ASSERT(mgr->index <= mgr->count);
   // If we got an error, skip all remaining handshakers and invoke the
@@ -165,9 +164,7 @@ static void call_next_handshaker_locked(grpc_exec_ctx* exec_ctx,
     // Cancel deadline timer, since we're invoking the on_handshake_done
     // callback now.
     grpc_timer_cancel(exec_ctx, &mgr->deadline_timer);
-    args->user_data = mgr->user_data;
-    grpc_exec_ctx_sched(exec_ctx, &mgr->on_handshake_done,
-                        GRPC_ERROR_REF(error), NULL);
+    grpc_exec_ctx_sched(exec_ctx, &mgr->on_handshake_done, error, NULL);
     // Since we're invoking the final callback, we won't be coming back
     // to this function, so we can release our reference to the
     // handshake manager.
@@ -176,7 +173,8 @@ static void call_next_handshaker_locked(grpc_exec_ctx* exec_ctx,
   }
   // Call the next handshaker.
   grpc_handshaker_do_handshake(exec_ctx, mgr->handshakers[mgr->index],
-                               mgr->acceptor, &mgr->call_next_handshaker, args);
+                               mgr->acceptor, &mgr->call_next_handshaker,
+                               &mgr->args);
   ++mgr->index;
 }
 
@@ -184,10 +182,9 @@ static void call_next_handshaker_locked(grpc_exec_ctx* exec_ctx,
 // handshakers together.
 static void call_next_handshaker(grpc_exec_ctx* exec_ctx, void* arg,
                                  grpc_error* error) {
-  grpc_handshaker_args* args = arg;
-  grpc_handshake_manager* mgr = args->user_data;
+  grpc_handshake_manager* mgr = arg;
   gpr_mu_lock(&mgr->mu);
-  call_next_handshaker_locked(exec_ctx, mgr, args, error);
+  call_next_handshaker_locked(exec_ctx, mgr, GRPC_ERROR_REF(error));
   gpr_mu_unlock(&mgr->mu);
 }
 
@@ -209,21 +206,15 @@ void grpc_handshake_manager_do_handshake(
   // handshakers and eventually be freed by the on_handshake_done callback.
   mgr->args.endpoint = endpoint;
   mgr->args.args = grpc_channel_args_copy(channel_args);
+  mgr->args.user_data = user_data;
   mgr->args.read_buffer = gpr_malloc(sizeof(*mgr->args.read_buffer));
   grpc_slice_buffer_init(mgr->args.read_buffer);
   // Initialize state needed for calling handshakers.
   gpr_mu_lock(&mgr->mu);
   GPR_ASSERT(mgr->index == 0);
   mgr->acceptor = acceptor;
-  grpc_closure_init(&mgr->call_next_handshaker, call_next_handshaker,
-                    &mgr->args);
+  grpc_closure_init(&mgr->call_next_handshaker, call_next_handshaker, mgr);
   grpc_closure_init(&mgr->on_handshake_done, on_handshake_done, &mgr->args);
-  // While chaining between handshakers, we use args->user_data to
-  // store a pointer to the handshake manager.  This will be
-  // changed to point to the caller-supplied user_data before calling
-  // the on_handshake_done callback.
-  mgr->args.user_data = mgr;
-  mgr->user_data = user_data;
   // Start deadline timer, which owns a ref.
   gpr_ref(&mgr->refs);
   grpc_timer_init(exec_ctx, &mgr->deadline_timer,
@@ -231,6 +222,6 @@ void grpc_handshake_manager_do_handshake(
                   on_timeout, mgr, gpr_now(GPR_CLOCK_MONOTONIC));
   // Start first handshaker, which also owns a ref.
   gpr_ref(&mgr->refs);
-  call_next_handshaker_locked(exec_ctx, mgr, &mgr->args, GRPC_ERROR_NONE);
+  call_next_handshaker_locked(exec_ctx, mgr, GRPC_ERROR_NONE);
   gpr_mu_unlock(&mgr->mu);
 }
-- 
GitLab