Fix race condition on ObjCNetworkMonitor::network_monitor_

There is a race condition on reading
`ObjCNetworkMonitor::_network_monitor` field.
The `ObjCNetworkMonitor::OnPathUpdate()` checks its nullability
on the org.webrtc.RTCDispatcherNetworkMonitor thread BEFORE the
`ObjCNetworkMonitor::Start()` assigns it on the network_monitor thread.
In addition, this field is neither atomic nor protected by mutex so its
last assigned value is not guaranteed to be visible to
another [reading] thread.

Bug: webrtc:355238623
Change-Id: I1a05215111cc873b7d4931824e18f281aebfb91f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/357880
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org>
Reviewed-by: Peter Hanspers <peterhanspers@webrtc.org>
Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42778}
This commit is contained in:
Andy Tamilo 2024-07-26 10:49:03 +03:00 committed by WebRTC LUCI CQ
parent 4fffeabba3
commit b8841f8ebe
2 changed files with 2 additions and 1 deletions

View File

@ -20,6 +20,7 @@ Alex Henrie <alexhenrie24@gmail.com>
Andrew MacDonald <andrew@webrtc.org>
Andrey Efremov <yoklmnprst@ya.ru>
Andrew Johnson <ajohnson@draster.com>
Andy Tamilo <andytamilo@gmail.com>
Anil Kumar <an1kumar@gmail.com>
Anton Barkov <anton.barkov@macpaw.com>
Ben Strong <bstrong@gmail.com>

View File

@ -84,9 +84,9 @@ rtc::NetworkMonitorInterface::InterfaceInfo ObjCNetworkMonitor::GetInterfaceInfo
void ObjCNetworkMonitor::OnPathUpdate(
std::map<std::string, rtc::AdapterType, rtc::AbslStringViewCmp> adapter_type_by_name) {
RTC_DCHECK(network_monitor_ != nil);
thread_->PostTask(SafeTask(safety_flag_, [this, adapter_type_by_name] {
RTC_DCHECK_RUN_ON(thread_);
RTC_DCHECK(network_monitor_ != nil);
adapter_type_by_name_ = adapter_type_by_name;
InvokeNetworksChangedCallback();
}));