From a97596adbdeaa11ae1b325e9969476b7bb25bf1f Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Tue, 19 Jul 2022 08:19:19 -0700 Subject: [PATCH] XdsClient: drop refs to ChannelState objects upon XdsClient shutdown (#30266) * XdsClient: drop refs to ChannelState objects upon XdsClient shutdown * be defensive to make sure we don't access a null pointer --- src/core/ext/xds/xds_client.cc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/core/ext/xds/xds_client.cc b/src/core/ext/xds/xds_client.cc index a03f3cff240..7a374da9130 100644 --- a/src/core/ext/xds/xds_client.cc +++ b/src/core/ext/xds/xds_client.cc @@ -1193,9 +1193,11 @@ void XdsClient::ChannelState::LrsCallState::Reporter::OnReportDoneLocked() { // If there are no more registered stats to report, cancel the call. auto it = xds_client()->xds_load_report_server_map_.find(parent_->chand()->server_); - if (it == xds_client()->xds_load_report_server_map_.end() || - it->second.load_report_map.empty()) { - it->second.channel_state->StopLrsCallLocked(); + if (it == xds_client()->xds_load_report_server_map_.end()) return; + if (it->second.load_report_map.empty()) { + if (it->second.channel_state != nullptr) { + it->second.channel_state->StopLrsCallLocked(); + } return; } // Otherwise, schedule the next load report. @@ -1414,6 +1416,13 @@ void XdsClient::Orphan() { // Clear cache and any remaining watchers that may not have been cancelled. authority_state_map_.clear(); invalid_watchers_.clear(); + // We may still be sending lingering queued load report data, so don't + // just clear the load reporting map, but we do want to clear the refs + // we're holding to the ChannelState objects, to make sure that + // everything shuts down properly. + for (auto& p : xds_load_report_server_map_) { + p.second.channel_state.reset(DEBUG_LOCATION, "XdsClient::Orphan()"); + } } RefCountedPtr XdsClient::GetOrCreateChannelStateLocked(