Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
Skip initialisation if not running in own thread
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jknoxville authored and facebook-github-bot committed Jul 9, 2018
1 parent 85e6bf6 commit d0ecb46
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions xplat/Sonar/SonarWebSocketImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions xplat/Sonar/SonarWebSocketImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class SonarWebSocketImpl : public SonarWebSocket {
bool isCertificateExchangeNeeded();
void requestSignedCertFromSonar();
bool ensureSonarDirExists();
bool isRunningInOwnThread();
};

} // namespace sonar
Expand Down

0 comments on commit d0ecb46

Please sign in to comment.