From 32c804e6a76d37460f34dc7154431b2c38d86c99 Mon Sep 17 00:00:00 2001
From: Craig Tiller <ctiller@google.com>
Date: Tue, 27 Sep 2016 13:40:03 -0700
Subject: [PATCH] Fix a conditional that got flipped

---
 src/core/lib/iomgr/buffer_pool.c   | 16 ++++++++++------
 src/core/lib/iomgr/buffer_pool.h   |  2 ++
 test/core/iomgr/buffer_pool_test.c | 19 ++++++++++++++++++-
 3 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/src/core/lib/iomgr/buffer_pool.c b/src/core/lib/iomgr/buffer_pool.c
index c0fd7cc45f..88f37c9fc1 100644
--- a/src/core/lib/iomgr/buffer_pool.c
+++ b/src/core/lib/iomgr/buffer_pool.c
@@ -337,11 +337,6 @@ static void bu_destroy(grpc_exec_ctx *exec_ctx, void *bu, grpc_error *error) {
     buffer_user->buffer_pool->free_pool += buffer_user->free_pool;
     bpstep_sched(exec_ctx, buffer_user->buffer_pool);
   }
-#ifndef NDEBUG
-  gpr_free(buffer_user->asan_canary);
-#endif
-  grpc_buffer_pool_internal_unref(exec_ctx, buffer_user->buffer_pool);
-  gpr_mu_destroy(&buffer_user->mu);
 }
 
 static void bu_allocated_slices(grpc_exec_ctx *exec_ctx, void *ts,
@@ -521,6 +516,15 @@ void grpc_buffer_user_shutdown(grpc_exec_ctx *exec_ctx,
   gpr_mu_unlock(&buffer_user->mu);
 }
 
+void grpc_buffer_user_destroy(grpc_exec_ctx *exec_ctx,
+                              grpc_buffer_user *buffer_user) {
+#ifndef NDEBUG
+  gpr_free(buffer_user->asan_canary);
+#endif
+  grpc_buffer_pool_internal_unref(exec_ctx, buffer_user->buffer_pool);
+  gpr_mu_destroy(&buffer_user->mu);
+}
+
 void grpc_buffer_user_alloc(grpc_exec_ctx *exec_ctx,
                             grpc_buffer_user *buffer_user, size_t size,
                             grpc_closure *optional_on_done) {
@@ -580,7 +584,7 @@ void grpc_buffer_user_free(grpc_exec_ctx *exec_ctx,
 void grpc_buffer_user_post_reclaimer(grpc_exec_ctx *exec_ctx,
                                      grpc_buffer_user *buffer_user,
                                      bool destructive, grpc_closure *closure) {
-  if (gpr_atm_acq_load(&buffer_user->on_done_destroy_closure) != 0) {
+  if (gpr_atm_acq_load(&buffer_user->on_done_destroy_closure) == 0) {
     GPR_ASSERT(buffer_user->reclaimers[destructive] == NULL);
     buffer_user->reclaimers[destructive] = closure;
     grpc_combiner_execute(exec_ctx, buffer_user->buffer_pool->combiner,
diff --git a/src/core/lib/iomgr/buffer_pool.h b/src/core/lib/iomgr/buffer_pool.h
index ca4c39f76d..301e059c18 100644
--- a/src/core/lib/iomgr/buffer_pool.h
+++ b/src/core/lib/iomgr/buffer_pool.h
@@ -90,6 +90,8 @@ void grpc_buffer_user_init(grpc_buffer_user *buffer_user,
 void grpc_buffer_user_shutdown(grpc_exec_ctx *exec_ctx,
                                grpc_buffer_user *buffer_user,
                                grpc_closure *on_done);
+void grpc_buffer_user_destroy(grpc_exec_ctx *exec_ctx,
+                              grpc_buffer_user *buffer_user);
 
 void grpc_buffer_user_alloc(grpc_exec_ctx *exec_ctx,
                             grpc_buffer_user *buffer_user, size_t size,
diff --git a/test/core/iomgr/buffer_pool_test.c b/test/core/iomgr/buffer_pool_test.c
index a15e02e3c6..5bc619b478 100644
--- a/test/core/iomgr/buffer_pool_test.c
+++ b/test/core/iomgr/buffer_pool_test.c
@@ -83,8 +83,10 @@ static void destroy_user(grpc_buffer_user *usr) {
   grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
   bool done = false;
   grpc_buffer_user_shutdown(&exec_ctx, usr, set_bool(&done));
-  grpc_exec_ctx_finish(&exec_ctx);
+  grpc_exec_ctx_flush(&exec_ctx);
   GPR_ASSERT(done);
+  grpc_buffer_user_destroy(&exec_ctx, usr);
+  grpc_exec_ctx_finish(&exec_ctx);
 }
 
 static void test_no_op(void) {
@@ -528,6 +530,11 @@ static void test_buffer_user_stays_allocated_until_memory_released(void) {
     grpc_exec_ctx_finish(&exec_ctx);
     GPR_ASSERT(done);
   }
+  {
+    grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+    grpc_buffer_user_destroy(&exec_ctx, &usr);
+    grpc_exec_ctx_finish(&exec_ctx);
+  }
 }
 
 static void test_pools_merged_on_buffer_user_deletion(void) {
@@ -569,6 +576,11 @@ static void test_pools_merged_on_buffer_user_deletion(void) {
       GPR_ASSERT(done);
       GPR_ASSERT(reclaimer_cancelled);
     }
+    {
+      grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+      grpc_buffer_user_destroy(&exec_ctx, &usr);
+      grpc_exec_ctx_finish(&exec_ctx);
+    }
   }
   grpc_buffer_pool_unref(p);
 }
@@ -678,6 +690,11 @@ static void test_one_slice_deleted_late(void) {
   grpc_buffer_pool_unref(p);
   gpr_slice_buffer_destroy(&buffer);
   GPR_ASSERT(done);
+  {
+    grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+    grpc_buffer_user_destroy(&exec_ctx, &usr);
+    grpc_exec_ctx_finish(&exec_ctx);
+  }
 }
 
 int main(int argc, char **argv) {
-- 
GitLab