Change SonarInitConfig to take two EventBases

Summary:
We currently give sonar one event base from java / obj-c code to use for scheduling tasks.
This causes a problem, because we schedule reconnect tasks on the event base, and then these tasks interact with rsocket which schedules it's own tasks.
The problem is that we're passing rsocket the same event base. So the reconnect code executes and blocks waiting for rsocket to connect.
But rsocket can never connect because it never executes because that thread is blocked, so we get deadlock.

This is the first step which just changes the interface to pass two event bases.
The consumers will be changed to pass in different threads next.

Reviewed By: danielbuechele

Differential Revision: D8748350

fbshipit-source-id: 481d6f23644f28fd0f1c786252605287019c999c
This commit is contained in:
John Knox
2018-07-09 07:45:59 -07:00
committed by Facebook Github Bot
parent 7ed154c510
commit cebc409da6
7 changed files with 25 additions and 14 deletions

View File

@@ -33,7 +33,12 @@ struct SonarInitConfig {
/**
EventBase on which client callbacks should be called.
*/
folly::EventBase* worker;
folly::EventBase* callbackWorker;
/**
EventBase to be used to maintain the network connection.
*/
folly::EventBase* connectionWorker;
};
} // namespace sonar

View File

@@ -90,7 +90,7 @@ class Responder : public rsocket::RSocketResponder {
};
SonarWebSocketImpl::SonarWebSocketImpl(SonarInitConfig config)
: deviceData_(config.deviceData), worker_(config.worker) {}
: deviceData_(config.deviceData), sonarEventBase_(config.callbackWorker), connectionEventBase_(config.connectionWorker) {}
SonarWebSocketImpl::~SonarWebSocketImpl() {
stop();
@@ -98,7 +98,7 @@ SonarWebSocketImpl::~SonarWebSocketImpl() {
void SonarWebSocketImpl::start() {
folly::makeFuture()
.via(worker_->getEventBase())
.via(sonarEventBase_->getEventBase())
.delayedUnsafe(std::chrono::milliseconds(0))
.then([this]() { startSync(); });
}
@@ -135,7 +135,7 @@ void SonarWebSocketImpl::doCertificateExchange() {
client_ =
rsocket::RSocket::createConnectedClient(
std::make_unique<rsocket::TcpConnectionFactory>(
*worker_->getEventBase(), std::move(address)),
*connectionEventBase_->getEventBase(), std::move(address)),
std::move(parameters),
nullptr,
std::chrono::seconds(connectionKeepaliveSeconds), // keepaliveInterval
@@ -170,7 +170,7 @@ void SonarWebSocketImpl::connectSecurely() {
client_ =
rsocket::RSocket::createConnectedClient(
std::make_unique<rsocket::TcpConnectionFactory>(
*worker_->getEventBase(),
*connectionEventBase_->getEventBase(),
std::move(address),
std::move(sslContext)),
std::move(parameters),
@@ -184,7 +184,7 @@ void SonarWebSocketImpl::connectSecurely() {
void SonarWebSocketImpl::reconnect() {
folly::makeFuture()
.via(worker_->getEventBase())
.via(sonarEventBase_->getEventBase())
.delayedUnsafe(std::chrono::seconds(reconnectIntervalSeconds))
.then([this]() { startSync(); });
}
@@ -203,7 +203,7 @@ void SonarWebSocketImpl::setCallbacks(Callbacks* callbacks) {
}
void SonarWebSocketImpl::sendMessage(const folly::dynamic& message) {
worker_->add([this, message]() {
sonarEventBase_->add([this, message]() {
if (client_) {
client_->getRequester()
->fireAndForget(rsocket::Payload(folly::toJson(message)))
@@ -239,7 +239,7 @@ void SonarWebSocketImpl::requestSignedCertFromSonar() {
folly::dynamic message = folly::dynamic::object("method", "signCertificate")(
"csr", csr.c_str())("destination", absoluteFilePath("").c_str());
worker_->add([this, message]() {
sonarEventBase_->add([this, message]() {
client_->getRequester()
->fireAndForget(rsocket::Payload(folly::toJson(message)))
->subscribe([this]() {

View File

@@ -47,7 +47,8 @@ class SonarWebSocketImpl : public SonarWebSocket {
Callbacks* callbacks_;
DeviceData deviceData_;
folly::EventBase* worker_;
folly::EventBase* sonarEventBase_;
folly::EventBase* connectionEventBase_;
std::unique_ptr<rsocket::RSocketClient> client_;
bool connectionIsTrusted_;
int failedConnectionAttempts_ = 0;