diff --git a/internal/platform/implementation/windows/wifi_lan_mdns.cc b/internal/platform/implementation/windows/wifi_lan_mdns.cc index 792b7d8d..b86c2057 100644 --- a/internal/platform/implementation/windows/wifi_lan_mdns.cc +++ b/internal/platform/implementation/windows/wifi_lan_mdns.cc @@ -157,24 +157,18 @@ bool WifiLanMdns::StopMdnsService() { if (status != DNS_REQUEST_PENDING) { NEARBY_LOGS(ERROR) << "Failed to stop mDNS advertising."; + CleanUp(); return false; } if (!dns_service_notification_->WaitForNotificationWithTimeout( kDnsServiceTimeout)) { LOG(ERROR) << "Failed to start mDNS advertising."; + CleanUp(); return false; } - dns_service_notification_ = nullptr; - if (dns_service_instance_.keys != nullptr) { - delete[] dns_service_instance_.keys; - delete[] dns_service_instance_.values; - dns_service_instance_.keys = nullptr; - dns_service_instance_.values = nullptr; - } - - is_service_started_ = false; + CleanUp(); LOG(INFO) << "Succeeded to stop mDNS advertising."; return true; @@ -206,4 +200,15 @@ void WifiLanMdns::DnsServiceRegisterComplete(DWORD Status, PVOID pQueryContext, mdns->NotifyStatusUpdated(Status); } +void WifiLanMdns::CleanUp() { + is_service_started_ = false; + dns_service_notification_ = nullptr; + if (dns_service_instance_.keys != nullptr) { + delete[] dns_service_instance_.keys; + delete[] dns_service_instance_.values; + dns_service_instance_.keys = nullptr; + dns_service_instance_.values = nullptr; + } +} + } // namespace nearby::windows diff --git a/internal/platform/implementation/windows/wifi_lan_mdns.h b/internal/platform/implementation/windows/wifi_lan_mdns.h index d83e6e7a..9cc10bff 100644 --- a/internal/platform/implementation/windows/wifi_lan_mdns.h +++ b/internal/platform/implementation/windows/wifi_lan_mdns.h @@ -38,9 +38,10 @@ class WifiLanMdns { bool StartMdnsService( const std::string& service_name, const std::string& service_type, - int port, absl::flat_hash_map text_records); + int port, absl::flat_hash_map text_records) + ABSL_LOCKS_EXCLUDED(mutex_); - bool StopMdnsService(); + bool StopMdnsService() ABSL_LOCKS_EXCLUDED(mutex_); void NotifyStatusUpdated(DWORD status); @@ -48,6 +49,7 @@ class WifiLanMdns { static void DnsServiceRegisterComplete(DWORD Status, PVOID pQueryContext, PDNS_SERVICE_INSTANCE pInstance); std::optional GetComputerName(); + void CleanUp() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); absl::Mutex mutex_; std::unique_ptr dns_service_notification_ = nullptr; diff --git a/internal/platform/implementation/windows/wifi_lan_medium.cc b/internal/platform/implementation/windows/wifi_lan_medium.cc index 15c44eab..6872e682 100644 --- a/internal/platform/implementation/windows/wifi_lan_medium.cc +++ b/internal/platform/implementation/windows/wifi_lan_medium.cc @@ -220,6 +220,7 @@ bool WifiLanMedium::StopAdvertising(const NsdServiceInfo& nsd_service_info) { } LOG(ERROR) << "failed to stop mDNS advertising."; + medium_status_ &= (~kMediumStatusAdvertising); return false; } else { dnssd_service_instance_ = nullptr;