[Sheepdog] [zookeeper][PATCH 1/5] Fix bug: sheep crash in zk_block

Yunkai Zhang yunkai.me at gmail.com
Tue Mar 6 19:10:32 CET 2012


Add lock to prevent zb_block popping from empty queue before
zb_dispatch have push_back in it.

Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com>
---
 sheep/cluster/zookeeper.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index 6da8626..39d9592 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -14,6 +14,7 @@
 #include <netdb.h>
 #include <search.h>
 #include <assert.h>
+#include <pthread.h>
 #include <sys/eventfd.h>
 #include <zookeeper/zookeeper.h>
 
@@ -60,6 +61,8 @@ struct zk_event {
 	int callbacked; /* set non-zero if sheep already called block_cb() */
 };
 
+/* protect queue_start_pos */
+static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
 
 /* ZooKeeper-based lock */
 
@@ -445,12 +448,16 @@ static void zk_block(struct work *work)
 {
 	struct zk_event ev;
 
+	pthread_mutex_lock(&queue_lock);
+
 	zk_queue_pop(zhandle, &ev);
 
 	ev.block_cb(ev.buf);
 	ev.blocked = 0;
 
 	zk_queue_push_back(zhandle, &ev);
+
+	pthread_mutex_unlock(&queue_lock);
 }
 
 static void zk_block_done(struct work *work)
@@ -475,6 +482,8 @@ static int zk_dispatch(void)
 	if (ret < 0)
 		return 0;
 
+	pthread_mutex_lock(&queue_lock);
+
 	ret = zk_queue_pop(zhandle, &ev);
 	if (ret < 0)
 		goto out;
@@ -541,6 +550,7 @@ static int zk_dispatch(void)
 		break;
 	}
 out:
+	pthread_mutex_unlock(&queue_lock);
 	return 0;
 }
 
-- 
1.7.7.6




More information about the sheepdog mailing list