Make AndroidNetworkMonitor::Start() create a new task safety flag

Instead of using SetAlive on the old flag (which might allow old
tasks in the queue to run).

Bug: webrtc:12339
Change-Id: Ia1a3eb6932f62881f013fd62b0e008d97d8713cd
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/211863
Reviewed-by: Tommi <tommi@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33475}
This commit is contained in:
Niels Möller
2021-03-15 16:40:24 +01:00
committed by Commit Bot
parent a776f5198f
commit 3e774f64b0
2 changed files with 7 additions and 6 deletions

View File

@ -229,8 +229,7 @@ AndroidNetworkMonitor::AndroidNetworkMonitor(
: android_sdk_int_(Java_NetworkMonitor_androidSdkInt(env)),
j_application_context_(env, j_application_context),
j_network_monitor_(env, Java_NetworkMonitor_getInstance(env)),
network_thread_(rtc::Thread::Current()),
safety_flag_(PendingTaskSafetyFlag::Create()) {}
network_thread_(rtc::Thread::Current()) {}
AndroidNetworkMonitor::~AndroidNetworkMonitor() {
RTC_DCHECK(!started_);
@ -250,8 +249,10 @@ void AndroidNetworkMonitor::Start() {
bind_using_ifname_ =
webrtc::field_trial::IsEnabled("WebRTC-BindUsingInterfaceName");
// Needed for restart after Stop().
safety_flag_->SetAlive();
// This pointer is also accessed by the methods called from java threads.
// Assigning it here is safe, because the java monitor is in a stopped state,
// and will not make any callbacks.
safety_flag_ = PendingTaskSafetyFlag::Create();
JNIEnv* env = AttachCurrentThreadIfNeeded();
Java_NetworkMonitor_startMonitoring(

View File

@ -149,8 +149,8 @@ class AndroidNetworkMonitor : public rtc::NetworkMonitorInterface {
// This applies to adapter_type_by_name_, vpn_underlying_adapter_type_by_name_
// and FindNetworkHandleFromIfname.
bool bind_using_ifname_ RTC_GUARDED_BY(network_thread_) = true;
const rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag_
RTC_PT_GUARDED_BY(network_thread_);
rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag_
RTC_PT_GUARDED_BY(network_thread_) = nullptr;
};
class AndroidNetworkMonitorFactory : public rtc::NetworkMonitorFactory {