diff --git a/connections/implementation/bwu_manager.cc b/connections/implementation/bwu_manager.cc index 4a1fc4ef..dbf4395a 100644 --- a/connections/implementation/bwu_manager.cc +++ b/connections/implementation/bwu_manager.cc @@ -129,8 +129,7 @@ void BwuManager::Shutdown() { V1Frame::BANDWIDTH_UPGRADE_NEGOTIATION, this); // Stop all the ongoing Runnables (as gracefully as possible). - alarm_executor_.Shutdown(); - serial_executor_.Shutdown(); + ShutdownExecutors(); // After worker threads are down we became exclusive owners of data and // may access it from current thread. @@ -162,6 +161,11 @@ void BwuManager::InvokeOnIncomingConnectionForTesting( OnIncomingConnection(client, std::move(mutable_connection)); } +void BwuManager::ShutdownExecutors() { + alarm_executor_.Shutdown(); + serial_executor_.Shutdown(); +} + void BwuManager::InitiateBwuForEndpoint(ClientProxy* client, const std::string& endpoint_id, Medium new_medium) { diff --git a/connections/implementation/bwu_manager.h b/connections/implementation/bwu_manager.h index 710c5349..ae9e642b 100644 --- a/connections/implementation/bwu_manager.h +++ b/connections/implementation/bwu_manager.h @@ -108,6 +108,11 @@ class BwuManager : public EndpointManager::FrameProcessor { ClientProxy* client, std::unique_ptr mutable_connection); + // Shutdown the executors during Nearby Connections shutdown process before + // Core objects destruction so that no task will be run/posted after + // ClientProxy objects are deleted. + void ShutdownExecutors(); + private: static constexpr absl::Duration kReadClientIntroductionFrameTimeout = absl::Seconds(5); diff --git a/connections/implementation/mock_service_controller.h b/connections/implementation/mock_service_controller.h index bea01560..10639253 100644 --- a/connections/implementation/mock_service_controller.h +++ b/connections/implementation/mock_service_controller.h @@ -83,6 +83,8 @@ class MockServiceController : public ServiceController { MOCK_METHOD(void, DisconnectFromEndpoint, (ClientProxy * client, const std::string& endpoint_id), (override)); + + MOCK_METHOD(void, ShutdownBwuManagerExecutors, (), (override)); }; } // namespace connections diff --git a/connections/implementation/offline_service_controller.cc b/connections/implementation/offline_service_controller.cc index ccecbee2..9e1bbf2c 100644 --- a/connections/implementation/offline_service_controller.cc +++ b/connections/implementation/offline_service_controller.cc @@ -142,6 +142,10 @@ void OfflineServiceController::DisconnectFromEndpoint( endpoint_manager_.UnregisterEndpoint(client, endpoint_id); } +void OfflineServiceController::ShutdownBwuManagerExecutors() { + bwu_manager_.ShutdownExecutors(); +} + } // namespace connections } // namespace nearby } // namespace location diff --git a/connections/implementation/offline_service_controller.h b/connections/implementation/offline_service_controller.h index 5c9de7b3..e6f5885b 100644 --- a/connections/implementation/offline_service_controller.h +++ b/connections/implementation/offline_service_controller.h @@ -77,6 +77,8 @@ class OfflineServiceController : public ServiceController { void Stop() override; + void ShutdownBwuManagerExecutors() override; + private: // Note that the order of declaration of these is crucial, because we depend // on the destructors running (strictly) in the reverse order; a deviation diff --git a/connections/implementation/service_controller.h b/connections/implementation/service_controller.h index 11e87a6d..d924549c 100644 --- a/connections/implementation/service_controller.h +++ b/connections/implementation/service_controller.h @@ -56,6 +56,10 @@ class ServiceController { // controller are affected. virtual void Stop() = 0; + // Shuts down executors in the BwuManager. After that no tasks should be + // running on or posted to BwuManager. + virtual void ShutdownBwuManagerExecutors() = 0; + // Starts advertising an endpoint for a local app. virtual Status StartAdvertising(ClientProxy* client, const std::string& service_id, diff --git a/connections/implementation/service_controller_router.cc b/connections/implementation/service_controller_router.cc index 60b2377e..a8c43d27 100644 --- a/connections/implementation/service_controller_router.cc +++ b/connections/implementation/service_controller_router.cc @@ -368,6 +368,7 @@ void ServiceControllerRouter::FinishClientSession(ClientProxy* client) { // Stop any advertising and discovery that may be underway due to this client. GetServiceController()->StopAdvertising(client); GetServiceController()->StopDiscovery(client); + GetServiceController()->ShutdownBwuManagerExecutors(); // Finally, clear all state maintained by this client. client->Reset();