From 305dbcf68953cf9503e1dca84670472ad159d300 Mon Sep 17 00:00:00 2001
From: Ilya Kosarev <ilyanapar@yandex.ru>
Date: Mon, 25 Feb 2019 17:49:34 +0300
Subject: [PATCH] iproto: close socket explicitly before wal_dir at exit

tarantool instance didn't close socket explicitly
which could cause hot standby instance to fail to bind
in case it tries to bind before socket is closed by OS.
Now it is fixed by closing socket explicitly before wal_dir.

Closes #3967
---
 src/box/box.cc    |  1 +
 src/box/iproto.cc | 13 +++++++++++++
 src/box/iproto.h  |  3 +++
 3 files changed, 17 insertions(+)

diff --git a/src/box/box.cc b/src/box/box.cc
index cf2254d0cf..a62595421c 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -1691,6 +1691,7 @@ box_free(void)
 		tuple_free();
 		port_free();
 #endif
+		iproto_free();
 		replication_free();
 		sequence_free();
 		gc_free();
diff --git a/src/box/iproto.cc b/src/box/iproto.cc
index 7a1d3bfd09..226b9d0651 100644
--- a/src/box/iproto.cc
+++ b/src/box/iproto.cc
@@ -2141,3 +2141,16 @@ iproto_set_msg_max(int new_iproto_msg_max)
 	iproto_do_cfg(&cfg_msg);
 	cpipe_set_max_input(&net_pipe, new_iproto_msg_max / 2);
 }
+
+void
+iproto_free()
+{
+	tt_pthread_cancel(net_cord.id);
+	tt_pthread_join(net_cord.id, NULL);
+	/*
+	* Close socket descriptor to prevent hot standby instance
+	* failing to bind in case it tries to bind before socket
+	* is closed by OS.
+	*/
+	close(binary.ev.fd);
+}
diff --git a/src/box/iproto.h b/src/box/iproto.h
index b9a6cf8f77..8f3607ffc5 100644
--- a/src/box/iproto.h
+++ b/src/box/iproto.h
@@ -80,6 +80,9 @@ iproto_listen(const char *uri);
 void
 iproto_set_msg_max(int iproto_msg_max);
 
+void
+iproto_free();
+
 #endif /* defined(__cplusplus) */
 
 #endif
-- 
GitLab