@@ -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