Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

romtsn
Copy link
Member

@romtsn romtsn commented Jul 16, 2025

📜 Description

  • Register network callback on a bg thread upon AndroidConnectionStatusProvider instantiation
  • Cache latest available NetworkCapabilities for 2 minutes
  • Use the capabilities to determine the current network status and type
    • If it's not available (cache expired), we'd fallback to retrieving it proactively and caching it
    • pre-API-23 we'd be always querying it, but it's a very small number of devices
  • onLost/onUnavailable would immediately send DISCONNECTED now as opposed to retrieving connectionStatus again
  • Implement a isSignificantChange check, because onNetworkCapabilitiesChanged can be called many times due to bandwidth change/etc. and we're only interested in transport/connectivity changes
  • Unregister the network callback when the SDK is closing
  • Cache ConnectivityManager instance too (it should be safe to cache once per process as it's not dependant on any particular Context)

Ran a test with perfetto to count the number of binder calls, and these are the results:

  • Send app to background
  • Freeze it
  • Turn wifi on and off 5 times,
  • Unfreeze it back

Before (83 binder calls from SentryExecService)

image

After (5 binder calls from SentryExecService)

image

💡 Motivation and Context

Fixes #4200

💚 How did you test it?

Manually + automated

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

  • Refactor NetworkBreadcrumbsIntegration so we register only one callback in AndroidConnectionStatusProvider and multiplex it
  • Register/unregister connectivity callback in foreground/background similar to SystemEvents integration

}
lastCacheUpdateTime = timeProvider.getCurrentTimeMillis();

options
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could drop this log call, but I thought it might be very useful to investigate potential issues. Under the hood getConnectionStatus and getConnectionType are very lightweight for a cache NetworkCapabilities instance, they just do bitmask operations to get the transport and capability

Copy link
Contributor

github-actions bot commented Jul 16, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 321.63 ms 369.32 ms 47.69 ms
Size 1.58 MiB 2.09 MiB 521.11 KiB

Previous results on branch: rz/perf/less-ipc

Startup times

Revision Plain With Sentry Diff
6ef06c6 383.29 ms 403.25 ms 19.96 ms
91ac06a 386.02 ms 407.53 ms 21.51 ms

App size

Revision Plain With Sentry Diff
6ef06c6 1.58 MiB 2.09 MiB 520.53 KiB
91ac06a 1.58 MiB 2.09 MiB 520.52 KiB

Copy link
Member

@markushi markushi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I've left a nit about the transports, but looks good otherwise!

@romtsn romtsn enabled auto-merge (squash) July 30, 2025 21:40
capabilities[0] = NetworkCapabilities.NET_CAPABILITY_INTERNET;
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.M) {
capabilities[1] = NetworkCapabilities.NET_CAPABILITY_VALIDATED;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Static Array Mutation Causes Network Capability Inconsistency

The static final capabilities array is mutated in the AndroidConnectionStatusProvider constructor based on instance-specific SDK versions. This creates a race condition where multiple instances, particularly those with different API levels, overwrite the shared static array, leading to inconsistent and incorrect network capability checks across all instances.

Locations (1)
Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While there might be multiple SDK instances running at a time, it's not possible for them to run on different API levels at the same time, because the device can only target one API level at a time.

@romtsn romtsn merged commit 3eb6466 into main Jul 30, 2025
34 checks passed
@romtsn romtsn deleted the rz/perf/less-ipc branch July 30, 2025 21:52
adinauer added a commit that referenced this pull request Aug 8, 2025
* Do not report cached events as lost

* E2E tests for OpenTelemetry based console sample (#4563)

* e2e tests for console app

* fix test failures by waiting for 10s after first try to find envelopes

* add system-test-runner.py script to replace bash scripts for running e2e / system tests

* use py script for ci, cleanup, makefile

* Format code

* remove bash scripts

* install requests module

* api

* fix gh script

* Implement E2E tests for OTel based console sample

* fixes after merge

* Format code

* e2e tests for console app

* Implement E2E tests for OTel based console sample

* fixes after merge

* Format code

* api

* Reduce scope forking when using OpenTelemetry (#4565)

* Reduce scope forking in OpenTelemetry

* Format code

* api

* changelog

---------

Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>

* SDKs send queue is no longer shutdown immediately on re-init (#4564)

* Let queue drain on a restart

* Format code

* Format code

* Update sentry-samples/sentry-samples-console-opentelemetry-noagent/src/test/kotlin/sentry/systemtest/ConsoleApplicationSystemTest.kt

* Let queue drain on a restart

* Format code

* Format code

* Update sentry-samples/sentry-samples-console-opentelemetry-noagent/src/test/kotlin/sentry/systemtest/ConsoleApplicationSystemTest.kt

* adapt tests

* changelog

---------

Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>

---------

Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>

* release: 8.18.0

* ref(replay): Use main thread to schedule capture (#4542)

* perf(connectivity): Cache network capabilities and status to reduce IPC calls (#4560)

* fix(breadcrumbs): Deduplicate battery breadcrumbs (#4561)

* fix(ci): remove obsolete NDK debug symbols (#4581)

As they don't exist anymore and this is done within sentry-native directly: https://github.com/getsentry/sentry-native/pull/1327/files

* fix(android): Remove unused method (#4585)

* fix(android): Remove unused method

* Update Changelog

* Add rules file for documenting SDK offline behaviour (#4572)

#skip-changelog

## 📜 Description
<!--- Describe your changes in detail -->
Add rules file for documenting SDK offline behaviour

## 💡 Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
Should help speed up AI reasoning about the SDK offline/retry behaviour.

## 💚 How did you test it?


## 📝 Checklist
<!--- Put an `x` in the boxes that apply -->

- [ ] I added tests to verify the changes.
- [ ] No new PII added or SDK only sends newly added PII if `sendDefaultPII` is enabled.
- [ ] I updated the docs if needed.
- [ ] I updated the wizard if needed.
- [ ] Review from the native team if needed.
- [ ] No breaking change or entry added to the changelog.
- [ ] No breaking change for hybrid SDKs or communicated to hybrid SDKs.


## 🔮 Next steps

* perf(connectivity): Have only one NetworkCallback active at a time (#4562)

* fix(scripts): update-gradle script set-version (#4591)

* fix: sentry-android-ndk proguard rule keeps all native class (#4427)

* fix: sentry-androi-ndk proguard rule keeps all native class

* docs: update CHANGELOG

* fix: update CHANGELOG

* Update CHANGELOG.md

* Update CHANGELOG.md

---------

Co-authored-by: Markus Hintersteiner <m.hintersteiner@gmail.com>
Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io>

* refactor(lifecycle): Use single lifecycle observer (#4567)

* perf(connectivity): Cache network capabilities and status to reduce IPC calls

* changelog

* Changelog

* revert

* fix(breadcrumbs): Deduplicate battery breadcrumbs

* ref

* Changelog

* Fix test

* perf(connectivity): Have only one NetworkCallback active at a time

* Changelog

* perf(integrations): Use single lifecycle observer

* Add tests

* Changelog

* Fix tests

* Improve callback handling and test visibility (#4593)

* Null-check lifecycleObserver

---------

Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io>

* fix(sqlite): Fix abstract method error (#4597)

* fix(sqlite): Fix abstract method error

* Update CHANGELOG.md

* Suppress metadata version checks

* perf(integrations): Do not register for SystemEvents and NetworkCallbacks when launched with background importance (#4579)

* fix(android): Ensure frame metrics listeners are registered/unregistered on the main thread (#4582)

* fix(android): Ensure frame metrics listeners are registered/unregistered on the main thread

* Fix race conditions

* Update Changelog

* Update CHANGELOG.md

* Address PR feedback

* perf(executor): Prewarm SentryExecutorService (#4606)

* review feedback

* changelog

* pass through whether cache stored in AndroidEnvelopeCache + test

* Format code

---------

Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
Co-authored-by: getsentry-bot <bot@sentry.io>
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io>
Co-authored-by: Ghasem Shirdel <shirdelghasem79@gmail.com>
Co-authored-by: Markus Hintersteiner <m.hintersteiner@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache current network connection state

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.