Skip to content

Commit

Permalink
Fix session destruction.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 706890703
  • Loading branch information
ftsui authored and copybara-github committed Dec 17, 2024
1 parent e907d82 commit 0b7bb59
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions sharing/nearby_sharing_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3296,28 +3296,26 @@ NearbySharingServiceImpl::RemoveOutgoingShareTargetWithEndpointId(
VLOG(1) << __func__ << ":Outgoing connection to " << endpoint_id
<< " disconnected, cancel disconnection timer";
disconnection_timeout_alarms_.erase(endpoint_id);
auto it = outgoing_share_target_map_.find(endpoint_id);
if (it == outgoing_share_target_map_.end()) {
auto target_node = outgoing_share_target_map_.extract(endpoint_id);
if (target_node.empty()) {
LOG(WARNING) << __func__ << ": endpoint_id=" << endpoint_id
<< " not found in outgoing share target map.";
return std::nullopt;
}

VLOG(1) << __func__ << ": Removing (endpoint_id=" << it->first
<< ", share_target.id=" << it->second.id
ShareTarget share_target = std::move(target_node.mapped());
VLOG(1) << __func__ << ": Removing (endpoint_id=" << endpoint_id
<< ", share_target.id=" << share_target.id
<< ") from outgoing share target map";
std::optional<ShareTarget> share_target =
std::move(outgoing_share_target_map_.extract(it).mapped());

// Do not destroy the session until it has been removed from the map.
// Session destruction can trigger callbacks that traverses the map and it
// cannot access the map while it is being modified.
auto session_it = outgoing_share_session_map_.find(share_target->id);
if (session_it == outgoing_share_session_map_.end()) {
LOG(WARNING) << __func__ << ": share_target.id=" << share_target->id
<< " not found in outgoing share session map.";
auto session_node = outgoing_share_session_map_.extract(share_target.id);
if (!session_node.empty()) {
session_node.mapped().OnDisconnect();
} else {
outgoing_share_session_map_.extract(session_it).mapped().OnDisconnect();
LOG(WARNING) << __func__ << ": share_target.id=" << share_target.id
<< " not found in outgoing share session map.";
}
return share_target;
}
Expand All @@ -3341,17 +3339,16 @@ void NearbySharingServiceImpl::MoveToDiscoveryCache(std::string endpoint_id,
*service_thread_, absl::StrCat("discovery_cache_timeout_", endpoint_id),
absl::Milliseconds(expiry_ms),
[this, expiry_ms, endpoint_id = std::string(endpoint_id)]() {
auto it = discovery_cache_.find(endpoint_id);
if (it == discovery_cache_.end()) {
auto cache_node = discovery_cache_.extract(endpoint_id);
if (cache_node.empty()) {
LOG(WARNING) << "Trying to remove endpoint_id: " << endpoint_id
<< " from discovery_cache, but cannot find it";
return;
}
ShareTarget share_target = std::move(cache_node.mapped().share_target);
LOG(INFO) << ": Removing (endpoint_id=" << endpoint_id
<< ", share_target.id=" << it->second.share_target.id
<< ", share_target.id=" << share_target.id
<< ") from discovery_cache after " << expiry_ms << "ms";
ShareTarget share_target =
std::move(discovery_cache_.extract(it).mapped().share_target);

for (auto& entry : foreground_send_surface_map_) {
entry.second.OnShareTargetLost(share_target);
Expand Down Expand Up @@ -3463,8 +3460,12 @@ void NearbySharingServiceImpl::UnregisterShareTarget(int64_t share_target_id) {

// If share target ID is found in incoming_share_session_map_, then it's an
// incoming share target.
bool is_incoming = (incoming_share_session_map_.erase(share_target_id) > 0);
if (is_incoming) {
// Do not destroy the session until it has been removed from the map.
// Session destruction can trigger callbacks that traverses the map and it
// cannot access the map while it is being modified.
auto session_node = incoming_share_session_map_.extract(share_target_id);
if (!session_node.empty()) {
// IncomingShareSession session = std::move(session_node.mapped());
if (last_incoming_metadata_ &&
std::get<0>(*last_incoming_metadata_).id == share_target_id) {
last_incoming_metadata_.reset();
Expand Down

0 comments on commit 0b7bb59

Please sign in to comment.