Fix two battery risks in the per-app remote routing feature - #742
Merged
Conversation
Gate routing-prefs resolution on WireGuard being active: with WG off, stale wg_route_mode/wg_route prefs no longer keep fwd53's per-DNS-query UID lookup and upcall path enabled. Make the native flow-verdict cache 4-way set-associative instead of direct-mapped, so two concurrently busy flows sharing a hash no longer guarantee mutual eviction and per-packet fallback to a Binder/procfs UID lookup.
kasnder
marked this pull request as ready for review
August 20, 2026 21:43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reviewed the recent per-app remote (WireGuard) routing work for battery impact and found two risks, both fixed here. Both are traffic-driven, opt-in-feature costs — the shipped default (no routing overrides configured) is unaffected either way.
ServiceSinkhole.prepareUidAllowedresolvedwg_route_mode/wg_routepreferences unconditionally, so a user who had switched toselectedmode or added a per-app override, then disabled WireGuard, kept payingfwd53's per-DNS-query UID lookup, JNI upcall, and forwarded UDP session — with no tunnel left to route around. Now that state only resolves whenhasActiveWireGuard()is true; otherwise it collapses to the no-tunnel defaults (empty override set, default-tunnel, no direct DNS redirect), matching pre-feature behaviour.policy.c) was direct-mapped (1024 slots). Two concurrently active flows sharing a hash bucket evicted each other on every packet; each eviction forced a session-table walk (guaranteed futile for tunnelled flows, which never get anng_session) followed by a per-packet Binder/procfs UID lookup — the exact cost the cache exists to avoid. Replaced with a 4-way set-associative cache (256 sets × 4 ways, same ~56 KB budget), so collisions now need 5 simultaneously-hot flows sharing a set before any fallback lookup is forced.Test plan
./gradlew :app:compileGithubDebugJavaWithJavac— not run: this session's container has no Android SDK/NDK (android-envreports exit 3;gradlewfails at SDK-location configuration before compiling)../gradlew :app:testGithubDebugUnitTest— not run, same reason.assembleGithubDebug(native build) — not run, same reason.policy.candRemoteRoutingLogic.java.Recommend running the above before merge on a machine/session with the Android SDK/NDK available.
Generated by Claude Code