From 6d0bdd197acb7c85be6af6d3fbede6aed392791e Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Wed, 4 Oct 2023 14:59:53 +0800 Subject: [PATCH] Add synchronized block when accessing global variables This is catched by errorprone where global variables accessing is not protected by the declared lock, which could lead to a potiential race problem where these variables are changed but cannot be seen for the invocation. This is also safe since: 1. The method called inside mDefaultNetworks is already holding the same lock. 2. Multiple global variables are protected by this synchronized block. Test: TH Fix: 181642673 Change-Id: I58e7f124de1f0291e9323ab0b9cf8f52cec32818 --- .../com/android/server/net/NetworkStatsService.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/service-t/src/com/android/server/net/NetworkStatsService.java b/service-t/src/com/android/server/net/NetworkStatsService.java index 25e59d53cd..cc675501be 100644 --- a/service-t/src/com/android/server/net/NetworkStatsService.java +++ b/service-t/src/com/android/server/net/NetworkStatsService.java @@ -517,11 +517,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub { break; } case MSG_NOTIFY_NETWORK_STATUS: { - // If no cached states, ignore. - if (mLastNetworkStateSnapshots == null) break; - // TODO (b/181642673): Protect mDefaultNetworks from concurrent accessing. - handleNotifyNetworkStatus( - mDefaultNetworks, mLastNetworkStateSnapshots, mActiveIface); + synchronized (mStatsLock) { + // If no cached states, ignore. + if (mLastNetworkStateSnapshots == null) break; + handleNotifyNetworkStatus( + mDefaultNetworks, mLastNetworkStateSnapshots, mActiveIface); + } break; } case MSG_PERFORM_POLL_REGISTER_ALERT: {