From 5ae556499bb7f2ff1cd196b69025f6f623c8d82d Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Wed, 14 Jun 2023 08:52:00 -0700 Subject: [PATCH] Fixed the deadlock issue in hotspot PiperOrigin-RevId: 540283852 --- .../implementation/windows/wifi_direct_medium.cc | 16 ++++++++-------- .../windows/wifi_hotspot_medium.cc | 15 ++++++++------- .../windows/wifi_hotspot_server_socket.cc | 8 ++++---- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/internal/platform/implementation/windows/wifi_direct_medium.cc b/internal/platform/implementation/windows/wifi_direct_medium.cc index c27595d0..7ced0f48 100644 --- a/internal/platform/implementation/windows/wifi_direct_medium.cc +++ b/internal/platform/implementation/windows/wifi_direct_medium.cc @@ -175,16 +175,16 @@ std::unique_ptr WifiDirectMedium::ListenForService( auto server_socket = std::make_unique(port); server_socket_ptr_ = server_socket.get(); - server_socket->SetCloseNotifier([this]() { - absl::MutexLock lock(&mutex_); - NEARBY_LOGS(INFO) << "server socket was closed on port " - << server_socket_ptr_->GetPort(); - medium_status_ &= (~kMediumStatusAccepting); - server_socket_ptr_ = nullptr; - }); - if (server_socket->listen()) { medium_status_ |= kMediumStatusAccepting; + server_socket->SetCloseNotifier([this]() { + absl::MutexLock lock(&mutex_); + NEARBY_LOGS(INFO) << "server socket was closed on port " + << server_socket_ptr_->GetPort(); + medium_status_ &= (~kMediumStatusAccepting); + server_socket_ptr_ = nullptr; + }); + NEARBY_LOGS(INFO) << "started to listen serive on port " << server_socket_ptr_->GetPort(); return server_socket; diff --git a/internal/platform/implementation/windows/wifi_hotspot_medium.cc b/internal/platform/implementation/windows/wifi_hotspot_medium.cc index 8a3532f7..80c2fe3e 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_medium.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_medium.cc @@ -192,15 +192,16 @@ WifiHotspotMedium::ListenForService(int port) { auto server_socket = std::make_unique(port); server_socket_ptr_ = server_socket.get(); - server_socket->SetCloseNotifier([this]() { - absl::MutexLock lock(&mutex_); - NEARBY_LOGS(INFO) << "Server socket was closed."; - medium_status_ &= (~kMediumStatusAccepting); - server_socket_ptr_ = nullptr; - }); - if (server_socket->listen()) { medium_status_ |= kMediumStatusAccepting; + + // Setup close notifier after listen started. + server_socket->SetCloseNotifier([this]() { + absl::MutexLock lock(&mutex_); + NEARBY_LOGS(INFO) << "Server socket was closed."; + medium_status_ &= (~kMediumStatusAccepting); + server_socket_ptr_ = nullptr; + }); NEARBY_LOGS(INFO) << "Started to listen serive on port " << server_socket_ptr_->GetPort(); return server_socket; diff --git a/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc b/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc index 3b742a2f..4a49ed82 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_server_socket.cc @@ -123,6 +123,10 @@ Exception WifiHotspotServerSocket::Close() { absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << __func__ << ": Close is called."; + if (closed_) { + return {Exception::kSuccess}; + } + if (NearbyFlags::GetInstance().GetBoolFlag( platform::config_package_nearby::nearby_platform_feature:: kEnableHotspotWin32Socket)) { @@ -154,10 +158,6 @@ Exception WifiHotspotServerSocket::Close() { } } - if (closed_) { - return {Exception::kSuccess}; - } - closed_ = true; cond_.SignalAll(); if (close_notifier_ != nullptr) {