From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <sergepetrenko@tarantool.org>
From: Serge Petrenko <sergepetrenko@tarantool.org>
Subject: [PATCH] replication: fix assertion in relay_cancel()
Date: Fri, 17 Aug 2018 16:59:37 +0300
Message-Id: <20180817135937.37840-1-sergepetrenko@tarantool.org>
To: vdavydov.dev@gmail.com
Cc: georgy@tarantool.org, kostja@tarantool.org, tarantool-patches@freelists.org, Serge Petrenko <sergepetrenko@tarantool.org>
List-ID: <tarantool-patches.dev.tarantool.org>

If relay thread is already exiting (but hadn't executed relay_stop()
yet) and relay_cancel() is called we may encounter an error trying to
call pthread_cancel() after the thread has exited. Handle this case.

Follow-up #3485
---
https://github.com/tarantool/tarantool/issues/3485
https://github.com/tarantool/tarantool/tree/sp/relay-cancel-fix

 src/box/relay.cc | 3 ++-
 src/tt_pthread.h | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/box/relay.cc b/src/box/relay.cc
index b9919ea86..138e66dd5 100644
--- a/src/box/relay.cc
+++ b/src/box/relay.cc
@@ -203,7 +203,8 @@ relay_cancel(struct relay *relay)
 {
 	/* Check that the thread is running first. */
 	if (relay->cord.id != 0) {
-		tt_pthread_cancel(relay->cord.id);
+		if (tt_pthread_cancel(relay->cord.id) == ESRCH)
+			return;
 		tt_pthread_join(relay->cord.id, NULL);
 	}
 }
diff --git a/src/tt_pthread.h b/src/tt_pthread.h
index d83694460..40b43b112 100644
--- a/src/tt_pthread.h
+++ b/src/tt_pthread.h
@@ -279,7 +279,8 @@
 
 #define tt_pthread_cancel(thread)			\
 ({	int e__ = pthread_cancel(thread);		\
-	tt_pthread_error(e__);				\
+	assert(e__ == 0 || e__ == ESRCH);		\
+	e__;						\
 })
 
 #define tt_pthread_key_create(key, dtor)		\
-- 
2.15.2 (Apple Git-101.1)