From d0ecb46d646e0f74f8f019307f721496781331ba Mon Sep 17 00:00:00 2001 From: John Knox Date: Mon, 9 Jul 2018 07:46:09 -0700 Subject: [PATCH] Skip initialisation if not running in own thread Summary: One design goal of sonar is to never cause the host app to crash or hang. For this reason, we do all heavy work in a background thread. If we detect that it's not running in it's own thread, just return so we don't hold up the caller. Reviewed By: danielbuechele Differential Revision: D8767288 fbshipit-source-id: e146cc2cfe5c3e62d12f527ff79f24c74873d4ff --- xplat/Sonar/SonarWebSocketImpl.cpp | 10 ++++++++++ xplat/Sonar/SonarWebSocketImpl.h | 1 + 2 files changed, 11 insertions(+) diff --git a/xplat/Sonar/SonarWebSocketImpl.cpp b/xplat/Sonar/SonarWebSocketImpl.cpp index b10de40a3e7..021d85744fd 100644 --- a/xplat/Sonar/SonarWebSocketImpl.cpp +++ b/xplat/Sonar/SonarWebSocketImpl.cpp @@ -33,6 +33,8 @@ #define SONAR_CA_FILE_NAME "sonarCA.crt" #define CLIENT_CERT_FILE_NAME "device.crt" #define PRIVATE_KEY_FILE "privateKey.pem" +#define WRONG_THREAD_EXIT_MSG \ + "ERROR: Aborting sonar initialization because it's not running in the sonar thread." static constexpr int reconnectIntervalSeconds = 2; static constexpr int connectionKeepaliveSeconds = 10; @@ -104,6 +106,10 @@ void SonarWebSocketImpl::start() { } void SonarWebSocketImpl::startSync() { + if (!isRunningInOwnThread()) { + SONAR_LOG(WRONG_THREAD_EXIT_MSG); + return; + } if (isOpen()) { SONAR_LOG("Already connected"); return; @@ -289,6 +295,10 @@ bool SonarWebSocketImpl::ensureSonarDirExists() { } } +bool SonarWebSocketImpl::isRunningInOwnThread() { + return sonarEventBase_->isInEventBaseThread(); +} + bool fileExists(std::string fileName) { struct stat buffer; return stat(fileName.c_str(), &buffer) == 0; diff --git a/xplat/Sonar/SonarWebSocketImpl.h b/xplat/Sonar/SonarWebSocketImpl.h index 7d91fcd18db..ca1e3710a02 100644 --- a/xplat/Sonar/SonarWebSocketImpl.h +++ b/xplat/Sonar/SonarWebSocketImpl.h @@ -62,6 +62,7 @@ class SonarWebSocketImpl : public SonarWebSocket { bool isCertificateExchangeNeeded(); void requestSignedCertFromSonar(); bool ensureSonarDirExists(); + bool isRunningInOwnThread(); }; } // namespace sonar