diff -x '*.so' -x Makefile -x '*.marks' -x '*.txt' -x '*.o' -x '*.internal' -x '*.includecache' -x '*.make' -x '*.cmake' -uNrp base_source/mariadb-10.1.24/storage/connect//ha_connect.cc mariadb-10.1.24/storage/connect//ha_connect.cc
--- base_source/mariadb-10.1.24/storage/connect//ha_connect.cc	2017-05-29 19:48:09.000000000 +0300
+++ mariadb-10.1.24/storage/connect//ha_connect.cc	2017-07-18 11:34:59.000000000 +0300
@@ -86,6 +86,7 @@
   ha_connect::rnd_next
   ha_connect::rnd_next
   ha_connect::rnd_next
+
   ha_connect::rnd_next
   ha_connect::extra
   ENUM HA_EXTRA_NO_CACHE     End caching of records (def)
@@ -723,6 +724,7 @@ static int connect_done_func(void *)
 	PROFILE_End();
 #endif  // !__WIN__
 
+    pthread_mutex_lock(&user_connect::to_users_mutex);
   for (pc= user_connect::to_users; pc; pc= pn) {
     if (pc->g)
       PlugCleanup(pc->g, true);
@@ -730,6 +732,7 @@ static int connect_done_func(void *)
     pn= pc->next;
     delete pc;
     } // endfor pc
+  pthread_mutex_unlock(&user_connect::to_users_mutex);
 
 	connect_hton= NULL;
   DBUG_RETURN(error);
@@ -843,6 +846,7 @@ ha_connect::~ha_connect(void)
 static void PopUser(PCONNECT xp)
 {
 	if (xp) {
+      pthread_mutex_lock(&user_connect::to_users_mutex);
 		xp->count--;
 
 		if (!xp->count) {
@@ -866,7 +870,7 @@ static void PopUser(PCONNECT xp)
 			PlugCleanup(xp->g, true);
 			delete xp;
 		} // endif count
-
+        pthread_mutex_unlock(&user_connect::to_users_mutex);
 	} // endif xp
 
 } // end of PopUser
@@ -883,10 +887,16 @@ static PCONNECT GetUser(THD *thd, PCONNE
   if (xp && thd == xp->thdp)
     return xp;
 
+  pthread_mutex_lock(&user_connect::to_users_mutex);
   for (xp= user_connect::to_users; xp; xp= xp->next)
     if (thd == xp->thdp)
       break;
 
+  if (xp) {
+    xp->count++;
+  }
+  pthread_mutex_unlock(&user_connect::to_users_mutex);
+
   if (!xp) {
     xp= new user_connect(thd);
 
@@ -894,9 +904,7 @@ static PCONNECT GetUser(THD *thd, PCONNE
       delete xp;
       xp= NULL;
       } // endif user_init
-
-  } else
-    xp->count++;
+  }
 
   return xp;
 } // end of GetUser
@@ -906,7 +914,12 @@ static PCONNECT GetUser(THD *thd, PCONNE
 /****************************************************************************/
 static PGLOBAL GetPlug(THD *thd, PCONNECT& lxp)
 {
+  PCONNECT old_xp = lxp;
   lxp= GetUser(thd, lxp);
+  if(old_xp!=NULL && old_xp!=lxp) {
+    PCONNECT_Guard guard(old_xp);
+  }
+
   return (lxp) ? lxp->g : NULL;
 } // end of GetPlug
 
@@ -3229,7 +3242,11 @@ int ha_connect::open(const char *name, i
   thr_lock_data_init(&share->lock,&lock,NULL);
 
   // Try to get the user if possible
+  PCONNECT old_xp = xp;
   xp= GetUser(ha_thd(), xp);
+  if(old_xp!=NULL && old_xp!=xp) {
+    PCONNECT_Guard guard(old_xp);
+  }
   PGLOBAL g= (xp) ? xp->g : NULL;
 
   // Try to set the database environment
@@ -6025,7 +6042,11 @@ int ha_connect::create(const char *name,
 #if defined(WITH_PARTITION_STORAGE_ENGINE)
   partition_info *part_info= table_arg->part_info;
 #endif   // WITH_PARTITION_STORAGE_ENGINE
+  PCONNECT old_xp = xp;
   xp= GetUser(thd, xp);
+  if(old_xp!=NULL && old_xp!=xp) {
+    PCONNECT_Guard guard(old_xp);
+  }
   PGLOBAL g= xp->g;
 
   DBUG_ENTER("ha_connect::create");
@@ -6737,7 +6758,11 @@ ha_connect::check_if_supported_inplace_a
   TABTYPE         newtyp, type= TAB_UNDEF;
   HA_CREATE_INFO *create_info= ha_alter_info->create_info;
   PTOS            newopt, oldopt;
+  PCONNECT old_xp = xp;
   xp= GetUser(thd, xp);
+  if(old_xp!=NULL && old_xp!=xp) {
+    PCONNECT_Guard guard(old_xp);
+  }
   PGLOBAL         g= xp->g;
 
   if (!g || !table) {
diff -x '*.so' -x Makefile -x '*.marks' -x '*.txt' -x '*.o' -x '*.internal' -x '*.includecache' -x '*.make' -x '*.cmake' -uNrp base_source/mariadb-10.1.24/storage/connect//plgdbutl.cpp mariadb-10.1.24/storage/connect//plgdbutl.cpp
--- base_source/mariadb-10.1.24/storage/connect//plgdbutl.cpp	2017-05-29 19:48:09.000000000 +0300
+++ mariadb-10.1.24/storage/connect//plgdbutl.cpp	2017-07-04 13:02:47.000000000 +0300
@@ -960,6 +960,10 @@ int PlugCloseFile(PGLOBAL g __attribute_
 /***********************************************************************/
 void PlugCleanup(PGLOBAL g, bool dofree)
   {
+    if (g==NULL || g->Activityp==NULL) {
+      return;
+    }
+   
   PCATLG  cat;
   PDBUSER dbuserp = (PDBUSER)g->Activityp->Aptr;
 
diff -x '*.so' -x Makefile -x '*.marks' -x '*.txt' -x '*.o' -x '*.internal' -x '*.includecache' -x '*.make' -x '*.cmake' -uNrp base_source/mariadb-10.1.24/storage/connect//user_connect.cc mariadb-10.1.24/storage/connect//user_connect.cc
--- base_source/mariadb-10.1.24/storage/connect//user_connect.cc	2017-05-29 19:48:09.000000000 +0300
+++ mariadb-10.1.24/storage/connect//user_connect.cc	2017-07-18 11:34:49.000000000 +0300
@@ -51,6 +51,7 @@
 /*  Initialize the user_connect static member.                              */
 /****************************************************************************/
 PCONNECT user_connect::to_users= NULL;
+pthread_mutex_t user_connect::to_users_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /****************************************************************************/
 /*  Get the work_size SESSION variable value .                              */
@@ -122,14 +123,16 @@ bool user_connect::user_init()
   strcpy(ap->Ap_Name, "CONNECT");
   g->Activityp= ap;
   g->Activityp->Aptr= dup;
+  pthread_mutex_lock(&user_connect::to_users_mutex);
   next= to_users;
   to_users= this;
 
   if (next)
     next->previous= this;
 
-  last_query_id= thdp->query_id;
   count= 1;
+  pthread_mutex_unlock(&user_connect::to_users_mutex);
+  last_query_id= thdp->query_id;
   return false;
 } // end of user_init
 
diff -x '*.so' -x Makefile -x '*.marks' -x '*.txt' -x '*.o' -x '*.internal' -x '*.includecache' -x '*.make' -x '*.cmake' -uNrp base_source/mariadb-10.1.24/storage/connect//user_connect.h mariadb-10.1.24/storage/connect//user_connect.h
--- base_source/mariadb-10.1.24/storage/connect//user_connect.h	2017-05-29 19:48:09.000000000 +0300
+++ mariadb-10.1.24/storage/connect//user_connect.h	2017-07-18 11:30:09.000000000 +0300
@@ -24,6 +24,13 @@
   /sql/handler.h and /storage/connect/user_connect.cc
 */
 
+#include <string>
+#include <fstream>
+#include <sstream>
+#include <execinfo.h>
+#include <stdint.h>
+#include <pthread.h>
+
 #ifdef USE_PRAGMA_INTERFACE
 #pragma interface     /* gcc class implementation */
 #endif
@@ -60,6 +67,7 @@ public:
   // Members
   THD         *thdp;                    // To the user thread
   static PCONNECT  to_users;            // To the chain of users
+  static pthread_mutex_t to_users_mutex;
   PCONNECT     next;                    // Next user in chain
   PCONNECT     previous;                // Previous user in chain
   PGLOBAL      g;                       // The common handle to CONNECT
@@ -70,3 +78,49 @@ public:
   ulonglong    tb1;
 }; // end of user_connect class definition
 
+class PCONNECT_Guard
+{
+public:
+  PCONNECT_Guard(PCONNECT pconnect) : _pconnect(pconnect)
+  {}
+
+  void release() {
+    if(_pconnect==NULL) {
+      return;
+    }
+    pthread_mutex_lock(&user_connect::to_users_mutex);
+    --_pconnect->count;
+
+    if(_pconnect->count<=0) {
+      PCONNECT p = NULL;
+
+      for (p= user_connect::to_users; p; p= p->next)
+        if (p == _pconnect)
+          break;
+
+      if(p!=NULL)
+      {
+        if(_pconnect->next!=NULL) {
+          _pconnect->next->previous = _pconnect->previous;
+        }
+
+        if(_pconnect->previous==NULL) {
+          user_connect::to_users = _pconnect->next;
+        } else {
+          _pconnect->previous->next = _pconnect->next;
+        }
+      }
+
+      PlugCleanup(_pconnect->g, true);
+      delete _pconnect;
+    }
+    _pconnect = NULL;
+    pthread_mutex_unlock(&user_connect::to_users_mutex);
+  }
+
+  ~PCONNECT_Guard() {
+    release();
+  }
+private:
+  PCONNECT _pconnect;
+};
