From 2c08cfc9e8c1299ff4f0cfc859fa0f1901860aff Mon Sep 17 00:00:00 2001 From: Janusz Sobczak Date: Fri, 21 Jul 2023 09:10:45 -0700 Subject: [PATCH] Close BaseEndpointChannel once Closing the bluetoth socket and streams more than once is wasteful and error prone. PiperOrigin-RevId: 549964135 --- connections/implementation/base_endpoint_channel.cc | 5 +++++ connections/implementation/base_endpoint_channel.h | 1 + 2 files changed, 6 insertions(+) diff --git a/connections/implementation/base_endpoint_channel.cc b/connections/implementation/base_endpoint_channel.cc index d9b98916..d404d7aa 100644 --- a/connections/implementation/base_endpoint_channel.cc +++ b/connections/implementation/base_endpoint_channel.cc @@ -265,6 +265,11 @@ void BaseEndpointChannel::Close() { { // In case channel is paused, resume it first thing. MutexLock lock(&is_paused_mutex_); + if (is_closed_) { + NEARBY_LOGS(VERBOSE) << "EndpointChannel already closed"; + return; + } + is_closed_ = true; UnblockPausedWriter(); } CloseIo(); diff --git a/connections/implementation/base_endpoint_channel.h b/connections/implementation/base_endpoint_channel.h index e3d77f65..5259748c 100644 --- a/connections/implementation/base_endpoint_channel.h +++ b/connections/implementation/base_endpoint_channel.h @@ -130,6 +130,7 @@ class BaseEndpointChannel : public EndpointChannel { ConditionVariable is_paused_cond_{&is_paused_mutex_}; // If true, writes should block until this has been set to false. bool is_paused_ ABSL_GUARDED_BY(is_paused_mutex_) = false; + bool is_closed_ ABSL_GUARDED_BY(is_paused_mutex_) = false; // The medium technology information of this endpoint channel. location::nearby::proto::connections::ConnectionTechnology technology_;