THRIFT-5841 possible init/deinit conflict with manual initialization flag
Client: cpp
Patch: Jens Geyer

This closes #3077
diff --git a/lib/cpp/src/thrift/TOutput.cpp b/lib/cpp/src/thrift/TOutput.cpp
index 971e5db..72360af 100644
--- a/lib/cpp/src/thrift/TOutput.cpp
+++ b/lib/cpp/src/thrift/TOutput.cpp
@@ -31,7 +31,7 @@
 namespace apache {
 namespace thrift {
 
-THRIFT_EXPORT TOutput GlobalOutput;
+/*THRIFT_EXPORT*/ TOutput GlobalOutput;   // if you need this exported, build your own wrapper lib around and export it yourself
 
 TOutput::TOutput() : f_(&errorTimeWrapper) {}
 
diff --git a/lib/cpp/src/thrift/TOutput.h b/lib/cpp/src/thrift/TOutput.h
index 26c9a56..8a9061a 100644
--- a/lib/cpp/src/thrift/TOutput.h
+++ b/lib/cpp/src/thrift/TOutput.h
@@ -20,7 +20,7 @@
 #ifndef _THRIFT_OUTPUT_H_
 #define _THRIFT_OUTPUT_H_ 1
 
-#include <thrift/thrift_export.h>
+//#include <thrift/thrift_export.h>
 
 namespace apache {
 namespace thrift {
@@ -53,7 +53,7 @@
   void (*f_)(const char*);
 };
 
-THRIFT_EXPORT extern TOutput GlobalOutput;
+/*THRIFT_EXPORT*/ extern TOutput GlobalOutput;   // if you need this exported, build your own wrapper lib around and export it yourself
 }
 } // namespace apache::thrift
 
diff --git a/lib/cpp/src/thrift/transport/TSSLServerSocket.cpp b/lib/cpp/src/thrift/transport/TSSLServerSocket.cpp
index b20c174..9e9fe97 100644
--- a/lib/cpp/src/thrift/transport/TSSLServerSocket.cpp
+++ b/lib/cpp/src/thrift/transport/TSSLServerSocket.cpp
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-#include <thrift/thrift_export.h>
+//#include <thrift/thrift_export.h>
 #include <thrift/transport/TSSLServerSocket.h>
 #include <thrift/transport/TSSLSocket.h>
 
diff --git a/lib/cpp/src/thrift/transport/TSSLSocket.cpp b/lib/cpp/src/thrift/transport/TSSLSocket.cpp
index da8b027..aaedf90 100644
--- a/lib/cpp/src/thrift/transport/TSSLSocket.cpp
+++ b/lib/cpp/src/thrift/transport/TSSLSocket.cpp
@@ -875,11 +875,13 @@
 uint64_t TSSLSocketFactory::count_ = 0;
 Mutex TSSLSocketFactory::mutex_;
 bool TSSLSocketFactory::manualOpenSSLInitialization_ = false;
+bool TSSLSocketFactory::didWeInitializeOpenSSL_ = false;
 
 TSSLSocketFactory::TSSLSocketFactory(SSLProtocol protocol) : server_(false) {
   Guard guard(mutex_);
   if (count_ == 0) {
     if (!manualOpenSSLInitialization_) {
+      didWeInitializeOpenSSL_ = true;
       initializeOpenSSL();
     }
     randomize();
@@ -892,8 +894,9 @@
   Guard guard(mutex_);
   ctx_.reset();
   count_--;
-  if (count_ == 0 && !manualOpenSSLInitialization_) {
+  if (count_ == 0 && didWeInitializeOpenSSL_) {
     cleanupOpenSSL();
+    didWeInitializeOpenSSL_ = false;
   }
 }
 
diff --git a/lib/cpp/src/thrift/transport/TSSLSocket.h b/lib/cpp/src/thrift/transport/TSSLSocket.h
index 5afc571..80a68ce 100644
--- a/lib/cpp/src/thrift/transport/TSSLSocket.h
+++ b/lib/cpp/src/thrift/transport/TSSLSocket.h
@@ -327,7 +327,8 @@
   std::shared_ptr<AccessManager> access_;
   static concurrency::Mutex mutex_;
   static uint64_t count_;
-  THRIFT_EXPORT static bool manualOpenSSLInitialization_;
+  /*THRIFT_EXPORT*/ static bool manualOpenSSLInitialization_;     // questionable to export a private member
+  static bool didWeInitializeOpenSSL_;  // in that case we also perform de-init
   void setup(std::shared_ptr<TSSLSocket> ssl);
   static int passwordCallback(char* password, int size, int, void* data);
 };