From eec1810760ccbdf95c68ed0d2c2ae10a8575551a Mon Sep 17 00:00:00 2001 From: Tommi Date: Thu, 22 Jun 2023 10:13:52 +0200 Subject: [PATCH] Avoid touching channel after OnSctpDataChannelClosed Bug: chromium:1454086 Change-Id: I39573b706c4031d091c45a182b13cb3b2dba6233 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/309920 Reviewed-by: Harald Alvestrand Commit-Queue: Tomas Gunnarsson Cr-Commit-Position: refs/heads/main@{#40332} --- pc/data_channel_controller.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pc/data_channel_controller.cc b/pc/data_channel_controller.cc index 7fea6c5e55..93599fdba9 100644 --- a/pc/data_channel_controller.cc +++ b/pc/data_channel_controller.cc @@ -70,6 +70,11 @@ void DataChannelController::OnChannelStateChanged( SctpDataChannel* channel, DataChannelInterface::DataState state) { RTC_DCHECK_RUN_ON(network_thread()); + + // Stash away the internal id here in case `OnSctpDataChannelClosed` ends up + // releasing the last reference to the channel. + const int channel_id = channel->internal_id(); + if (state == DataChannelInterface::DataState::kClosed) OnSctpDataChannelClosed(channel); @@ -77,8 +82,7 @@ void DataChannelController::OnChannelStateChanged( ? DataChannelUsage::kHaveBeenUsed : DataChannelUsage::kInUse; signaling_thread()->PostTask(SafeTask( - signaling_safety_.flag(), [this, channel_id = channel->internal_id(), - state = state, channel_usage] { + signaling_safety_.flag(), [this, channel_id, state, channel_usage] { RTC_DCHECK_RUN_ON(signaling_thread()); channel_usage_ = channel_usage; pc_->OnSctpDataChannelStateChanged(channel_id, state);