Skip to content

Commit 7ae75a2

Browse files
committed
update cache inside lock
avoid concurrent cache updates
1 parent e520ff7 commit 7ae75a2

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/internal/util/AndroidConnectionStatusProvider.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public final class AndroidConnectionStatusProvider
5454
private static final @NotNull AutoClosableReentrantLock childCallbacksLock =
5555
new AutoClosableReentrantLock();
5656
private static final @NotNull List<NetworkCallback> childCallbacks = new ArrayList<>();
57+
private static final AtomicBoolean isUpdatingCache = new AtomicBoolean(false);
5758

5859
private static final int[] transports = {
5960
NetworkCapabilities.TRANSPORT_WIFI,
@@ -268,19 +269,16 @@ private void updateCacheAndNotifyObservers(
268269

269270
// Only notify observers if something meaningful changed
270271
if (shouldUpdate) {
272+
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
271273
cachedNetworkCapabilities = networkCapabilities;
272274
lastCacheUpdateTime = timeProvider.getCurrentTimeMillis();
275+
final @NotNull ConnectionStatus status = getConnectionStatusFromCache();
273276
options
274277
.getLogger()
275278
.log(
276279
SentryLevel.DEBUG,
277-
"Cache updated - Status: "
278-
+ getConnectionStatusFromCache()
279-
+ ", Type: "
280-
+ getConnectionTypeFromCache());
280+
"Cache updated - Status: " + status + ", Type: " + getConnectionTypeFromCache());
281281

282-
final @NotNull ConnectionStatus status = getConnectionStatusFromCache();
283-
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
284282
for (final @NotNull IConnectionStatusObserver observer :
285283
connectionStatusObservers) {
286284
observer.onConnectionStatusChanged(status);
@@ -378,27 +376,31 @@ private void updateCache() {
378376
// Fallback: query current active network in the background
379377
submitSafe(
380378
() -> {
381-
final ConnectivityManager connectivityManager =
379+
// Avoid concurrent updates
380+
if (!isUpdatingCache.getAndSet(true)) {
381+
final ConnectivityManager connectivityManager =
382382
getConnectivityManager(context, options.getLogger());
383-
if (connectivityManager != null) {
384-
final @Nullable NetworkCapabilities capabilities =
383+
if (connectivityManager != null) {
384+
final @Nullable NetworkCapabilities capabilities =
385385
getNetworkCapabilities(connectivityManager);
386386

387-
try (final @NotNull ISentryLifecycleToken ignored2 = lock.acquire()) {
388-
cachedNetworkCapabilities = capabilities;
389-
lastCacheUpdateTime = timeProvider.getCurrentTimeMillis();
387+
try (final @NotNull ISentryLifecycleToken ignored2 = lock.acquire()) {
388+
cachedNetworkCapabilities = capabilities;
389+
lastCacheUpdateTime = timeProvider.getCurrentTimeMillis();
390390

391-
if (capabilities != null) {
392-
options
391+
if (capabilities != null) {
392+
options
393393
.getLogger()
394394
.log(
395-
SentryLevel.DEBUG,
396-
"Cache updated - Status: "
397-
+ getConnectionStatusFromCache()
398-
+ ", Type: "
399-
+ getConnectionTypeFromCache());
395+
SentryLevel.DEBUG,
396+
"Cache updated - Status: "
397+
+ getConnectionStatusFromCache()
398+
+ ", Type: "
399+
+ getConnectionTypeFromCache());
400+
}
400401
}
401402
}
403+
isUpdatingCache.set(false);
402404
}
403405
});
404406

0 commit comments

Comments
 (0)