From 039b64766670e796d13de0bf6b7bb82dde2d0b17 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Wed, 13 Jul 2022 05:09:34 -0700 Subject: [PATCH] Schedule close on the scheduler Summary: ^ If Flipper stops, we cannot freely access the client outside of the scheduler. There may be an scheduled operation that depends on it. Instead, schedule the stop and wait until it finishes. Reviewed By: cekkaewnumchai Differential Revision: D37814820 fbshipit-source-id: 44217b5f623a8d92211a3d72a4a204d674b4bb76 --- .../Flipper/FlipperConnectionManagerImpl.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/xplat/Flipper/FlipperConnectionManagerImpl.cpp b/xplat/Flipper/FlipperConnectionManagerImpl.cpp index b52b1ed01..eee230582 100644 --- a/xplat/Flipper/FlipperConnectionManagerImpl.cpp +++ b/xplat/Flipper/FlipperConnectionManagerImpl.cpp @@ -305,10 +305,18 @@ void FlipperConnectionManagerImpl::stop() { } isStarted_ = false; - if (client_) { - client_->disconnect(); - } - client_ = nullptr; + std::shared_ptr> joinPromise = + std::make_shared>(); + std::future join = joinPromise->get_future(); + flipperScheduler_->schedule([this, joinPromise]() { + if (client_) { + client_->disconnect(); + } + client_ = nullptr; + joinPromise->set_value(); + }); + + join.wait(); } bool FlipperConnectionManagerImpl::isOpen() const { @@ -453,6 +461,9 @@ void FlipperConnectionManagerImpl::requestSignedCertificate() { certificateExchangeCompleted_ = false; flipperScheduler_->schedule([this, message, gettingCert]() { + if (!client_) { + return; + } client_->sendExpectResponse( folly::toJson(message), [this, gettingCert](const std::string& response, bool isError) {