Skip to content

[client] Build the Android network list from a single peer snapshot - #7549

Open
pappz wants to merge 1 commit into
mainfrom
networks-single-status
Open

pappz wants to merge 1 commit into
mainfrom
networks-single-status

Conversation

@pappz

@pappz pappz commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

Describe your changes

The Android app asks the engine for the list of networks with Networks().
For every network in that list, the engine called GetFullStatus() to find
the peer that serves the route. One GetFullStatus() call takes eight locks
on the status recorder and copies the state of every peer. With N networks
this ran N times per Networks() call.

This is cheap with a few peers. With 100+ peers it is not: an Android ANR
report showed 283 threads waiting inside Networks() at the same time, all
queued on the status recorder lock, and the UI thread stuck behind them.
The peer list callback storm that starts those calls is fixed separately in
#7546; this PR removes the cost per call.

Now Networks() takes one snapshot of the peer states at the start, builds a
route -> peer map from it, and looks up each network in that map. The two
cheap GetPeer fallbacks stay as they were, so the result does not change.

Only the Android binding has this pattern, the iOS SDK already builds its
route status from a single snapshot.

Issue ticket number and link

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • I ran and tested this change locally — I did not rely on CI to find out whether it works
  • This PR has a single purpose (not a fix + refactor + feature in one)
  • This change is a trivial fix, OR it links an issue the NetBird team agreed on beforehand. Changes to the public API, gRPC protocols, functionality behavior, CLI / service flags, or new features always need that agreement first. See CONTRIBUTING.md.

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

Review in cubic

Summary by CodeRabbit

  • Bug Fixes

    • Improved network route selection to more reliably identify the peer responsible for each network.
    • Added fallback handling to maintain route selection when a direct owner cannot be identified.
    • Resolved cases involving shared routes by consistently selecting the first applicable peer.
  • Tests

    • Added coverage for multiple routes, peers without routes, unavailable owners, and shared-network scenarios.

Networks() called GetFullStatus() once per network to find the peer
that serves it, copying every peer state and taking eight recorder
locks each time. With 100+ peers and the UI calling Networks() from
every peer list change, this queued hundreds of callers on the status
recorder lock. Take one snapshot per call and index it by route.
@coderabbitai

coderabbitai Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 0086a9c0-131e-49c0-a7b4-e313459316aa

📥 Commits

Reviewing files that changed from the base of the PR and between 58b5263 and 30f4a43.

📒 Files selected for processing (3)
  • client/android/client.go
  • client/android/route_owners.go
  • client/android/route_owners_test.go

Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The Android client now builds a route-owner map from resolved peer states and uses it during network construction. Route ownership tests cover multiple routes, idle peers, missing owners, and duplicate routes.

Changes

Route owner lookup

Layer / File(s) Summary
Owner map and network selection
client/android/route_owners.go, client/android/client.go, client/android/route_owners_test.go
routeOwners maps each network to the first peer state that serves it. Networks() builds this map once and passes it through network construction. findBestRoutePeer checks the map before its fallback selections. Tests cover route mapping and first-peer precedence.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to 30f4a

The route-owner tests run successfully, and the new lookup preserves existing network peer selection behavior.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The description explains the performance change and its scope, but it omits the required issue ticket or approved discussion link for a behavior change. It also leaves the test and local-validation ch… Add the required issue ticket or approved discussion link. Update the checklist to reflect the added tests and local validation. Explain why documentation is not needed.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: the Android network list now uses a single peer snapshot.
Full details: Description check

Explanation

The description explains the performance change and its scope, but it omits the required issue ticket or approved discussion link for a behavior change. It also leaves the test and local-validation checklist items unchecked even though tests were added, and does not explain why documentation is not needed.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch networks-single-status

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1 issue found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="client/android/route_owners.go">

<violation number="1" location="client/android/route_owners.go:7">
P2: routeOwners picks whichever peer appears first in the states slice, but GetPeerStates builds that slice from map iteration (d.peers), so the order is nondeterministic. For a route served by an HA group, the chosen owner can be a random, disconnected sibling, and findBestRoutePeer returns that state without checking ConnStatus, so the UI can show a stale Disconnected status for a network a connected peer actively serves. Prefer a connected peer (StatusConnected) when several peers advertise the same route, or at least document that selection is arbitrary.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

"github.com/netbirdio/netbird/client/internal/peer"
)

func routeOwners(states []peer.State) map[string]peer.State {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: routeOwners picks whichever peer appears first in the states slice, but GetPeerStates builds that slice from map iteration (d.peers), so the order is nondeterministic. For a route served by an HA group, the chosen owner can be a random, disconnected sibling, and findBestRoutePeer returns that state without checking ConnStatus, so the UI can show a stale Disconnected status for a network a connected peer actively serves. Prefer a connected peer (StatusConnected) when several peers advertise the same route, or at least document that selection is arbitrary.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At client/android/route_owners.go, line 7:

<comment>routeOwners picks whichever peer appears first in the states slice, but GetPeerStates builds that slice from map iteration (d.peers), so the order is nondeterministic. For a route served by an HA group, the chosen owner can be a random, disconnected sibling, and findBestRoutePeer returns that state without checking ConnStatus, so the UI can show a stale Disconnected status for a network a connected peer actively serves. Prefer a connected peer (StatusConnected) when several peers advertise the same route, or at least document that selection is arbitrary.</comment>

<file context>
@@ -0,0 +1,17 @@
+	"github.com/netbirdio/netbird/client/internal/peer"
+)
+
+func routeOwners(states []peer.State) map[string]peer.State {
+	owners := make(map[string]peer.State)
+	for _, state := range states {
</file context>

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

A route is added to a peer state only by the route watcher, for the currently chosen route peer (routemanager/client/client.go addAllowedIPs), and removed when the choice changes. So at most one peer carries a given route key at a time; HA siblings that are not selected do not have it. The first-wins branch is only a guard. Same map order as the old GetFullStatus() scan, so behavior is unchanged.

@github-actions

Copy link
Copy Markdown

Release artifacts

Built for PR head 30f4a43 in workflow run #18985.

Artifact Link
All release artifacts Download
Linux packages Download
Windows packages Download
macOS packages Download
UI artifacts Download
UI GTK3 artifacts Download
UI macOS artifacts Download

GHCR images (amd64)

This comment is updated by the Release workflow. Artifact links expire according to the workflow retention policy.

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.

1 participant