887953
From 11f9ffcd733c95e8a728c150ff2ffc3dbeaddac1 Mon Sep 17 00:00:00 2001
887953
From: Mohit Agrawal <moagrawa@redhat.com>
887953
Date: Fri, 11 Jan 2019 11:57:20 +0530
887953
Subject: [PATCH 502/506] posix: posix_health_check_thread_proc crash due to
887953
 priv is NULL
887953
887953
Problem: posix_fini sends a cancellation request to health_check
887953
         thread and cleanup priv without ensuring health_check thread
887953
         is running
887953
887953
Solution: Make health_check && disk_space thread joinable and call
887953
          gf_thread_cleanup_xint to wait unless thread is not finished
887953
887953
> Change-Id: I4d37b08138766881dab0922a47ed68a2c3411f13
887953
> fixes: bz#1636570
887953
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/21717/)
887953
> (Cherry pick from commit e82bcc33ed2d5cd54d3f918397f31818089299ad)
887953
887953
Change-Id: I9edadd5bc445549b5f45bab98e4794d62a185f1c
887953
BUG: 1662828
887953
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
887953
Reviewed-on: https://code.engineering.redhat.com/gerrit/160404
887953
Tested-by: RHGS Build Bot <nigelb@redhat.com>
887953
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
887953
---
887953
 xlators/storage/posix/src/posix-helpers.c |  8 +++-----
887953
 xlators/storage/posix/src/posix.c         | 15 +++++++++++----
887953
 2 files changed, 14 insertions(+), 9 deletions(-)
887953
887953
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
887953
index cddf02e63..ed5d3e55e 100644
887953
--- a/xlators/storage/posix/src/posix-helpers.c
887953
+++ b/xlators/storage/posix/src/posix-helpers.c
887953
@@ -1854,8 +1854,6 @@ posix_spawn_health_check_thread (xlator_t *xl)
887953
                         goto unlock;
887953
                 }
887953
 
887953
-                /* run the thread detached, resources will be freed on exit */
887953
-                pthread_detach (priv->health_check);
887953
                 priv->health_check_active = _gf_true;
887953
         }
887953
 unlock:
887953
@@ -1958,9 +1956,9 @@ posix_spawn_disk_space_check_thread (xlator_t *xl)
887953
                         priv->disk_space_check_active = _gf_false;
887953
                 }
887953
 
887953
-                ret = gf_thread_create_detached (&priv->disk_space_check,
887953
-                                                 posix_disk_space_check_thread_proc,
887953
-                                                 xl, "posix_reserve");
887953
+                ret = gf_thread_create (&priv->disk_space_check, NULL,
887953
+                                        posix_disk_space_check_thread_proc,
887953
+                                        xl, "posix_reserve");
887953
                 if (ret < 0) {
887953
                         priv->disk_space_check_active = _gf_false;
887953
                         gf_msg (xl->name, GF_LOG_ERROR, errno,
887953
diff --git a/xlators/storage/posix/src/posix.c b/xlators/storage/posix/src/posix.c
887953
index 13b4aa6b6..591119ea9 100644
887953
--- a/xlators/storage/posix/src/posix.c
887953
+++ b/xlators/storage/posix/src/posix.c
887953
@@ -7999,18 +7999,25 @@ void
887953
 fini (xlator_t *this)
887953
 {
887953
         struct posix_private *priv = this->private;
887953
+        gf_boolean_t health_check = _gf_false;
887953
+
887953
         if (!priv)
887953
                 return;
887953
         LOCK (&priv->lock);
887953
-        if (priv->health_check_active) {
887953
+        {
887953
+                health_check = priv->health_check_active;
887953
                 priv->health_check_active = _gf_false;
887953
-                pthread_cancel (priv->health_check);
887953
-                priv->health_check = 0;
887953
         }
887953
         UNLOCK (&priv->lock);
887953
+
887953
+        if (health_check) {
887953
+                (void)gf_thread_cleanup_xint(priv->health_check);
887953
+                priv->health_check = 0;
887953
+        }
887953
+
887953
         if (priv->disk_space_check) {
887953
                 priv->disk_space_check_active = _gf_false;
887953
-                pthread_cancel (priv->disk_space_check);
887953
+                (void)gf_thread_cleanup_xint(priv->disk_space_check);
887953
                 priv->disk_space_check = 0;
887953
         }
887953
         if (priv->janitor) {
887953
-- 
887953
2.20.1
887953